Docker环境下部署MySQL MGR单主模式集群

文章目录

  • 一、MySQL MGR单主模式集群部署
    • 1.拉取镜像
    • 2.创建MySQL专用网络
    • 3.创建MySQL容器挂载在本地的存储数据的目录
    • 4.创建3个MySQL容器
    • 5.在本地修改MySQL的配置项
    • 6.重启MySQL容器
    • 7.在所有MySQL容器中安装MGR插件
    • 8.在所有节点上创建复制账号
    • 9.启动MGR单主模式
  • 二、MySQL MGR单主模式集群中添加节点
    • 1.创建新节点在本地挂载的存储数据的目录
    • 2.创建1个MySQL容器
    • 3.修改新增MySQL容器的配置项
    • 4.重启新增MySQL容器
    • 5.在新增MySQL容器上安装MGR插件并验证安装是否完成
    • 6.在新增MySQL容器中设置复制账号
    • 7.修改集群中原来3个节点的配置信息
    • 8.在新增节点上启动复制
    • 9.查看集群中的节点信息
  • 三、补充
    • 1.进入MySQL容器
    • 2.查看MySQL容器日志

一、MySQL MGR单主模式集群部署

1.拉取镜像

docker pull mysql:8.0.20

2.创建MySQL专用网络

docker network create --subnet=172.72.0.0/24 mysql-network
//查看创建好的网络
docker network ls
NETWORK ID     NAME            DRIVER    SCOPE
9315b794232d   bridge          bridge    local
c349f69981fc   host            host      local
7d4d632f293f   mysql-network   bridge    local
630ce37f9903   none            null      local

3.创建MySQL容器挂载在本地的存储数据的目录

mkdir -p /xxx/docker/mysql01/conf.d
mkdir -p /xxx/docker/mysql01/data
mkdir -p /xxx/docker/mysql02/conf.d
mkdir -p /xxx/docker/mysql02/data
mkdir -p /xxx/docker/mysql03/conf.d
mkdir -p /xxx/docker/mysql03/data

4.创建3个MySQL容器

在运行以下命令时注意挂载路径需要与步骤三中创建的路径保持一致

docker run -d --name mysql8020mgr33065 \
   -h mgr15 -p 33065:3306 --net=mysql-network --ip 172.72.0.15 \
   -v /xxx/docker/mysql01/conf.d:/etc/mysql/conf.d -v /xxx/docker/mysql01/data:/var/lib/mysql/ \
   -e MYSQL_ROOT_PASSWORD=sunxi \
   -e TZ=Asia/Shanghai \
   mysql:8.0.20

docker run -d --name mysql8020mgr33066 \
   -h mgr16 -p 33066:3306 --net=mysql-network --ip 172.72.0.16 \
   -v /xxx/docker/mysql02/conf.d:/etc/mysql/conf.d -v /xxx/docker/mysql02/data:/var/lib/mysql/ \
   -e MYSQL_ROOT_PASSWORD=sunxi \
   -e TZ=Asia/Shanghai \
   mysql:8.0.20

docker run -d --name mysql8020mgr33067 \
   -h mgr17 -p 33067:3306 --net=mysql-network --ip 172.72.0.17 \
   -v /xxx/docker/mysql03/conf.d:/etc/mysql/conf.d -v /xxx/docker/mysql03/data:/var/lib/mysql/ \
   -e MYSQL_ROOT_PASSWORD=sunxi \
   -e TZ=Asia/Shanghai \
   mysql:8.0.20

5.在本地修改MySQL的配置项

cat > /xxx/docker/mysql01/conf.d/my.cnf <<"EOF"
[mysqld]
user=mysql
port=3306
character_set_server=utf8mb4
secure_file_priv=''
server-id = 802033065
default-time-zone = '+8:00'
log_timestamps = SYSTEM
log-bin = 
binlog_format=row
binlog_checksum=NONE
log-slave-updates=1
skip-name-resolve
auto-increment-increment=2
auto-increment-offset=1
gtid-mode=ON
enforce-gtid-consistency=on
default_authentication_plugin=mysql_native_password
max_allowed_packet = 500M

master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log=mgr15-relay-bin-ip15


transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=OFF
loose-group_replication_local_address= "172.72.0.15:33061"
loose-group_replication_group_seeds= "172.72.0.15:33061,172.72.0.16:33062,172.72.0.17:33063"
loose-group_replication_bootstrap_group=OFF
loose-group_replication_ip_whitelist="172.72.0.15,172.72.0.16,172.72.0.17"

report_host=172.72.0.15
report_port=3306

EOF
cat >  /xxx/docker/mysql02/conf.d/my.cnf <<"EOF"
[mysqld]
user=mysql
port=3306
character_set_server=utf8mb4
secure_file_priv=''
server-id = 802033066
default-time-zone = '+8:00'
log_timestamps = SYSTEM
log-bin = 
binlog_format=row
binlog_checksum=NONE
log-slave-updates=1
gtid-mode=ON
enforce-gtid-consistency=ON
skip_name_resolve
default_authentication_plugin=mysql_native_password
max_allowed_packet = 500M

master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log=mgr16-relay-bin-ip16


transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=OFF
loose-group_replication_local_address= "172.72.0.16:33062"
loose-group_replication_group_seeds= "172.72.0.15:33061,172.72.0.16:33062,172.72.0.17:33063"
loose-group_replication_bootstrap_group=OFF
loose-group_replication_ip_whitelist="172.72.0.15,172.72.0.16,172.72.0.17"

report_host=172.72.0.16
report_port=3306

EOF
cat > /xxx/docker/mysql03/conf.d/my.cnf <<"EOF"
[mysqld]
user=mysql
port=3306
character_set_server=utf8mb4
secure_file_priv=''
server-id = 802033067
default-time-zone = '+8:00'
log_timestamps = SYSTEM
log-bin = 
binlog_format=row
binlog_checksum=NONE
log-slave-updates=1
gtid-mode=ON
enforce-gtid-consistency=ON
skip_name_resolve
default_authentication_plugin=mysql_native_password
max_allowed_packet = 500M


master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log=mgr17-relay-bin-ip17


transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=OFF
loose-group_replication_local_address= "172.72.0.17:33063"
loose-group_replication_group_seeds= "172.72.0.15:33061,172.72.0.16:33062,172.72.0.17:33063"
loose-group_replication_bootstrap_group=OFF
loose-group_replication_ip_whitelist="172.72.0.15,172.72.0.16,172.72.0.17"

report_host=172.72.0.17
report_port=3306

EOF

6.重启MySQL容器

docker restart mysql8020mgr33065 mysql8020mgr33066 mysql8020mgr33067

7.在所有MySQL容器中安装MGR插件

//xxx是容器的名称
docker exec -it xxx mysql -uroot -psunxi
//安装MGR插件
INSTALL PLUGIN group_replication SONAME 'group_replication.so';

验证插件是否安装完成:

show plugins;

在这里插入图片描述

8.在所有节点上创建复制账号

mysql> SET SQL_LOG_BIN=0;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE USER repl@'%' IDENTIFIED BY 'sunxi';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> SET SQL_LOG_BIN=1;
Query OK, 0 rows affected (0.00 sec)

mysql> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='sunxi' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.08 sec)

9.启动MGR单主模式

在主节点上启动MGR:

SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=OFF;
SELECT * FROM performance_schema.replication_group_members;

剩余节点需要加入MGR,在剩余节点上执行以下操作:

START GROUP_REPLICATION;
SELECT * FROM performance_schema.replication_group_members;

注意:正常的单主模式MGR集群中的所有节点的状态是ONLINE,并且MEMBER_ROLE中有一个是PRIMARY,其余的是SECONDARY。
这里在从节点上执行START GROUP_REPLICATION;命令时,节点日志会报以下错误信息:

[ERROR] [MY-011526] [Repl] Plugin group_replication reported: 'This member has more executed transactions than those present in the group. Local transactions: 5da99189-0c8e-11ec-a1f4-0242ac480011:1-5 > Group transactions: 4be24ffe-0c8e-11ec-8495-0242ac48000f:1-5,
aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1-5'

解决方法:在从节点上先执行reset master;然后再执行START GROUP_REPLICATION;


二、MySQL MGR单主模式集群中添加节点

1.创建新节点在本地挂载的存储数据的目录

mkdir -p /xxx/docker/mysql04/conf.d
mkdir -p /xxx/docker/mysql04/data

2.创建1个MySQL容器

