背景
最近在尝试容器搭建MySQL集群时碰到一个错误,启动MySQL时碰到一个,经过排查解决,在此做一个记录
问题过程
1、启动MySQL容器
$ sudo docker run -d -p 3306:3306 \
> --name mysql \
> -v /opt/mysql/log:/var/log/mysql \
> -v /opt/mysql/data:/var/lib/mysql \
> -v /opt/mysql/conf:/etc/mysql \
> -e MYSQL_ROOT_PASSWORD=root \
> mysql
3cf4012772cc2e22732aa6d5eb716d7b290a95da25b750d4a1189e77ab985f33
# 运行后,查看运行中的容器发现没有刚启动MySQL
[bing@centosone nginx]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c7b103435d1 nginx:latest "/docker-entrypoint.…" 8 hours ago Up 8 hours 0.0.0.0:443->443/tcp, :::443->443/tcp, 0.0.0.0:85->80/tcp, [::]:85->80/tcp my-nginx
28cda42916c3 nginx:latest "/docker-entrypoint.…" 15 hours ago Up 15 hours 0.0.0.0:82->80/tcp, [::]:82->80/tcp
- mysql/log: 存储MySQL日志
- mysql/data: 是数据库文件(库表数据存放处)存放的地方。必须要挂载到容器外,否则容器重启一切数据消失。
- mysql/conf: mysql配置,mysql启动时读取配置
查看所有容器docker ps -a:
发现MySQL是已经exit了
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e163949caa2 mysql "docker-entrypoint.s…" 20 minutes ago Exited (1) 17 minutes ago mysql
然后尝试删除容器重启还是一样:
$ sudo docker rm 4e1
4e1
$ sudo docker run -p 3306:3306
--name mysql
-v /opt/mysql/log:/var/log/mysql
-v /opt/mysql/data:/var/lib/mysql
-v /opt/mysql/conf:/etc/mysql
-e MYSQL_ROOT_PASSWORD=root
-d mysql
b3153c7d9d33f28e9808fbd6f8feea44ac12e45f9aec637a789a29b9f7f5dcd2
# 仍然是exit状态
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b3153c7d9d33 mysql "docker-entrypoint.s…" 3 seconds ago Exited (1) 2 seconds ago mysql
查看容器日志:
$ sudo docker logs --tail 100 --follow --timestamps mysql
问题原因:
如上图,查看日志发现是没有配置文件目录,我们的命令指定的配置文件路径是/etc/mysql/,而容器读取的配置文件路径是/etc/mysql/conf.d/。
在这一行-v /opt/mysql/conf:/etc/mysql,我们将主机路径/opt/mysql/conf挂载到容器路径/etc/mysql下,实际上容器配置在/etc/mysql/conf.d/目录下。
修改启动命令
修改启动命令中的配置文件路径,再次重新启动成功,如下:
sudo docker run -p 3306:3306 --name mysql \
-v /opt/mysql/log:/var/log/mysql \
-v /opt/mysql/data:/var/lib/mysql \
-v /opt/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql
ef8d15ff3c2828b718357ad7d41117ca46c3fec7bb6b3a365c886926b275fbb8
# 启动成功
sudo docker ps
[sudo] bing 的密码:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ef8d15ff3c28 mysql "docker-entrypoint.s…" 11 minutes ago Up 11 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp
mysql容器终于是 Up 状态了