1. 程式人生 > >Docker 掛載Mysql用volume方式 docker-compose 登陸不進去

Docker 掛載Mysql用volume方式 docker-compose 登陸不進去

CI 刪除 exec mage 刪除mysql command red roo ext

信息:

  • Docker版本($ docker --version):Docker版本18.03.1-ce,版本9ee9f40
  • 系統信息:Windows10專業版

mysql掛載在Docker的volume中

1.第一步:

1 docker volume create mysql-data

2.第二步:

創建一個mysql文件夾

3.第三步:

再mysql文件夾下創建docker-compose.yml

因最新版docker已不支持/g/的絕對路徑表達方式,改為./的相對路徑【以docker-compose.yml的相對路徑】)

 1 version: 3
 2 services:
 3
db: 4 image: mysql/mysql-server 5 container_name: db-mysql 6 restart: always 7 command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci 8 ports: 9 - "3306:3306" 10 networks: 11 - net_db 12 environment: 13 MYSQL_ROOT_PASSWORD: pwd123456
14 volumes: 15 - mysql-data:/var/lib/mysql 16 volumes: 17 mysql-data: 18 external: true 19 networks: 20 net_db: 21 external: true

4.第四步:

docker-compose up

5.第五步:

登陸: 1 docker exec -it db-mysql bash 2 3 mysql -uroot -p 4 pwd123456

如果出現

Access denied for user root@localhost (using
password : YES)

請繼續看:

刪除鏡像:

列出所有images: 
docker images
docker rmi   IMAGE ID

刪除volume

1  列出所有volume: 
2 docker volume ls

docker volume rm mysql-data

刪除mysql文件夾

重復上面的操作把第四不的命令加上db

1 docker-compose up db

 1 C:\Users\GYW>docker exec -it db-mysql bash
 2 bash-4.2# mysql -uroot -p
 3 Enter password:
 4 Welcome to the MySQL monitor.  Commands end with ; or \g.
 5 Your MySQL connection id is 18
 6 Server version: 8.0.11 MySQL Community Server - GPL
 7 
 8 Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
 9 
10 Oracle is a registered trademark of Oracle Corporation and/or its
11 affiliates. Other names may be trademarks of their respective
12 owners.
13 
14 Type help; or \h for help. Type \c to clear the current input statement.
15 
16 mysql>

大功告成!!!

Docker 掛載Mysql用volume方式 docker-compose 登陸不進去