mysql8.0 MGR集群,添加节点,删除节点(迁移节点)

本文是多主模式(在线操作)

一、环境

CentOS 7.6 or 7.9

mysql 8.0.32

IP 机器名 端口 集群端口 备注
10.10.10.45 test2 3306 33061 引导节点,主节点
10.10.10.51 test3 3306 33061 主节点
10.10.10.52 test4 3306 33061 主节点,(待迁移到70)
10.10.10.70 test5 3306 33061 新增节点 (待52迁移到此)

二、准备工作

1、防火墙开通端口

firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --permanent --zone=public --add-port=33061/tcp
firewall-cmd --reload

2、设置主机名和IP的映射,设置时区

主机名,集群查找节点时需要(如果不设置主机名,可尝试配置report_host,report_port)

[root@localhost ~]# hostname
localhost
[root@localhost ~]# hostname -i
10.10.10.70
[root@localhost ~]# vi /etc/hosts
10.10.10.45 test2
10.10.10.51 test3
10.10.10.52 test4
10.10.10.70 test5
[root@localhost ~]# hostnamectl set-hostname test5
[root@localhost ~]# timedatectl
[root@localhost ~]# timedatectl set-timezone Asia/Shanghai

原有所有节点添加新节点的机器名和IP

10.10.10.45 test2
10.10.10.51 test3
10.10.10.52 test4
10.10.10.70 test5

三、新节点my.cnf 添加集群配置

主要修改server_id,group_replication_local_address,group_replication_group_seeds

server_id=70
gtid_mode=ON
enforce_gtid_consistency=ON

binlog_checksum=NONE

log_bin=binlog
log_slave_updates=ON
binlog_format=ROW
master_info_repository=TABLE
relay_log_info_repository=TABLE
transaction_write_set_extraction=XXHASH64

plugin_load_add='group_replication.so'
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address= "10.10.10.70:33061"
group_replication_group_seeds= "10.10.10.45:33061,10.10.10.51:33061,10.10.10.52:33061,10.10.10.70:33061"
group_replication_bootstrap_group=off

group_replication_recovery_get_public_key=ON
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON

四、重启新节点

systemctl stop mysqld80
systemctl start mysqld80

五、原来的所有节点添加新节点

修改my.cnf

group_replication_group_seeds='10.10.10.45:33061,10.10.10.51:33061,10.10.10.52:33061,10.10.10.70:33061'

不用重启mysql

在线修改变量

/opt/mysql-8.0.32/bin/mysql -h 127.0.0.1 -u root -p
mysql> set global group_replication_group_seeds='10.10.10.45:33061,10.10.10.51:33061,10.10.10.52:33061,10.10.10.70:33061';

六、新节点配置复制


18.2.1.3 User Credentials For Distributed Recovery

这步可尝试不做(用户会复制过来)

mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER replicator@'%' IDENTIFIED BY 'replicator8';
mysql> GRANT REPLICATION SLAVE ON *.* TO replicator@'%';
mysql> GRANT CONNECTION_ADMIN ON *.* TO replicator@'%';
mysql> GRANT BACKUP_ADMIN ON *.* TO replicator@'%';
mysql> GRANT GROUP_REPLICATION_STREAM ON *.* TO replicator@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;

# 创建复制用户后,必须向服务器提供用于分布式恢复的用户凭据。

mysql> CHANGE MASTER TO MASTER_USER='replicator', MASTER_PASSWORD='replicator8' FOR CHANNEL 'group_replication_recovery';
Or from MySQL 8.0.23:
mysql> CHANGE REPLICATION SOURCE TO SOURCE_USER='replicator', SOURCE_PASSWORD='replicator8' FOR CHANNEL 'group_replication_recovery';

七、新加节点启动组复制

mysql> START GROUP_REPLICATION;
--查看组复制成员状态
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION | MEMBER_COMMUNICATION_STACK |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
| group_replication_applier | 2f098a17-b99f-11ed-bc03-0050569904bb | test2       |        3306 | ONLINE       | PRIMARY     | 8.0.32         | XCom                       |
| group_replication_applier | 376ecef2-b99f-11ed-8df9-00505699c130 | test3       |        3306 | ONLINE       | PRIMARY     | 8.0.32         | XCom                       |
| group_replication_applier | 4265e768-b99f-11ed-9e71-005056999453 | test4       |        3306 | ONLINE       | PRIMARY     | 8.0.32         | XCom                       |
| group_replication_applier | 80292762-e425-11ed-a55b-005056992ee3 | test5       |        3306 | ONLINE       | PRIMARY     | 8.0.32         | XCom                       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+-------------+----------------+----------------------------+
4 rows in set (0.00 sec)

