本博文介绍了Group Replication的两种工作模式的架构。
并详细介绍了Single-Master Mode的部署过程,以及如何切换到Multi-Master Mode。
当然,文末给出了Group Replication的配置要求和一些限制。
〇 结构介绍
在2016年12月发布的5.7.17版本的MySQL,甲骨文宣布Group Replication已经GA。
Group Replication(下简称GR)有两个工作模式,分别为S ingle-Master Mode与 Multi-Master Mode:
Single-Master Mode的failover图:
只有primary成员 可读写,而其他的节点为只读,在primary成员发生故障时,将会有其他成员顶替成primary。
Multi-Master Mode的failover图:
所有的成员均可读可写。
关于脑裂问题,可发现MySQL Group Replication与MongoDB Relicate Set有相似之处:
和MongoDB的Relicate Set相近,其读写库类似于Primary,只读库类似于Secondary。
(下图来自MySQL官方)
(下图来自MongoDB 3.4 Manual)
〇 部署
测试实例数量:3台
版本:MySQL 5.7.17
安装(此处用的是二进制包安装)
mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
创建数据目录及日志目录:
解压二进制包并将其设置为basedir:
〇 添加第一台实例:
添加3306实例的配置文件:
初始化3306实例datadir:
启动3306实例:
通过MySQL Client进入第一个实例(密码为空)
创建复制用户与授权,并让其作为group的第一个成员:
安装GR插件:
可以检查一下是否安装成功:
开启第一个组复制:
检查一下组复制成员,其中member_id就是@@server_uuid的值
添加测试数据:
〇 添加第二个实例(3307)
修改3307配置文件,将3306改成3307,并且将loose-group_replication_local_address的端口从24901改成24902:
初始化3307实例:
启动3307实例:
通过MySQL Client进入3307实例:
重复在3306实例的操作:
在3307实例上安装GR插件,开启组复制:
检查一下成员状态:
过了一阵子再检查,仍然是RECOVERING。
再过一阵子检查,发现member_state被置为ERROR:
此时检查3306实例的组复制情况,发现检查不到另一个实例的信息了:
开多一个终端,检查3307实例的error log发现:
应该是解析的问题,修改hosts文件,在末尾加上主机名:
重新操作3307实例:
检查组复制状态,发现两个实例的状态均为ONLINE了:
在3307上检查一下同步状态:
〇 添加3308实例:
修改3308配置文件:
然后初始化并启动3308实例:
同样进入3308实例:
在3308实例上重复操作:
继续重复操作,安装GR插件并启动它:
最后再检查一下组复制成员的状态:
当然在3308实例上也已将3306的事务apply过来了:
root@localhost用户在上述操作中为空密码,可以给root@localhost加个密码……
因为三个实例都在一个GR组里,所以对3306实例操作就行了:
当然ALTER操作会被记录到3306的binlog里,并同步到3307和3308实例上。
可以查看一下三台实例的read_only和super-read-only值:
可以发现只有3306实例也就是第一个实例属于可写实例,而3307和3308均为read-only模式。
决定因素为第一个加入该GR组的成员,之后加入该GR组的均为ro,在该模式与MongoDB Replicate Set很相似。
当然如果要确定哪一个成员是primary,可以在三个成员中的任意一个执行:
至此,Group Replication默认的single-master mode已经搭建完毕。
〇 将S ingle-Master Mode修改为Multi-Master Mode
如果要将Single-Master Mode修改为Multi-Master Mode,也比较简单。
考虑到此时的Primary成员是3306,并且假定3306实例在对外提供写服务, 我这边的操作如下:
首先停掉两个 secondary的组复制, 在3307和3308实例上操作 :
再在3306实例上重复以上操作:
然后在3306上作为第一个成员启动组复制:
在停启组复制的 过程中,3306实例仍对外提供服务 ,此处模拟修改:
再3307和3308两个实例上分别开启组复制:
并 检查3307和3308 是否将3306的事务应用过来 :
当然可以看到,在3306上做的修改,在3307和3308开启组复制之后也已经同步过来了。
那么再检查一下3307和3308是否可写:
显然和
Single-Master M
ode
不一样的是,除了3306实例,另外两个成员也就是3307和3308实例均为可写成员了。
也就是所谓的 Multi-Master M ode 。
当然可以测试一下:
在3307实例上做insert,在3308实例上update,最后在3306上查询:
至此,已经成功将Single-Master Mode修改为Multi-Master Mode。
P.S. 在多主模式中,已经不能通过下述SQL来查询primary member是哪一台实例了……虽然不明白为毛,可能在后续版本会改进???(猜测)
但总之在多主模式中,每一台status为online的成员都是primary。
总之……看起来很好用的样子。
从零开始搭建Multi-Master Mode的GR同样也很简单,可以参考:
http://mysqlhighavailability.com/mysqlha/gr/doc/getting_started.html
两种工作模式在配置参数上的核心差别为:
〇 要求和限制
仅可用于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 Reference Manual - 19.2 Group Replication
马楚成 - 使用群组复制实现MySQL高可用性 (https://www.mysql.com/news-and-events/web-seminars/mysql-cluster-ha-group-replication2-zh/)
mysqlha组文档 Docs ? Requirements and Limitations
作者微信公众号(持续更新)
并详细介绍了Single-Master Mode的部署过程,以及如何切换到Multi-Master Mode。
当然,文末给出了Group Replication的配置要求和一些限制。
〇 结构介绍
在2016年12月发布的5.7.17版本的MySQL,甲骨文宣布Group Replication已经GA。
Group Replication(下简称GR)有两个工作模式,分别为S ingle-Master Mode与 Multi-Master Mode:
Single-Master Mode的failover图:
![](http://img.blog.itpub.net/blog/attachment/201702/9/29773961_1486628226v7Pr.png?x-oss-process=style/bb)
只有primary成员 可读写,而其他的节点为只读,在primary成员发生故障时,将会有其他成员顶替成primary。
Multi-Master Mode的failover图:
![](http://img.blog.itpub.net/blog/attachment/201702/9/29773961_1486629118D7rR.png?x-oss-process=style/bb)
所有的成员均可读可写。
关于脑裂问题,可发现MySQL Group Replication与MongoDB Relicate Set有相似之处:
和MongoDB的Relicate Set相近,其读写库类似于Primary,只读库类似于Secondary。
(下图来自MySQL官方)
![](http://img.blog.itpub.net/blog/attachment/201702/9/29773961_14866281994rfG.jpg?x-oss-process=style/bb)
(下图来自MongoDB 3.4 Manual)
![](http://img.blog.itpub.net/blog/attachment/201702/9/29773961_1486628210NyHD.jpg?x-oss-process=style/bb)
〇 部署
测试实例数量:3台
版本:MySQL 5.7.17
安装(此处用的是二进制包安装)
mysql-5.7.17-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:
mv mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz /data/mysql57
cd /data/mysql57
tar zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.17-linux-glibc2.5-x86_64 mysql-basedir
cd /data/mysql57
tar zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.17-linux-glibc2.5-x86_64 mysql-basedir
〇 添加第一台实例:
添加3306实例的配置文件:
- vim /data/mysql57/mysql3306/3306.cnf
-
- [client]
- prompt = "(\u@\h) [\d]> "
-
- [mysqld]
- server_id = 3306
- user = mysql
- port = 3306
- socket = /tmp/mysql3306.sock
- basedir = /data/mysql57/mysql-basedir
- 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
- loose-group_replication_group_name = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" # 组名,此处可拿select uuid();生成
- loose-group_replication_start_on_boot = off # 在mysqld启动时不自动启动组复制
- 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:
- /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3306.cnf --initialize-insecure
启动3306实例:
- /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3306.cnf &
通过MySQL Client进入第一个实例(密码为空)
- /data/mysql57/mysql-basedir/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_PASSWORD='repl@123' FOR CHANNEL 'group_replication_recovery';
安装GR插件:
- (root@localhost) [(none)]> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
- Query OK, 0 rows affected (0.07 sec)
可以检查一下是否安装成功:
- (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;
- Query OK, 1 row affected (0.00 sec)
-
- (root@localhost) [(none)]> USE test;
- Database changed
-
- (root@localhost) [test]> CREATE TABLE tb_test(id int PRIMARY KEY, name varchar(20)) CHARACTER SET utf8;
- Query OK, 0 rows affected (0.04 sec)
-
- (root@localhost) [test]> INSERT INTO tb_test VALUES(1,'风暴之灵');
- Query OK, 1 row affected (0.00 sec)
-
- (root@localhost) [test]> INSERT INTO tb_test VALUES(2,'影之灵龛');
- Query OK, 1 row affected (0.01 sec)
-
- (root@localhost) [test]> UPDATE tb_test SET name='斯嘉蒂之眼' WHERE id=2;
- Query OK, 1 row affected (0.02 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
-
- (root@localhost) [test]> SELECT * FROM tb_test;
- +----+-----------------+
- | id | name |
- +----+-----------------+
- | 1 | 风暴之灵 |
- | 2 | 斯嘉蒂之眼 |
- +----+-----------------+
- 2 rows in set (0.00 sec)
〇 添加第二个实例(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实例:
- /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3307.cnf --initialize-insecure
启动3307实例:
- /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3307.cnf &
通过MySQL Client进入3307实例:
- /data/mysql57/mysql-basedir/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';
- Query OK, 0 rows affected (0.00 sec)
-
- (root@localhost) [(none)]> START GROUP_REPLICATION;
- Query OK, 0 rows affected (5.61 sec)
检查一下成员状态:
- (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 |
- | group_replication_applier | 6bddae0d-eca1-11e6-b9d5-000c297f23b7 | sAno1y | 3307 | RECOVERING |
- +---------------------------+--------------------------------------+-------------+-------------+--------------+
- 2 rows in set (0.00 sec)
过了一阵子再检查,仍然是RECOVERING。
再过一阵子检查,发现member_state被置为ERROR:
- (root@localhost) [(none)]> SELECT * FROM performance_schema.replication_group_members;
- +---------------------------+--------------------------------------+-------------+-------------+--------------+
- | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
- +---------------------------+--------------------------------------+-------------+-------------+--------------+
- | group_replication_applier | 6bddae0d-eca1-11e6-b9d5-000c297f23b7 | sAno1y | 3307 | ERROR |
- +---------------------------+--------------------------------------+-------------+-------------+--------------+
- 1 row in set (0.01 sec)
此时检查3306实例的组复制情况,发现检查不到另一个实例的信息了:
- (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)
开多一个终端,检查3307实例的error log发现:
- 2017-02-07T12:06:21.674093Z 47 [ERROR] Slave I/O for channel 'group_replication_recovery': error connecting to master 'repl@sAno1y:3306' - retry-time: 60 retries: 1, Error_code: 2005
应该是解析的问题,修改hosts文件,在末尾加上主机名:
- [root@sAno1y 3307]# hostname
- sAno1y
- [root@sAno1y 3307]# cat /etc/hosts
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 sAno1y
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 sAno1y
重新操作3307实例:
- (root@localhost) [(none)]> STOP GROUP_REPLICATION;
- Query OK, 0 rows affected (0.00 sec)
-
- (root@localhost) [(none)]> START GROUP_REPLICATION;
- Query OK, 0 rows affected (7.80 sec)
检查组复制状态,发现两个实例的状态均为ONLINE了:
- (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 |
- | group_replication_applier | 6bddae0d-eca1-11e6-b9d5-000c297f23b7 | sAno1y | 3307 | ONLINE |
- +---------------------------+--------------------------------------+-------------+-------------+--------------+
- 2 rows in set (0.00 sec)
在3307上检查一下同步状态:
- (root@localhost) [(none)]> SELECT * FROM test.tb_test;
- +----+-----------------+
- | id | name |
- +----+-----------------+
- | 1 | 风暴之灵 |
- | 2 | 斯嘉蒂之眼 |
- +----+-----------------+
- 2 rows in set (0.00 sec)
〇 添加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实例:
- /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3308.cnf --initialize-insecure
- /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3308.cnf &
同样进入3308实例:
- /data/mysql57/mysql-basedir/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;
最后再检查一下组复制成员的状态:
- (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 |
- | group_replication_applier | 6bddae0d-eca1-11e6-b9d5-000c297f23b7 | sAno1y | 3307 | ONLINE |
- | group_replication_applier | 6ce8c980-ed4a-11e6-bf00-000c297f23b7 | sAno1y | 3308 | ONLINE |
- +---------------------------+--------------------------------------+-------------+-------------+--------------+
- 3 rows in set (0.00 sec)
当然在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实例操作就行了:
- /data/mysql57/mysql-basedir/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值:
- [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3306.sock -e "SELECT @@read_only, @@super_read_only";
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +-------------+-------------------+
- | @@read_only | @@super_read_only |
- +-------------+-------------------+
- | 0 | 0 |
- +-------------+-------------------+
-
- [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "SELECT @@read_only, @@super_read_only";
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +-------------+-------------------+
- | @@read_only | @@super_read_only |
- +-------------+-------------------+
- | 1 | 1 |
- +-------------+-------------------+
-
- [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3308.sock -e "SELECT @@read_only, @@super_read_only";
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +-------------+-------------------+
- | @@read_only | @@super_read_only |
- +-------------+-------------------+
- | 1 | 1 |
- +-------------+-------------------+
可以发现只有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已经搭建完毕。
〇 将S ingle-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;
- Query OK, 0 rows affected (0.00 sec)
-
- (root@localhost) [(none)]> START GROUP_REPLICATION;
- Query OK, 0 rows affected (1.05 sec)
-
- (root@localhost) [(none)]> SET GLOBAL group_replication_bootstrap_group=off;
- Query OK, 0 rows affected (0.00 sec)
在停启组复制的 过程中,3306实例仍对外提供服务 ,此处模拟修改:
- (root@localhost) [(none)]> UPDATE test.tb_test SET name='灵魂守卫' WHERE id=1;
- Query OK, 1 row affected (0.02 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
-
- (root@localhost) [(none)]> INSERT INTO test.tb_test VALUES(3,'幻影斧');
- Query OK, 1 row affected (0.00 sec)
-
- (root@localhost) [(none)]> SELECT * FROM test.tb_test;
- +----+-----------------+
- | id | name |
- +----+-----------------+
- | 1 | 灵魂守卫 |
- | 2 | 斯嘉蒂之眼 |
- | 3 | 幻影斧 |
- +----+-----------------+
- 3 rows in set (0.00 sec)
再3307和3308两个实例上分别开启组复制:
- START GROUP_REPLICATION;
并 检查3307和3308 是否将3306的事务应用过来 :
- [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "START GROUP_REPLICATION;"
- mysql: [Warning] Using a password on the command line interface can be insecure.
- [root@sAno1y mysql57]#
- [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3308.sock -e "START GROUP_REPLICATION;"
- mysql: [Warning] Using a password on the command line interface can be insecure.
- [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "SELECT * FROM test.tb_test;"
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +----+-----------------+
- | id | name |
- +----+-----------------+
- | 1 | 灵魂守卫 |
- | 2 | 斯嘉蒂之眼 |
- | 3 | 幻影斧 |
- +----+-----------------+
- [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3308.sock -e "SELECT * FROM test.tb_test;"
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +----+-----------------+
- | id | name |
- +----+-----------------+
- | 1 | 灵魂守卫 |
- | 2 | 斯嘉蒂之眼 |
- | 3 | 幻影斧 |
- +----+-----------------+
那么再检查一下3307和3308是否可写:
- [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "SELECT @@read_only, @@super_read_only;"
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +-------------+-------------------+
- | @@read_only | @@super_read_only |
- +-------------+-------------------+
- | 0 | 0 |
- +-------------+-------------------+
- [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3308.sock -e "SELECT @@read_only, @@super_read_only;"
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +-------------+-------------------+
- | @@read_only | @@super_read_only |
- +-------------+-------------------+
- | 0 | 0 |
- +-------------+-------------------+
也就是所谓的 Multi-Master M ode 。
当然可以测试一下:
在3307实例上做insert,在3308实例上update,最后在3306上查询:
- [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "INSERT INTO test.tb_test VALUES(4,'吸血面具')";
- mysql: [Warning] Using a password on the command line interface can be insecure.
- [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3308.sock -e "UPDATE test.tb_test SET name='撒旦之邪力' WHERE id=4;"
- mysql: [Warning] Using a password on the command line interface can be insecure.
- [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3306.sock -e "SELECT * FROM test.tb_test;"
- mysql: [Warning] Using a password on the command line interface can be insecure.
- +----+-----------------+
- | id | name |
- +----+-----------------+
- | 1 | 灵魂守卫 |
- | 2 | 斯嘉蒂之眼 |
- | 3 | 幻影斧 |
- | 4 | 撒旦之邪力 |
- +----+-----------------+
至此,已经成功将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';
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';
总之……看起来很好用的样子。
![](http://img.blog.itpub.net/blog/attachment/201702/10/29773961_14867179245m9G.jpg?x-oss-process=style/bb)
从零开始搭建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 Reference Manual - 19.2 Group Replication
马楚成 - 使用群组复制实现MySQL高可用性 (https://www.mysql.com/news-and-events/web-seminars/mysql-cluster-ha-group-replication2-zh/)
mysqlha组文档 Docs ? Requirements and Limitations
作者微信公众号(持续更新)
![](http://img.blog.itpub.net/blog/attachment/201805/23/29773961_1527061708b8rU.png?x-oss-process=style/bb)
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29773961/viewspace-2133314/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29773961/viewspace-2133314/