Docker部署MySQL 8的MGR组复制

一键部署脚本

# 拉取MySQL镜像
# docker pull mysql/mysql-server:8.0.16

# 创建专门的网络,连接各个容器
$ docker network create mysql_mgr
$ docker network ls

# 创建容器
for N in 1 2 3
do docker run -d --name=node$N --net=mysql_mgr --hostname=node$N \
  -v $PWD/d$N:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mypass \
  mysql/mysql-server:8.0.16 \
  --server-id=$N \
  --log-bin='mysql-bin-1.log' \
  --enforce-gtid-consistency='ON' \
  --log-slave-updates='ON' \
  --gtid-mode='ON' \
  --transaction-write-set-extraction='XXHASH64' \
  --binlog-checksum='NONE' \
  --master-info-repository='TABLE' \
  --relay-log-info-repository='TABLE' \
  --plugin-load='group_replication.so' \
  --relay-log-recovery='ON' \
  --group-replication-start-on-boot='OFF' \
  --group-replication-group-name='aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' \
  --group-replication-local-address="node$N:33061" \
  --group-replication-group-seeds='node1:33061,node2:33061,node3:33061' \
  --loose-group-replication-single-primary-mode='OFF' \    # 如果单主模式,则设置为ON
  --loose-group-replication-enforce-update-everywhere-checks='ON'  # 如果单主模式,则设置为OFF
done

# 配置首节点,如:创建用户,开启组复制等
docker exec -it node1 mysql -uroot -pmypass \
  -e "SET @@GLOBAL.group_replication_bootstrap_group=1;" \
  -e "create user 'repl'@'%' identified by 'P@ssword1!';" \
  -e "GRANT REPLICATION SLAVE ON *.* TO repl@'%';" \
  -e "flush privileges;" \
  -e "change master to master_user='repl', master_password='P@ssword1!' 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;"

# 新增其他节点
for N in 2 3
do docker exec -it node$N mysql -uroot -pmypass \
  -e "change master to master_user='repl', master_password='P@ssword1!'  for channel 'group_replication_recovery';" \
  -e "START GROUP_REPLICATION;"
done

# 验证集群是否正常
docker exec -it node1 mysql -uroot -pmypass \
  -e "SELECT * FROM performance_schema.replication_group_members;"

你可能感兴趣的:(MySQL,Docker)