Docker 部署 MySQL 8.0 主从集群

部署 MySQL 8.0 集群 master & slave(仅测试用)

镜像版本 mysql:8.0

1、创建 overlay 网络

docker network create --driver overlay common-network --attachable

2、启动 2 个 MYSQL:mysql-master 、mysql-slave

docker run -d \
--name mysql-master \
--network common-network \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3306:3306 \
-d mysql --default-authentication-plugin=mysql_native_password
docker run -d \
--name mysql-slave \
--network common-network \
-e MYSQL_ROOT_PASSWORD=123456 \
-p 3307:3306 \
-d mysql --default-authentication-plugin=mysql_native_password

3、配置 master & slave

docker run -it --rm --network common-network mysql mysql -hmysql-master -uroot -p123456 \
-e "SET PERSIST server_id=1;" \
-e "SET PERSIST_ONLY gtid_mode=ON;" \
-e "SET PERSIST_ONLY enforce_gtid_consistency=true; " \
-e "CREATE USER 'repl'@'%' IDENTIFIED BY 'password' REQUIRE SSL; " \
-e "GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';"
docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -p123456 \
-e "SET PERSIST server_id=2;" \
-e "SET PERSIST_ONLY gtid_mode=ON;" \
-e "SET PERSIST_ONLY enforce_gtid_consistency=true; "

4、重启 master & slave

docker restart mysql-master mysql-slave

5、连接 master & slave

docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -p123456 \
-e "CHANGE MASTER TO MASTER_HOST='mysql-master', MASTER_PORT=3306, MASTER_USER='repl', MASTER_PASSWORD='password', MASTER_AUTO_POSITION=1, MASTER_SSL=1;" \
-e "START SLAVE;" 

6、验证 slave 状态

docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -p123456 -e "show slave status\G"
如下状态为正常:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

验证数据同步
在 master 创建数据库 snail

docker run -it --rm --network common-network mysql mysql -hmysql-master -uroot -p123456 \
 -e "create database snail default character set utf8mb4 collate utf8mb4_general_ci;"

在 slave 查看数据库列表

docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -p123456 \
 -e "show databases;"

mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| snail |
| information_schema |
| mysql |
| performance_schema |
| sys |

参考

Docker 部署 MySQL 5.7 & 8.0 主从集群

posted @ 2020-12-18 15:21  snail灬  阅读(509)  评论(0编辑  收藏  举报