MySQL Group Replication(MGR)组复制支持单主模式和多主模式,本文简单介绍单主模式下,选举新主节点的不同方法。
- 在 8.0.2 版本之前,主节点选举是基于节点的 UUID 编号,在一次故障切换过程中,最小的 UUID 那个节点将被选举为新主节点。
- 从 8.0.2 版本开始,可以通过使用权重来选举新主节点,权重参数为 group_replication_member_weight,在当前主节点发生故障时,拥有高权重的备节点将被选举为新主节点。
- 从 8.0.12 版本开始,可以通过调用函数 group_replication_set_as_primary 来选举任一节点作为新的主节点,即使没有发生故障,也可以调用该函数来选举新的主节点。
1. 场景
有 3 个节点的 MGR 集群,版本为 Percona Server for MySQL 8.0.22,节点信息如下:
- 172.28.128.15
- 172.28.128.14
- 172.28.128.13
mysql> select member_host,member_state,member_role,member_version from performance_schema.replication_group_members;
+---------------+--------------+-------------+----------------+
| member_host | member_state | member_role | member_version |
+---------------+--------------+-------------+----------------+
| 172.28.128.15 | ONLINE | SECONDARY | 8.0.22 |
| 172.28.128.14 | ONLINE | PRIMARY | 8.0.22 |
| 172.28.128.13 | ONLINE | SECONDARY | 8.0.22 |
+---------------+--------------+-------------+----------------+
3 rows in set (0.03 sec)
当前主节点是 172.28.128.14,假设该节点由于操作系统或者硬件升级,需要停机维护,计划将 172.28.128.15 选举为新的主节点,通常有如下两种方式实现。
- 使用权重参数,group_replication_member_weight
- 使用函数,group_replication_set_as_primary
2. 通过权重选举新主节点
通过权重选举新主节点,这种方式并不直接了当,有点绕了弯路,默认条件下,所有节点的权重都是一样的,比如所有节点权重都为 50,如下:
[root@innodb1 ~]# mysql -e "select @@hostname, @@group_replication_member_weight\G"
*************************** 1. row ***************************
@@hostname: 172.28.128.13
@@group_replication_member_weight: 50
[root@innodb2 ~]# mysql -e "select @@hostname, @@group_replication_member_weight\G"
*************************** 1. row ***************************
@@hostname: 172.28.128.14
@@group_replication_member_weight: 50
[root@innodb3 ~]# mysql -e "select @@hostname, @@group_replication_member_weight\G"
*************************** 1. row ***************************
@@hostname: 172.28.128.15
@@group_replication_member_weight: 50
通过增加 172.28.128.15 的权重,以便 172.28.128.14 停机时,172.28.128.15 能够自动被选举为主节点。
在 172.28.128.15 上执行:
mysql> set global group_replication_member_weight = 70;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@group_replication_member_weight;
+-----------------------------------+
| @@group_replication_member_weight |
+-----------------------------------+
| 70 |
+-----------------------------------+
1 row in set (0.00 sec)
注意:
设置权重值,并不需要执行 STOP/START GROUP_REPLICATION。
当 172.28.128.15 的权重值增加到 70 后,下线 172.28.128.14 节点,在 172.28.128.14 上执行:
mysql> stop group_replication;
Query OK, 0 rows affected (4.29 sec)
mysql> select member_host,member_state,member_role,member_version from performance_schema.replication_group_members;
+---------------+--------------+-------------+----------------+
| member_host | member_state | member_role | member_version |
+---------------+--------------+-------------+----------------+
| 172.28.128.14 | OFFLINE | | |
+---------------+--------------+-------------+----------------+
1 row in set (0.01 sec)
执行完成后,172.28.128.14 节点从集群下线,新主节点被选举出来,如下:
mysql> select member_host,member_state,member_role,member_version from performance_schema.replication_group_members;
+---------------+--------------+-------------+----------------+
| member_host | member_state | member_role | member_version |
+---------------+--------------+-------------+----------------+
| 172.28.128.15 | ONLINE | PRIMARY | 8.0.22 |
| 172.28.128.13 | ONLINE | SECONDARY | 8.0.22 |
+---------------+--------------+-------------+----------------+
2 rows in set (0.04 sec)
可以看到,172.28.128.15 被选举为新的主节点。
3. 通过函数 group_replication_set_as_primary 选举新主节点
相对于使用权重选举新主节点,通过调用函数 group_replication_set_as_primary 实现主节点选举更加直接,便捷,并且不需要将当前主节点停机。
同样的环境进行测试,如下:
+---------------+--------------+-------------+----------------+
| member_host | member_state | member_role | member_version |
+---------------+--------------+-------------+----------------+
| 172.28.128.15 | ONLINE | SECONDARY | 8.0.22 |
| 172.28.128.14 | ONLINE | PRIMARY | 8.0.22 |
| 172.28.128.13 | ONLINE | SECONDARY | 8.0.22 |
+---------------+--------------+-------------+----------------+
3 rows in set (0.00 sec)
在 172.28.128.15 节点上执行函数,函数参数为新主节点的 UUID,如下:
mysql> show global variables like 'server_uu%';
+---------------+--------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------+
| server_uuid | c5aed435-d58d-11ea-bb26-5254004d77d3 |
+---------------+--------------------------------------+
1 row in set (0.00 sec)
mysql> select group_replication_set_as_primary('c5aed435-d58d-11ea-bb26-5254004d77d3');
+--------------------------------------------------------------------------+
| group_replication_set_as_primary('c5aed435-d58d-11ea-bb26-5254004d77d3') |
+--------------------------------------------------------------------------+
| Primary server switched to: c5aed435-d58d-11ea-bb26-5254004d77d3 |
+--------------------------------------------------------------------------+
1 row in set (1.03 sec)
mysql> select member_host,member_state,member_role,member_version from performance_schema.replication_group_members;
+---------------+--------------+-------------+----------------+
| member_host | member_state | member_role | member_version |
+---------------+--------------+-------------+----------------+
| 172.28.128.15 | ONLINE | PRIMARY | 8.0.22 |
| 172.28.128.14 | ONLINE | SECONDARY | 8.0.22 |
| 172.28.128.13 | ONLINE | SECONDARY | 8.0.22 |
+---------------+--------------+-------------+----------------+
3 rows in set (0.00 sec)
以上可以看到,172.28.128.15 被选举为新主节点,这种方式切换主节点非常方便,简单。
注意:
group_replication_set_as_primary_node 函数可以在任一节点执行,效果一样。
本文译自:
https://www.percona.com/blog/2021/01/11/mysql-group-replication-how-to-elect-the-new-primary-node/