时间:2021-07-01 10:21:17 帮助过:11人阅读
本博文介绍了Group Replication的两种工作模式的架构。
并详细介绍了Single-Master Mode的部署过程,以及如何切换到Multi-Master Mode。
当然,文末给出了Group Replication的配置要求和一些限制。
〇 结构介绍
在2016年12月发布的5.7.17版本的MySQL,甲骨文宣布Group Replication已经GA。
Group Replication(下简称GR)有两个工作模式,分别为Single-Master Mode与Multi-Master Mode:
Single-Master Mode:
只有primary成员可读写,而其他的节点为只读,在primary成员发生故障时,将会有其他成员顶替成primary。
Multi-Master Mode:
所有的成员均可读可写。
〇 部署
测试实例数量:3台
版本:MySQL 5.7.20
安装(此处用的是二进制包安装)
mysql-5.7.20-linux-glibc2.5-x86_64.tar.gz
创建数据目录及日志目录:
mkdir -p /data/mysql57/mysql3306
mkdir -p /data/mysql57/mysql3307
mkdir -p /data/mysql57/mysql3308
mkdir -p /data/mysql57/logs/3306
mkdir -p /data/mysql57/logs/3307
mkdir -p /data/mysql57/logs/3308
解压二进制包并将其设置为basedir:
上传二进制包到/usr/local/src
cd /usr/local/src
tar zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.20-linux-glibc2.12-x86_64 /usr/local/mysql
〇 添加第一台实例:
添加3306实例的配置文件:
vim /data/mysql57/3306.cnf
[client]
prompt = "(\u@\h) [\d]> "
[mysqld]
server_id = 3306
user = mysql
port = 3306
socket = /tmp/mysql3306.sock
basedir = /usr/local/myssql
datadir = /data/mysql57/mysql3306
pid-file = /data/mysql57/mysql3306/3306.pid
log-error = /data/mysql57/logs/3306/error-log
log-bin = /data/mysql57/logs/3306/binlog
log-bin-index = /data/mysql57/logs/3306/binlog.index
relay-log = /data/mysql57/logs/3306/relaylog
relay-log-index = /data/mysql57/logs/3306/relaylog.index
master_info_repository = TABLE
relay_log_info_repository = TABLE
log_slave_updates = ON
binlog_checksum = NONE
binlog_format = ROW
transaction_isolation = READ-COMMITTED
gtid_mode = ON
enforce_gtid_consistency = ON
# GR 配置项 其中loose前缀表示若Group Replication plugin未加载 mysql server仍继续启动
transaction_write_set_extraction = XXHASH64
# 组名,此处可拿select uuid();生成
loose-group_replication_group_name = "33c90212-f120-11e7-8426-6c0b846288f8"
# 在mysqld启动时不自动启动组复制
loose-group_replication_start_on_boot = off
loose-group_replication_local_address = "127.0.0.1:24901"
loose-group_replication_group_seeds = "127.0.0.1:24901,127.0.0.1:24902,127.0.0.1:24903"
loose-group_replication_bootstrap_group = off
初始化3306实例datadir:
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql57/3306.cnf --initialize-insecure
启动3306实例:
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql57/3306.cnf &
通过MySQL Client进入第一个实例(密码为空)
/usr/local/mysql/bin/mysql -uroot -p -S /tmp/mysql3306.sock
创建复制用户与授权,并让其作为group的第一个成员:
SET SQL_LOG_BIN=0;
CREATE USER repl@‘%‘;
GRANT REPLICATION SLAVE ON *.* TO repl@‘%‘ IDENTIFIED BY ‘repl@123‘;
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
change master to master_user=‘repl‘,master_passowrd=‘repl@123‘ for channel‘group_replication_recovery‘;
安装GR插件:
INSTALL PLUGIN group_replication SONAME ‘group_replication.so‘;
可以检查一下是否安装成功:
(root@localhost) [(none)]> SELECT * FROM information_schema.plugins WHERE PLUGIN_NAME LIKE ‘%group%‘\G
*************************** 1. row ***************************
PLUGIN_NAME: group_replication
PLUGIN_VERSION: 1.0
PLUGIN_STATUS: ACTIVE
PLUGIN_TYPE: GROUP REPLICATION
PLUGIN_TYPE_VERSION: 1.1
PLUGIN_LIBRARY: group_replication.so
PLUGIN_LIBRARY_VERSION: 1.7
PLUGIN_AUTHOR: ORACLE
PLUGIN_DESCRIPTION: Group Replication (1.0.0)
PLUGIN_LICENSE: GPL
LOAD_OPTION: ON
1 row in set (0.00 sec)
开启第一个组复制:
(root@localhost) [(none)]> SET GLOBAL group_replication_bootstrap_group=ON;
Query OK, 0 rows affected (0.00 sec)
(root@localhost) [(none)]> START GROUP_REPLICATION;
Query OK, 0 rows affected (1.20 sec)
# 启动组复制之后将group_replication_bootstrap_group设置为OFF
# This option must only be set on one server and only when starting the group for the first time or restarting the entire group. After the group has been bootstrapped, set this option to OFF. It should be set to OFF both dynamically and in the configuration files
(root@localhost) [(none)]> SET GLOBAL group_replication_bootstrap_group=OFF;
Query OK, 0 rows affected (0.01 sec)
检查一下组复制成员,其中member_id就是@@server_uuid的值
(root@localhost) [(none)]> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
1 row in set (0.00 sec)
添加测试数据:
(root@localhost) [(none)]> CREATE DATABASE test;
(root@localhost) [(none)]> USE test;
(root@localhost) [test]> CREATE TABLE tb_test(id int PRIMARY KEY, name varchar(20)) CHARACTER SET utf8;
(root@localhost) [test]> INSERT INTO tb_test VALUES(1,‘风暴之灵‘);
(root@localhost) [test]> INSERT INTO tb_test VALUES(2,‘影之灵龛‘);
(root@localhost) [test]> UPDATE tb_test SET name=‘斯嘉蒂之眼‘ WHERE id=2;
(root@localhost) [test]> SELECT * FROM tb_test;
〇 添加第二个实例(3307)
修改3307配置文件,将3306改成3307,并且将loose-group_replication_local_address的端口从24901改成24902:
cp /data/mysql57/3306.cnf /data/mysql57/3307.cnf
sed -i "s/3306/3307/g" /data/mysql57/3307.cnf
sed -i "s/24901\"/24902\"/g" 3307.cnf
初始化3307实例:
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql57/3307.cnf --initialize-insecure
启动3307实例:
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql57/3307.cnf &
通过MySQL Client进入3307实例:
/usr/local/mysql/bin/mysql -uroot -p -S /tmp/mysql3307.sock
重复在3306实例的操作:
SET SQL_LOG_BIN=0;
CREATE USER repl@‘%‘;
GRANT REPLICATION SLAVE ON *.* TO repl@‘%‘ IDENTIFIED BY ‘repl@123‘;
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER=‘repl‘, MASTER_PASSWORD=‘repl@123‘ FOR CHANNEL ‘group_replication_recovery‘;
在3307实例上安装GR插件,开启组复制:
(root@localhost) [(none)]> INSTALL PLUGIN group_replication SONAME ‘group_replication.so‘;
(root@localhost) [(none)]> START GROUP_REPLICATION;
检查一下成员状态:
过了一阵子再检查,仍然是RECOVERING。
再过一阵子检查,发现member_state被置为ERROR:
此时检查3306实例的组复制情况,发现检查不到另一个实例的信息了:
开多一个终端,检查3307实例的error log发现:
应该是解析的问题,修改hosts文件,在末尾加上主机名:
重新操作3307实例:
(root@localhost) [(none)]> STOP GROUP_REPLICATION;
(root@localhost) [(none)]> START GROUP_REPLICATION;
检查组复制状态,发现两个实例的状态均为ONLINE了:
在3307上检查一下同步状态:
〇 添加3308实例:
修改3308配置文件:
cp /data/mysql57/3306.cnf /data/mysql57/3308.cnf
sed -i "s/3306/3308/g" 3308.cnf
sed -i "s/24901\"/24093\"/g" 3308.cnf
然后初始化并启动3308实例:
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql57/3308.cnf --initialize-insecure
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql57/3308.cnf &
同样进入3308实例:
/usr/local/mysql/bin/mysql -uroot -p -S /tmp/mysql3308.sock
在3308实例上重复操作:
SET SQL_LOG_BIN=0;
CREATE USER repl@‘%‘;
GRANT REPLICATION SLAVE ON *.* TO repl@‘%‘ IDENTIFIED BY ‘repl@123‘;
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
CHANGE MASTER TO MASTER_USER=‘repl‘, MASTER_PASSWORD=‘repl@123‘ FOR CHANNEL ‘group_replication_recovery‘;
继续重复操作,安装GR插件并启动它:
INSTALL PLUGIN group_replication SONAME ‘group_replication.so‘;
START GROUP_REPLICATION;
最后再检查一下组复制成员的状态:
当然在3308实例上也已将3306的事务apply过来了:
(root@localhost) [(none)]> SELECT * FROM test.tb_test;
+----+-----------------+
| id | name |
+----+-----------------+
| 1 | 风暴之灵 |
| 2 | 斯嘉蒂之眼 |
+----+-----------------+
2 rows in set (0.00 sec)
root@localhost用户在上述操作中为空密码,可以给root@localhost加个密码……
因为三个实例都在一个GR组里,所以对3306实例操作就行了:
/usr/local/mysql/bin/mysql -uroot -p -S /tmp/mysql3306.sock -e "ALTER USER root@localhost IDENTIFIED BY ‘root123‘;"
当然ALTER操作会被记录到3306的binlog里,并同步到3307和3308实例上。
可以查看一下三台实例的read_only和super-read-only值:
可以发现只有3306实例也就是第一个实例属于可写实例,而3307和3308均为read-only模式。
决定因素为第一个加入该GR组的成员,之后加入该GR组的均为ro,在该模式与MongoDB Replicate Set很相似。
当然如果要确定哪一个成员是primary,可以在三个成员中的任意一个执行:
SELECT b.member_id, b.member_host, b.member_port
FROM performance_schema.global_status a
JOIN performance_schema.replication_group_members b
ON a.variable_value = b.member_id
WHERE a.variable_name= ‘group_replication_primary_member‘;
# 输出结果:
+--------------------------------------+-------------+-------------+
| member_id | member_host | member_port |
+--------------------------------------+-------------+-------------+
| 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y | 3306 |
+--------------------------------------+-------------+-------------+
1 row in set (0.00 sec)
至此,Group Replication默认的single-master mode已经搭建完毕。
〇 将Single-Master Mode修改为Multi-Master Mode
如果要将Single-Master Mode修改为Multi-Master Mode,也比较简单。
考虑到此时的Primary成员是3306,并且假定3306实例在对外提供写服务,我这边的操作如下:
首先停掉两个secondary的组复制,在3307和3308实例上操作:
STOP GROUP_REPLICATION;
SET GLOBAL group_replication_single_primary_mode=FALSE;
SET GLOBAL group_replication_enforce_update_everywhere_checks=TRUE;
再在3306实例上重复以上操作:
STOP GROUP_REPLICATION;
SET GLOBAL group_replication_single_primary_mode=FALSE;
SET GLOBAL group_replication_enforce_update_everywhere_checks=TRUE;
然后在3306上作为第一个成员启动组复制:
(root@localhost) [(none)]> SET GLOBAL group_replication_bootstrap_group=on;
(root@localhost) [(none)]> START GROUP_REPLICATION;
(root@localhost) [(none)]> SET GLOBAL group_replication_bootstrap_group=off;
在停启组复制的过程中,3306实例仍对外提供服务,此处模拟修改:
(root@localhost) [(none)]> UPDATE test.tb_test SET name=‘灵魂守卫‘ WHERE id=1;
(root@localhost) [(none)]> INSERT INTO test.tb_test VALUES(3,‘幻影斧‘);
(root@localhost) [(none)]> SELECT * FROM test.tb_test;
再3307和3308两个实例上分别开启组复制:
START GROUP_REPLICATION;
并检查3307和3308是否将3306的事务应用过来:
当然可以看到,在3306上做的修改,在3307和3308开启组复制之后也已经同步过来了。
那么再检查一下3307和3308是否可写:
显然和Single-Master Mode不一样的是,除了3306实例,另外两个成员也就是3307和3308实例均为可写成员了。
也就是所谓的Multi-Master Mode。
当然可以测试一下:
在3307实例上做insert,在3308实例上update,最后在3306上查询:
至此,已经成功将Single-Master Mode修改为Multi-Master Mode。
P.S. 在多主模式中,已经不能通过下述SQL来查询primary member是哪一台实例了……虽然不明白为毛,可能在后续版本会改进???(猜测)
但总之在多主模式中,每一台status为online的成员都是primary。
SELECT b.member_id, b.member_host, b.member_port
FROM performance_schema.global_status a
JOIN performance_schema.replication_group_members b
ON a.variable_value = b.member_id
WHERE a.variable_name= ‘group_replication_primary_member‘;
从零开始搭建Multi-Master Mode的GR同样也很简单,可以参考:
http://mysqlhighavailability.com/mysqlha/gr/doc/getting_started.html
两种工作模式在配置参数上的核心差别为:
loose-group_replication_single_primary_mode=FALSE
loose-group_replication_enforce_update_everywhere_checks=TRUE
(single-master mode 这俩个参数的值为TRUE和FALSE)
〇 要求和限制
仅可用于InnoDB存储引擎(需要事务的支持和行级锁)
表必须有主键(创建无主键的表不会报错,但在插入数据的时候会抛出:ERROR 3098 (HY000): The table does not comply with the requirements by an external plugin.)
必须启用GTID
必须开启二进制日志,并且其格式必须为ROW(binlog_format=row)
冲突DDl、DML只能在同一成员上执行成功
在多主结构中,不完全支持外键(单主结构中是没有问题的)
不支持serializable的事务隔离级别
只支持IPv4,并且需要低延迟,高带宽的网络环境
GR最大支持9个成员
复制信息元数据必须存在系统表(master-info-repository=TABLE、relay-log-info-repository=TABLE)
二进制日志checksums必须关闭(binlog-checksum=NONE)
不支持savepoint的使用
MySQL 5.7.20 Group Relication(组复制)搭建手册
标签:ever key blog 只读 his hang table rac 1.0