Docker环境
yum安装docker
第一步 yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 第二步使用阿里云镜像 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && yum makecache fast 第三步安装 yum install docker-ce docker-ce-cli containerd.io systemctl start docker systemctl enable docker docker run hello-world
配置加速器
配置加速器能让镜像下载速度更快,这个要开通才有
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://..........mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
安装Mysql
固定的套路,后面的-v参数是对容器卷进行挂载,使得我们操作本地对应的容器卷文件就可以控制容器内部的文件,下面会有详细的解释。不必死记硬背,重要的是理解理解!!!
docker run -p 3306:3306 --name mysql \ -v /mydata/mysql/log:/var/log/mysql \ -v /mydata/mysql/data:/var/lib/mysql \ -v /mydata/mysql/conf:/etc/mysql \ -e MYSQL_ROOT_PASSWORD=root \ -d mysql:5.7
- -p 3306:3306:端口映射,本地3306映射容器内3306
- -v /mydata/mysql/conf:/etc/mysql:将容器配置文件夹/etc/mysql,挂在到主机/mydata/mysql/conf
- -v /mydata/mysql/log:/var/log/mysql:将日志文件夹/var/log/mysql挂载到主机/mydata/mysql/log
- -v /mydata/mysql/data:/var/lib/mysql/:将数据文件夹挂载到主机
- -e MYSQL_ROOT_PASSWORD=root:初始化root用户的密码
MySQL 配置
每一步都不可少,这里主要是对mysql编码设置,因为mysql默认是拉丁编码,是不能识别中文的
vi /mydata/mysql/conf/my.cnf [client] default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_unicode_ci skip-character-set-client-handshake skip-name-resolve #跳过域名解析
允许远程登录
docker exec -it mysql mysql -uroot -proot grant all privileges on . to 'root'@'%' identified by 'root' with grant option;
flush privileges;
Mysql本地连接失败
- pc和vm能互相ping通
- 关闭firewalld,或放开端口
- 打开ipv4转发
- vi /etc/sysctl.conf net.ipv4.ip_forward=1 #添加此行配置
- systemctl restart network && systemctl restart docker
- sysctl net.ipv4.ip_forward
- 如果返回为“net.ipv4.ip_forward = 1”则表示修改成功
安装Redis
docker run -p 6379:6379 --name redis -v /mydata/redis/data:/data \ -v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf \ -d redis redis-server /etc/redis/redis.conf
- -p 6379:6379:端口映射,本地6379映射容器内6379
- -v /mydata/redis/data:/data 将容器数据文件夹/data,挂在到主机/mydata/redis/data
- -v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf 将容器配置文件夹/etc/redis/redis.conf,挂在到主机/mydata/redis/conf/redis.conf