参考文章:
docker-compose mysql5.7.30 主从
docker compose 安装 与 Mysql 安装
使用Docker-compose部署MySQL测试环境
通过国内网易镜像源下载
[root@yan ~]# docker pull hub.c.163.com/library/mysql:5.7
...省略
[root@yan ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hub.c.163.com/library/mysql 5.7 9e64176cd8a2 3 years ago 407MB
[root@yan ~]#
[root@yan ~]# docker tag hub.c.163.com/library/mysql:5.7 mysql:5.7 # 通过源镜像创建新镜像
[root@yan ~]#
[root@yan ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 9e64176cd8a2 3 years ago 407MB
hub.c.163.com/library/mysql 5.7 9e64176cd8a2 3 years ago 407MB
[root@yan ~]#
[root@yan ~]# docker rmi hub.c.163.com/library/mysql:5.7 # 删除源镜像版本
Untagged: hub.c.163.com/library/mysql:5.7
Untagged: hub.c.163.com/library/mysql@sha256:10b4a255e03cce2eb12adfede34c99d86cc0e4d66f515f6bec92eb8c8c12bfef
[root@yan ~]#
[root@yan ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 9e64176cd8a2 3 years ago 407MB
[root@yan ~]#
在 /home/docker/mysql 目录编写 docker-compose.yml 文件
external, but could not be found. Please create the network manually using docker network create mysql-net
and try again.
[root@yan mysql]#
[root@yan mysql]#
[root@yan mysql]#
[root@yan mysql]# vi docker-compose.yml
version: "3"
services:
mysql:
image: mysql:5.7
container_name: mysql
restart: always
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: "root"
MYSQL_USER: 'root'
MYSQL_PASS: 'qq1122'
command:
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
--max_allowed_packet=128M
--sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO"
ports:
- 3306:3306
volumes:
- /usr/local/mysql/data:/var/lib/mysql
- /usr/local/mysql/mysql.cnf:/etc/mysql/mysql.cnf
- /usr/local/mysql/conf.d:/etc/mysql/conf.d
- /usr/local/mysql/mysql.conf.d:/etc/mysql/mysql.conf.d
networks:
- mysql-net
networks:
mysql-net:
external: true
docker-compose up 启动, 需要创建手动创建网络
[root@yan mysql]# docker-compose up
ERROR: Network mysql-net declared as external, but could not be found. Please create the network manually using `docker network create mysql-net` and try again.
[root@yan mysql]#
[root@yan mysql]# docker network create mysql-net
397f6f37d99bc885a740b439e8dfaca096a6433082d59e0cc93c54be1520b209
[root@yan mysql]#
启动 docker-compose.yml 前必须把数据卷中的配置从镜像文件中拷贝过来。
通过 docker run 启动 mysql ,通过 navicat 测试连接。普通用户名:flying 普通密码:qq1122 root用户名:root root密码:root
docker run -e MYSQL_DATABASE="flying" -e MYSQL_USER="flying" -e MYSQL_PASSWORD="qq1122" -e MYSQL_ROOT_PASSWORD="root" -d --name mysql -p 3306:3306 mysql:5.7
拷贝配置文件:
....省略
volumes:
- /usr/local/mysql/data:/var/lib/mysql
- /usr/local/mysql/mysql.cnf:/etc/mysql/mysql.cnf
- /usr/local/mysql/conf.d:/etc/mysql/conf.d
- /usr/local/mysql/mysql.conf.d:/etc/mysql/mysql.conf.d
networks:
- mysql-net
networks:
mysql-net:
external: true
[root@yan ~]# cd /usr/local/mysql/
[root@yan mysql]#
[root@yan mysql]# pwd
/usr/local/mysql
[root@yan mysql]# docker cp mysql:/var/lib/mysql /usr/local/mysql
[root@yan mysql]# mv mysql data # 重命名文件夹
[root@yan mysql]# docker cp mysql:/etc/mysql/mysql.cnf /usr/local/mysql/
[root@yan mysql]# docker cp mysql:/etc/mysql/conf.d /usr/local/mysql
[root@yan mysql]# docker cp mysql:/etc/mysql/mysql.conf.d /usr/local/mysql
[root@yan mysql]# ll
总用量 8
drwxr-xr-x. 2 root root 41 4月 25 2017 conf.d
drwxr-xr-x. 6 polkitd input 4096 9月 30 03:42 data
-rw-r--r--. 1 root root 796 3月 18 2017 mysql.cnf
drwxr-xr-x. 2 root root 24 4月 25 2017 mysql.conf.d
[root@yan mysql]#
my.cnf -> /etc/alternatives/my.cnf报错------参考文章
docker-compose up
。。。省略
2020-09-29T19:48:39.079264Z 0 [Note] mysqld: ready for connections.
Version: '5.7.18' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
2020-09-29T19:48:39.079343Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check.
2020-09-29T19:48:39.079354Z 0 [Note] Beginning of list of non-natively partitioned tables
2020-09-29T19:48:39.100140Z 0 [Note] End of list of non-natively partitioned tables
[root@yan mysql]
完毕