码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2019-05-06 19:03:19      阅读:442      评论:0      收藏:0      [点我收藏+]

标签:alt   vol   The   mys   mon   article   names   挂载   图片   

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的所有者

[root@gitlab 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用户组

[root@gitlab 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即可

[root@gitlab dzzoffice]# chown -R 27 mysql/data
[root@gitlab 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相同,再次启动容器,发现正常运行

[root@gitlab 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
[root@gitlab 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容器自动退出

标签:alt   vol   The   mys   mon   article   names   挂载   图片   

原文地址:https://www.cnblogs.com/fqxy/p/10821112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!