docker-compose内mysql 挂载数据库目录,启动时mysql容器自动退出

Posted fqxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker-compose内mysql 挂载数据库目录,启动时mysql容器自动退出相关的知识,希望对你有一定的参考价值。

2019-05-06


 

问题现象

使用docker-compose部署web+mysql架构网站,出现docker-compose up -d启动之后,mysql容器自动退出现象

技术图片

docker-compose.yml

version: 2 

services:

    dzzoffice:
       image: cheggwpt/php7.2-nginx
       ports: 
          - "80:80"
       volumes:
          - "/mnt/dzzoffice:/app"
       links:
          - db
 
    db:
       image: centos/mysql-57-centos7
       environment:
          - MYSQL_USER=xxx
          - MYSQL_PASSWORD=xxx
          - MYSQL_DATABASE=xxx
       volumes:
          - "/mnt/mysql/data:/var/lib/mysql/data"

 解决方案:

真正的问题所在其实就是在服务器上的volume目录/mnt/mysql/data和容器里目录/var/lib/mysql/data拥有者不一样导致的,那么如何查看拥有者,需要使用如下几条指令

查看容器中/var/lib/mysql的所有者

[[email protected] dzzoffice]# docker run -ti --rm --entrypoint="/bin/bash" centos/mysql-57-centos7 -c "ls -la /var/lib/mysql"
total 12
drwxrwxr-x 1 mysql root 4096 Apr  3 07:29 .
drwxr-xr-x 1 root  root 4096 Apr  3 07:29 ..
drwxrwxr-x 1 mysql root 4096 Apr  3 07:29 data

 以上可以看出,data属于mysql用户组,而在宿主机上,新建的data目录却属于root用户组

[[email protected] mysql]# ll
total 4
drwxr-xr-x 2 root root 4096 Apr 30 16:22 data

也就是说,这两个目录的所有者不同导致的权限问题,现在把他们的id统一就可以了,统一前要先查出来容器里的mysql用户组id,然后修改服务器的/mnt/mysql/data下的用户组id

查出来容器里的mysql用户组id

docker run -ti --rm --entrypoint="/bin/bash" centos/mysql-57-centos7 -c "cat /etc/group"
root:x:0:mysql bin:x:1: daemon:x:2: sys:x:3: adm:x:4: tty:x:5: disk:x:6: lp:x:7: mem:x:8: kmem:x:9: wheel:x:10: cdrom:x:11: mail:x:12: man:x:15: dialout:x:18: floppy:x:19: games:x:20: tape:x:33: video:x:39: ftp:x:50: lock:x:54: audio:x:63: nobody:x:99: users:x:100: utmp:x:22: utempter:x:35: input:x:999: systemd-journal:x:190: systemd-network:x:192: dbus:x:81: cgred:x:998: mysql:x:27:

可以看到mysql的组ID是27,将宿主机的data目录组ID改为27即可

[[email protected] dzzoffice]# chown -R 27 mysql/data
[[email protected] dzzoffice]# cat /etc/group
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
wheel:x:10:
cdrom:x:11:
mail:x:12:postfix
man:x:15:
dialout:x:18:
floppy:x:19:
games:x:20:
tape:x:30:
video:x:39:
ftp:x:50:
lock:x:54:
audio:x:63:
nobody:x:99:
users:x:100:
utmp:x:22:
utempter:x:35:
avahi-autoipd:x:170:
ssh_keys:x:999:
systemd-journal:x:190:
dbus:x:81:
polkitd:x:998:
dip:x:40:
tss:x:59:
ntp:x:38:
slocate:x:21:
postdrop:x:90:
postfix:x:89:
sshd:x:74:
tcpdump:x:72:
input:x:997:
systemd-network:x:192:
cgred:x:996:
gitlab-www:x:995:
git:x:994:
gitlab-redis:x:993:
gitlab-psql:x:992:
gitlab-prometheus:x:991:
mysql:x:27:

以上可看到修改后的data组ID已经改变,已经与容器内部data相同,再次启动容器,发现正常运行

[[email protected] dzzoffice]# docker-compose up -d
Creating dzzoffice_db_1 ... 
Creating dzzoffice_db_1 ... done
Creating dzzoffice_dzzoffice_1 ... 
Creating dzzoffice_dzzoffice_1
Creating dzzoffice_dzzoffice_1 ... done
[[email protected] dzzoffice]# docker ps 
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                  PORTS                                     NAMES
9ec3fe00ec33        cheggwpt/php7.2-nginx     "/entrypoint.sh su..."   1 second ago        Up Less than a second   443/tcp, 9000/tcp, 0.0.0.0:8000->80/tcp   dzzoffice_dzzoffice_1
62db492d56c5        centos/mysql-57-centos7   "container-entrypo..."   3 seconds ago       Up 2 seconds            3306/tcp                                  dzzoffice_db_1

 

参考文章:

https://blog.csdn.net/grape875499765/article/details/80089853

以上是关于docker-compose内mysql 挂载数据库目录,启动时mysql容器自动退出的主要内容,如果未能解决你的问题,请参考以下文章

Docker 挂载Mysql用volume方式 docker-compose 登陆不进去

docker-compose:为绑定挂载和托管挂载定义挂载

Docker部署Docker-compose部署redis容器及启动失败挂载失败等问题

Docker部署Docker-compose部署redis容器及启动失败挂载失败等问题

Docker部署Docker-compose部署redis容器及启动失败挂载失败等问题

在 docker-compose 中挂载 NFS 卷时连接超时