Docker部署MySQL 5.7&8.0主从集群

什么是MySQL集群?MySQL集群是一个无共享的(shared-nothing)、分布式节点架构的存储方案,其目的是提供容错性和高性能。

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

镜像版本 5.7

1、创建 overlay 网络

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

2、编辑两个配置文件 master.cnf 与 slave.cnf

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

[mysqld]
log-bin=mysql-bin
server-id=1
gtid-mode=ON
enforce-gtid-consistency=ON
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

[mysqld]
server-id=2
gtid-mode=ON
enforce-gtid-consistency=ON

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

docker run -d \
--name mysql-master \
--network common-network \
-e MYSQL_ROOT_PASSWORD=Passw0rd \
-v `pwd`/master.cnf:/etc/mysql/my.cnf \
-p 3306:3306 \
-d mysql:5.7
docker run -d \
--name mysql-slave \
--network common-network \
-e MYSQL_ROOT_PASSWORD=Passw0rd \
-v `pwd`/slave.cnf:/etc/mysql/my.cnf \
-p 3307:3306 \
-d mysql:5.7

4、添加从库用于复制的用户

docker run -it --rm --network common-network mysql mysql -hmysql-master -uroot -pPassw0rd \
-e "CREATE USER 'repl'@'%' IDENTIFIED BY 'password' REQUIRE SSL; " \
-e "GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';"

5、连接 master & slave

docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -pPassw0rd \
-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 -pPassw0rd -e "show slave status\G"

如下状态为正常:

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

> 部署 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=Passw0rd \
-p 3306:3306 \
-d mysql --default-authentication-plugin=mysql_native_password
docker run -d \
--name mysql-slave \
--network common-network \
-e MYSQL_ROOT_PASSWORD=Passw0rd \
-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 -pPassw0rd \
-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 -pPassw0rd \
-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 -pPassw0rd \
-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 -pPassw0rd -e "show slave status\G"

如下状态为正常:

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

> 验证数据同步

在 master 创建数据库 anoyi

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

在 slave 查看数据库列表

docker run -it --rm --network common-network mysql mysql -hmysql-slave -uroot -pPassw0rd \
 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database      |
+--------------------+
| anoyi       |
| information_schema |
| mysql       |
| performance_schema |
| sys        |
+--------------------+

相关资料:

mysql 5.7: https://dev.mysql.com/doc/refman/5.7/en/replication.html

mysql 8.0: https://dev.mysql.com/doc/refman/8.0/en/replication.html

赞(2)
声明:本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-62778877-8306;邮箱:fanjiao@west.cn。本站原创内容未经允许不得转载,或转载时需注明出处:西部数码知识库 » Docker部署MySQL 5.7&8.0主从集群

登录

找回密码

注册