八、查看日志

查看日志:tail -f /opt/mysql-8.0.32/data/mysqld.log

九、删除节点

1、在待删除节点上停止组复制:
10.10.10.52,停止后会从集群组成员中移除
mysql> STOP GROUP_REPLICATION;
#mysql> show master status\G
#mysql> RESET MASTER;
#mysql> show master status\G
(mysql> show slave status\G)

在其他节点查询
mysql> SELECT * FROM performance_schema.replication_group_members;

2、其他节点修改配置,删除待删除节点信息
my.cnf 不用重启mysql,下次重启生效
group_replication_group_seeds='10.10.10.45:33061,10.10.10.51:33061,10.10.10.70:33061'

在线修改变量
mysql> set global group_replication_group_seeds='10.10.10.45:33061,10.10.10.51:33061,10.10.10.70:33061';

3、在待删除节点清理相关配置
10.10.10.52
my.cnf修改或注释,稍后重启mysql服务
group_replication_start_on_boot=off
group_replication_local_address= ""
group_replication_group_seeds= ""

在线修改变量
mysql> set global group_replication_start_on_boot=off;
mysql> set global group_replication_local_address='';
mysql> set global group_replication_group_seeds='';

mysql> SHOW GLOBAL VARIABLES LIKE 'group_replication_start_on_boot';

4、在待删除节点删除复制账号,删除组复制插件(可选操作)
mysql> set global super_read_only=0;
mysql> set sql_log_bin=0;
mysql> drop user replicator@'%';
mysql> uninstall plugin group_replication;
mysql> set sql_log_bin=1;
mysql> set global super_read_only=1;
mysql> show plugins;

my.cnf 注释掉(或删除)所有和集群相关的配置

5、在待删除节点重启mysql(可选操作)
systemctl stop mysqld80
systemctl start mysqld80

6、在待删除节点删除业务数据库(可选操作)

7、在待删除节点删除防火墙端口(可选操作)
firewall-cmd --permanent --zone=public --remove-port=3306/tcp
firewall-cmd --permanent --zone=public --remove-port=33061/tcp
firewall-cmd --reload

8、其他节点/etc/hosts文件清理(可选操作)

十、参考

Mysql MGR集群增加新的节点

MGR新增节点和删除节点

利用GreatSQL部署部署MGR集群,并完成添加新节点

十一、报错处理

 1、从节点有复制用户,复制时创建复制用户报错

[ERROR] [MY-010584] [Repl] Slave SQL for channel 'group_replication_recovery': Worker 1 failed executing transaction '2ca72062-b99f-11ed-a000-000c29b604d9:1' at master log binlog.000002, end_log_pos 464; Error 'Operation CREATE USER failed for 'replicator'@'%'' on query. Default database: ''. Query: 'CREATE USER 'replicator'@'%' IDENTIFIED WITH 'caching_sha2_password' AS '$A$005$Ju_\n~0hQngc`c/UslQGjU0HxgL15VsMk5QzMhdyBoh6ynOy577ryRV5g/'', Error_code: MY-001396
[ERROR] [MY-010586] [Repl] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'binlog.000002' position 153

个人的操作,删除从节点复制用户

mysql> STOP GROUP_REPLICATION;
Query OK, 0 rows affected (3.22 sec)

mysql> set global super_read_only=0;
Query OK, 0 rows affected (0.00 sec)

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

mysql> drop user replicator@'%';
Query OK, 0 rows affected (0.02 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> set global super_read_only=1;
Query OK, 0 rows affected (0.00 sec)

mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (4.49 sec)

2、从节点有数据库,复制时创建复制数据库报错

[ERROR] [MY-010584] [Repl] Slave SQL for channel 'group_replication_recovery': Worker 1 failed executing transaction 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:55' at master log binlog.000004, end_log_pos 1725; Error 'Can't create database 'testdb'; database exists' on query. Default database: 'testdb'. Query: 'CREATE DATABASE `testdb`', Error_code: MY-001007

个人的操作,删除从节点的数据库

mysql> STOP GROUP_REPLICATION;
Query OK, 0 rows affected (3.22 sec)

mysql> set global super_read_only=0;
Query OK, 0 rows affected (0.00 sec)

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

mysql> drop database testdb;
Query OK, 293 rows affected (3.49 sec)

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

mysql> set global super_read_only=1;
Query OK, 0 rows affected (0.00 sec)

mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (4.49 sec)

你可能感兴趣的:(mysql,mysql,MGR集群,添加加点,删除节点,迁移节点)