docker run -d --name mysql8020mgr33068 \
  -h mgr18 -p 33068:3306 --net=mysql-network --ip 172.72.0.18 \
  -v /xxx/docker/mysql04/conf.d:/etc/mysql/conf.d -v /xxx/docker/mysql04/data:/var/lib/mysql/ \
  -e MYSQL_ROOT_PASSWORD=sunxi \
  -e TZ=Asia/Shanghai \
  mysql:8.0.20

3.修改新增MySQL容器的配置项

cat > /xxx/docker/mysql04/conf.d/my.cnf <<"EOF"
[mysqld]
user=mysql
port=3306
character_set_server=utf8mb4
secure_file_priv=''
server-id = 802033068
log-bin = 
binlog_format=row
binlog_checksum=NONE
log-slave-updates=1
skip-name-resolve
auto-increment-increment=2
auto-increment-offset=1
gtid-mode=ON
enforce-gtid-consistency=on
default_authentication_plugin=mysql_native_password
max_allowed_packet = 500M
log_slave_updates=on

master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log=mgr18-relay-bin-ip18

transaction_write_set_extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=OFF
loose-group_replication_local_address= "172.72.0.18:33064"
loose-group_replication_group_seeds= "172.72.0.15:33061,172.72.0.16:33062,172.72.0.17:33063,172.72.0.18:33064"
loose-group_replication_bootstrap_group=OFF
loose-group_replication_ip_whitelist="172.72.0.15,172.72.0.16,172.72.0.17,172.72.0.18"
report_host=172.72.0.18
report_port=3306

EOF

4.重启新增MySQL容器

docker restart mysql8020mgr33068

5.在新增MySQL容器上安装MGR插件并验证安装是否完成

docker exec -it mysql8020mgr33068 mysql -uroot -psunxi
INSTALL PLUGIN group_replication SONAME 'group_replication.so';
show plugins;

6.在新增MySQL容器中设置复制账号

SET SQL_LOG_BIN=0;
CREATE USER repl@'%' IDENTIFIED BY 'sunxi';
GRANT REPLICATION SLAVE ON *.* TO repl@'%';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='sunxi' FOR CHANNEL 'group_replication_recovery';

7.修改集群中原来3个节点的配置信息

这一步修改集群中原有节点的配置信息主要是包含以下两个配置项:

  • group_replication_group_seeds
  • group_replication_ip_whitelist

在修改配置信息时可以进行动态修改:

set global group_replication_group_seeds='172.72.0.15:33061,172.72.0.16:33062,172.72.0.17:33063,172.72.0.18:33064';
stop group_replication;
set global group_replication_ip_whitelist="172.72.0.15,172.72.0.16,172.72.0.17,172.72.0.18";
start group_replication;

动态修改配置信息后,当集群进行重启时之前进行动态配置的配置项不会起作用,所以可以对其配置文件直接修改,在配置文件中修改以下两个配置项:

loose-group_replication_group_seeds= "172.72.0.15:33061,172.72.0.16:33062,172.72.0.17:33063,172.72.0.18:33064"
loose-group_replication_ip_whitelist="172.72.0.15,172.72.0.16,172.72.0.17,172.72.0.18"

重启MySQL服务:

docker restart xxx

启动复制:

//xxx是容器的名称
docker exec -it xxx mysql -uroot -psunxi
START GROUP_REPLICATION;
SELECT * FROM performance_schema.replication_group_members;

8.在新增节点上启动复制

start group_replication;

在执行这一步时新增节点的日志中会出现以下报错日志:

[ERROR] [MY-011526] [Repl] Plugin group_replication reported: 'This member has more executed transactions than those present in the group. Local transactions: 5cdef463-0ebc-11ec-9c11-0242ac480012:1-5 > Group transactions: 4be24ffe-0c8e-11ec-8495-0242ac48000f:1-5,
aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1-10'

解决方法:在该节点上执行reset master;后再次执行START GROUP_REPLICATION;

9.查看集群中的节点信息

SELECT * FROM performance_schema.replication_group_members;

三、补充

1.进入MySQL容器

//xxx为容器名称
docker exec -it xxx bash
docker exec -it xxx mysql -uroot -psunxi

2.查看MySQL容器日志

//xxx为容器名称
docker logs xxx

你可能感兴趣的:(docker,mysql,运维)