- 安装前准备
- 保证centos 7能够连外网。
- 关闭防火墙
- 修改centos为中国时区
- 关闭SELinux
- 更新操作系统:
- 安装docker
- 查看当前可安装的docker版本
- 设置yum源为阿里源
- 安装docker版本
- 检查docker是否安装成功
- 启动docker服务
- 关于防火墙补充说明
- 安装mysql
- 规划Mysql目录
- 拉取docker镜像
- 拷贝my.cnf
- 启动mysql容器
- docker启动成功后需要设置远程访问权限
- 测试MySql8是否可用
- docker部署mysql8常见问题解决
- pull 镜像时 Downloading 卡住的问题。
- Docer启动失败报:mysqld: Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)
- mysql客户端访问报错:The driver has not received any packets from the server
- message from server: "Host 'XXX.XXX.X.XX' is not allowed to connect to this MySQL server
- Access denied for user 'root'@'XXX.XXX.X.XX' (using password: YES) Access denied for user 'root'@'
[root@localhost ~]# date Fri May 13 07:25:15 PDT 2022 [root@localhost ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime cp: overwrite '/etc/localtime'? y [root@localhost ~]# date Fri May 13 22:25:53 CST 2022关闭SELinux
SELinux本身是一个安全组件,但是根据官方文档,现在并不是所有的程序都能完美支持SELinux,而且涉及到服务器安全的话,现在也有太多好的方案可以选择,所以这个SELinux关掉就好,避免出现诡异的问题
vim /etc/sysconfig/selinux
修改SELINUX=enforcing为SELINUX=disabled
保存后重启系统。
yum -y update安装docker
cento 7 下安装docker比较简单。
查看当前可安装的docker版本[root@localhost /]# yum list docker-ce --showduplicates | sort -r BDB2053 Freeing read locks for locker 0x588: 75174/140274931337024 * updates: mirror.lzu.edu.cn Loading mirror speeds from cached hostfile Loaded plugins: fastestmirror, langpacks * extras: ftp.sjtu.edu.cn docker-ce.x86_64 3:20.10.9-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.8-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.7-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.6-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.5-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.4-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.3-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.2-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.16-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.15-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.14-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.1-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.13-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.12-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.11-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.10-3.el7 docker-ce-stable docker-ce.x86_64 3:20.10.0-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.9-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.8-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.7-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.6-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.5-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.4-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.3-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.2-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.15-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.14-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.13-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.12-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.11-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.10-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.0-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.9-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.8-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.7-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.6-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.5-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.2-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.1-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.0-3.el7 docker-ce-stable docker-ce.x86_64 18.06.3.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.2.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.1.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 18.03.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.12.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.09.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.3.ce-1.el7 docker-ce-stable docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable * base: ftp.sjtu.edu.cn Available Packages
其他文章有说要安装更新包,但是我自己操作并不用特意安装,在这里贴出来如果真有需要的可以执行一下。
yum install -y yum-utils device-mapper-persistent-data lvm2设置yum源为阿里源
不然使用默认源的话时间会比较长。
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo Loaded plugins: fastestmirror, langpacks adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo repo saved to /etc/yum.repos.d/docker-ce.repo安装docker版本
选择一个docker安装版本,版本的规则:docker-ce-版本号,比如上面的第一行记录
docker-ce.x86_64 3:20.10.9-3.el7 docker-ce-stable
20.10.9-3.el7就是他的版本号
yum -y install docker-ce-20.10.9-3.el7
控制台输出内容太多,就不粘贴了。
检查docker是否安装成功[root@localhost ~]# docker version Client: Docker Engine - Community Version: 20.10.16 API version: 1.41 Go version: go1.17.10 Git commit: aa7e414 Built: Thu May 12 09:19:45 2022 OS/Arch: linux/amd64 Context: default Experimental: true Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
出现如上信息说明docker安装成功,只是没有启动而已。
设置docker 开机自动启动
systemctl enable docker启动docker服务
systemctl start docker
使用docker version命令查看docker是否启动成功
[root@localhost ~]# docker version Client: Docker Engine - Community Version: 20.10.16 API version: 1.41 Go version: go1.17.10 Git commit: aa7e414 Built: Thu May 12 09:19:45 2022 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.16 API version: 1.41 (minimum version 1.12) Go version: go1.17.10 Git commit: f756502 Built: Thu May 12 09:18:08 2022 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.6.4 GitCommit: 212e8b6fa2f44b9c21b2798135fc6fb7c53efc16 runc: Version: 1.1.1 GitCommit: v1.1.1-0-g52de29d docker-init: Version: 0.19.0 GitCommit: de40ad0关于防火墙补充说明
生产环境一般不会关闭防火墙,而个人练习环境或者研发环境一般都是关闭防火墙的。如果你是在防火墙开启状态下安装的docker并启动的服务,后续使用docker容器时保持防火墙开启就好,反之就一直保持防火墙关闭,主要是为了避免docker服务无法连通的情况。其实通过配置firewall和iptables也能解决,只是麻烦了一点。
安装mysql 规划Mysql目录因为使用docker安装,所以必须规划好映射目录。docker的映射目录是为了实现数据持久化,不然docker重启后容器内所有修改的内容都会丢失。对于线上生产环境来说就是严重的事故。需要规划的目录内容包括:
- 配置文件my.cnf
- 数据文件目录
- 日志文件目录
- redo文件目录。
- mysql-files目录,如果不指定的话会导致docker服务无法正常启动
端口一般用默认的就好。
首先df -h查看硬盘情况
mysql的映射目录,建议创建一个单独的目录,将mysql所有的映射目录都放在这个目录,方便管理。以下创建映射目录和文件。
mkdir -p /data/mysql/docker/ mkdir -p /data/mysql/docker/mysql-files mkdir -p /data/mysql/docker/mysql cd /data/mysql/docker/ vim my.cnf # 输入一个空行保存退出即可 vim error.log # 输入一个空行保存退出即可拉取docker镜像
直接最新版本:
[root@localhost ~]# docker pull mysql Using default tag: latest latest: Pulling from library/mysql c32ce6654453: Downloading 20.53MB 415d08ee031a: Download complete 7a38fec2542f: Download complete 352881ee8fe9: Download complete b8e20da291b6: Download complete 66c2a8cc1999: Download complete d3a3a8e49878: Download complete e33a48832bec: Download complete 410b942b8b28: Downloading [=======> ] 17.28MB/109.1MB d5323c9dd265: Download complete 3212737f31c0: Download complete d0032d4b0dc5: Download complete
等待pull结束。
拷贝my.cnf首先启动过一个临时的容器,以便拷贝出my.cnf。不然直接用映射目录启动docker后,my.cnf是空的。
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name temp mysql docker ps #获取到镜像ID:063daa9d6a43,然后拷贝文件 docker cp 063d:/etc/mysql/my.cnf /data/mysql/docker/my.cnf # 拷贝结束后删除docker docker stop 063daa9d6a43 docker rm 063daa9d6a43
启动mysql容器在网络上有的人说要增加skip-grant-tables,这样可以解决root用户无法在容器内登录的问题,我的建议这个参数非必要不要增加,会导致其他问题,例如 read 0 bytes before connection was unexpectedly lost。```
如果按照上面的映射目录,则启动docker的命令如下:
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -v /data/mysql/docker/my.cnf:/etc/mysql/my.cnf -v /data/mysql/docker/mysql-files:/var/lib/mysql-files -v /data/mysql/docker/mysql:/var/lib/mysql -v /data/mysql/docker/error.log:/var/log/mysql/error.log --name mysql-8.0 mysql
其中最后一个mysql,是我的镜像名称,具体镜像名称可以用images查看:
[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mysql latest 3218b38490ce 4 months ago 516MBdocker启动成功后需要设置远程访问权限
不然客户端无法登录数据库
docker exec -it 6734 /bin/bash #其中6734是容器id,用docker ps可以查看 mysql -uroot use mysql; update user set host='%' where user='root'; FLUSH PRIVILEGES;测试MySql8是否可用
新建一个SQL编辑器,执行SQL命令成功即可。
在pull 镜像时,出现 Downloading总是卡住,每次重启docker能多安装一些,或者等待很长时间才能走一点点。
网络上说需要重新安装docker,我的环境上并不起作用,reboot了也不行。最后还是通过设置阿里云加速解决的。
首先登录阿里云,进入镜像工具-镜像加速器,复制加速器地址。
vim /etc/docker/daemon.json
将加速的内容复制粘贴进去,注意这里的加速器地址需要更新成你自己的,因为这个地址不是长久有效的。
{
"registry-mirrors": ["https://gten58ev.mirror.aliyuncs.com"]
}
wq保存,然后重启相关服务
sudo systemctl daemon-reload sudo systemctl restart docker
再次pull时就很快了。
[root@localhost ~]# docker pull mysql Using default tag: latest latest: Pulling from library/mysql 72a69066d2fe: Pull complete 93619dbc5b36: Pull complete 99da31dd6142: Pull complete 626033c43d70: Pull complete 37d5d7efb64e: Pull complete ac563158d721: Pull complete d2ba16033dad: Pull complete 688ba7d5c01a: Pull complete 00e060b6d11d: Pull complete 1c04857f594f: Pull complete 4d7cfa90e6ea: Pull complete e0431212d27d: Pull complete Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latestDocer启动失败报:mysqld: Error on realpath() on ‘/var/lib/mysql-files’ (Error 2 - No such file or directory)
原因就是在启动docker时映射目录没有指定mysql-files,指定了外部配置文件与外部存储路径时也需要同时指定mysql-files。
解决方法:增加-v /data/mysql/docker/mysql-files:/var/lib/mysql-files即可,其中 /data/mysql/docker/是你自己创建的映射目录,根据实际目录改写即可。
DOCKER服务启动后,在服务器本地能访问mysql,但是客户端远程访问的时候却提示:read 0 bytes before connection was unexpectedly lost.,具体的报错信息如下:
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
这个情况下先检查下你的my.cnf里是不是有skip-grant-tables,如果有则去掉。然后重启docker服务
docker restart 6734dcb7c0ac
然后登录docker容器
docker exec -it 6734dcb7c0ac /bin/bash
进入mysql
mysql -uroot use mysql SET PASSWORD FOR root@localhost = '123456';
如果你碰到这个问题的时候,很可能你的客户端远程登录也访问不了。MYSQL8下开启远程访问权限和之前的版本不一样
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; mysql> update user set host='%' where user='root'; mysql> flush privileges;
此时再用客户端连接就可以了。
设置远程登录即可。
docker exec -it 6734 /bin/bash #其中6734是容器id,用docker ps可以查看 mysql -uroot use mysql; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; FLUSH PRIVILEGES;Access denied for user ‘root’@‘XXX.XXX.X.XX’ (using password: YES) Access denied for user ‘root’@’
按照如下步骤解决:
docker exec -it 6734 /bin/bash #其中6734是容器id,用docker ps可以查看 mysql -uroot use mysql; SET PASSWORD FOR root@localhost = '123456'; update user set host='%' where user='root'; FLUSH PRIVILEGES;
不用重启docker,重新登录即可。



