docker-compose內mysql 掛載數據庫目錄,啓動時mysql容器自動退出

2019-05-06php


 

問題現象mysql

使用docker-compose部署web+mysql架構網站,出現docker-compose up -d啓動以後,mysql容器自動退出現象nginx

docker-compose.ymlgit

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"

 解決方案:web

真正的問題所在其實就是在服務器上的volume目錄/mnt/mysql/data和容器裏目錄/var/lib/mysql/data擁有者不同致使的,那麼如何查看擁有者,須要使用以下幾條指令redis

查看容器中/var/lib/mysql的全部者sql

[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用戶組docker

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

也就是說,這兩個目錄的全部者不一樣致使的權限問題,如今把他們的id統一就能夠了,統一前要先查出來容器裏的mysql用戶組id,而後修改服務器的/mnt/mysql/data下的用戶組idcentos

查出來容器裏的mysql用戶組idbash

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

相關文章
相關標籤/搜索