Setting up MySQL Group replication with Docker Compose
The below steps show how to setup MySQL Group Replication with Docker Compose using mysql/mysql-server:8.0 Docker images.
1
|
git clone git@github.com:wagnerjfr/mysql-group-replication-docker-compose.git
|
如何启动 docker-compose
注意
前置条件
-
只能为 innodb 引擎。
-
所有表必须有主键。
查看哪些表没有主键
1
|
select table_schema,table_name from information_schema.tables where (table_schema,table_name) not in( select distinct table_schema,table_name from information_schema.columns where COLUMN_KEY='PRI' ) and table_schema not in ('sys','mysql','information_schema','performance_schema');
|
为没有主键的表添加主键
1
|
ALTER TABLE db1.table1 ADD COLUMN `id` varchar(240) NOT NULL FIRST , ADD PRIMARY KEY (`id`);
|
启动后查看到 mysql 的集群为健康状态。
到 docker 中执行 sql
1
2
3
4
5
6
7
8
9
|
docker exec -it node1 mysql -uroot -pmypass \
-e "SET @@GLOBAL.group_replication_bootstrap_group=1;" \
-e "create user 'repl'@'%';" \
-e "GRANT REPLICATION SLAVE ON *.* TO repl@'%';" \
-e "flush privileges;" \
-e "change master to master_user='repl' for channel 'group_replication_recovery';" \
-e "START GROUP_REPLICATION;" \
-e "SET @@GLOBAL.group_replication_bootstrap_group=0;" \
-e "SELECT * FROM performance_schema.replication_group_members;"
|
再到 node2 和 node3 上执行
1
2
3
4
5
|
for N in 2 3
do docker exec -it node$N mysql -uroot -pmypass \
-e "change master to master_user='repl' for channel 'group_replication_recovery';" \
-e "START GROUP_REPLICATION;"
done
|
执行监控命令看一下,是否正常执行
1
2
|
docker exec -it node1 mysql -uroot -pmypass \
-e "SELECT * FROM performance_schema.replication_group_members;"
|
从上图可以看到是单主模式。
添加数据
1
2
|
docker exec -it node1 mysql -uroot -pmypass \
-e "create database TEST; use TEST; CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY) ENGINE=InnoDB; show tables;"
|
去 node2 和 node3 上查看一下是否成功
1
2
3
4
|
for N in 2 3
do docker exec -it node$N mysql -uroot -pmypass \
-e "INSERT INTO TEST.t1 VALUES($N);"
done
|
执行报错,发现是单主模式:
解下改成多主模式
多主模式下,在执行一下上面的操作即可。
需要等到都是 online 才可以
接下来再测试数据
1
2
3
4
|
for N in 2 3
do docker exec -it node$N mysql -uroot -pmypass \
-e "INSERT INTO TEST.t1 VALUES($N);"
done
|
接下来查看每个节点的数据是否一致
1
2
3
4
5
|
for N in 1 2 3
do docker exec -it node$N mysql -uroot -pmypass \
-e "SHOW VARIABLES WHERE Variable_name = 'hostname';" \
-e "SELECT * FROM TEST.t1;"
done
|
所有节点上的数据是一致的。
接下来容灾测试 GR 失败处理
修改 node3 为 readonly
1
2
|
docker exec -it node3 mysql -uroot -pmypass \
-e "set @@global.group_replication_exit_state_action=READ_ONLY;"
|
断开网络
1
|
docker network disconnect mysql-group-replication-docker-compose_default node3
|
测试一下 memebr
1
2
3
4
5
|
for N in 1 3
do docker exec -it node$N mysql -uroot -pmypass \
-e "SHOW VARIABLES WHERE Variable_name = 'hostname';" \
-e "SELECT * FROM performance_schema.replication_group_members;"
done
|
重新建立连接关系
1
|
docker network connect mysql-group-replication-docker-compose_default node3
|
重新加入到集群
1
2
|
docker exec -it node3 mysql -uroot -pmypass \
-e "STOP GROUP_REPLICATION; START GROUP_REPLICATION;"
|
在检查一下节点情况
1
2
3
4
5
|
for N in 1 3
do docker exec -it node$N mysql -uroot -pmypass \
-e "SHOW VARIABLES WHERE Variable_name = 'hostname';" \
-e "SELECT * FROM performance_schema.replication_group_members;"
done
|
官方文档的位置:
https://dev.mysql.com/doc/refman/8.0/en/group-replication.html
参考文档:
https://dev.mysql.com/blog-archive/setting-up-mysql-group-replication-with-mysql-docker-images/
参考文档:
https://bbotte.github.io/service_config/mysql-mgr/mysql_group_rplication_config