1 介绍
MySQL Group Replication(简称MGR)是MySQL官方于2016年12月推出的一个全新的高可用与高扩展的解决方案。MySQL组复制提供了高可用、高扩展、高可靠的MySQL集群服务。

高一致性,基于原生复制及paxos协议的组复制技术,并以插件的方式提供,提供一致数据安全保证;

高容错性,只要不是大多数节点坏掉就可以继续工作,有自动检测机制,当不同节点产生资源争用冲突时,不会出现错误,按照先到者优先原则进行处理,并且内置了自动化脑裂防护机制;

高扩展性,节点的新增和移除都是自动的,新节点加入后,会自动从其他节点上同步状态,直到新节点和其他节点保持一致,如果某节点被移除了,其他节点自动更新组信息,自动维护新的组信息;

高灵活性,有单主模式和多主模式,单主模式下,会自动选主,所有更新操作都在主上进行;多主模式下,所有server都可以同时处理更新操作。

MGR是MySQL数据库未来发展的一个重要方向。

2 环境准备
操作系统centos7
192.168.200.111  mysql-5.7        181
192.168.200.112  mysql-5.7  182
192.168.200.113  mysql-5.7  183
2.2 二进制安装MySQL 省略
2.3 设置hostname和ip映射
[root@server2 ~]# vim /etc/hosts
192.168.200.111 server1
192.168.200.112 server2
192.168.200.113 server3scp /etc/hosts  192.168.200.112
scp /etc/hosts 192.168.200.113
111:
hostname  server1
bash
112
hostname server2
bash
113
hostname server3
bash
3. 创建复制环境
3.1 服务器server1
3.1.1 配置/etc/my.cnf
添加:
server_id = 181
gtid_mode = ON
enforce_gtid_consistency = ON
master_info_repository = TABLE
relay_log_info_repository = TABLE
binlog_checksum = NONE
log_slave_updates = ON
log_bin = binlog
binlog_format= ROW
transaction_write_set_extraction = XXHASH64
loose-group_replication_group_name = 'ce9be252-2b71-11e6-b8f4-00212844f856'
loose-group_replication_start_on_boot = off
loose-group_replication_local_address = '192.168.200.111:33061'
loose-group_replication_group_seeds ='192.168.200.111:33061,192.168.200.112:33062,192.168.200.113:33063'
loose-group_replication_bootstrap_group = off3.1.2 服务器apollo.mgr.com上建立复制账号:
[root@server1 ~]# /etc/init.d/mysqld restart
[root@server1 ~]# mysql -uroot -p123123
mysql> set SQL_LOG_BIN=0;
mysql> create user repl@'%' identified by 'Love88me=-.,';
mysql> grant replication slave on *.* to repl@'%';
mysql> flush privileges;
mysql> set SQL_LOG_BIN=1;
mysql> change master to master_user='repl',master_password='Love88me=-.,' for channel 'group_replication_recovery'; -- 构建group replication集群3.1.3 在mysql服务器apollo.mgr.com上安装group replication插件
mysql> install PLUGIN group_replication SONAME 'group_replication.so';
mysql> show plugins;
3.1.4 启动服务器server1上mysql的group replication
-- 设置group_replication_bootstrap_group为ON是为了标示以后加入集群的服务器以这台服务器为基准,以后加入的就不需要设置。
mysql> set global group_replication_bootstrap_group=ON; 
-- 作为首个节点启动mgr集群
mysql> start group_replication;
mysql> set global group_replication_bootstrap_group=OFF;3.1.5 查看mgr的状态
-- 查询表performance_schema.replication_group_members 
mysql> select * from performance_schema.replication_group_members;
注意观看:MEMBER_STATE 是不是ONLINE,如果是ONLINE则正确
3.1.6 测试服务器server1上的mysql
mysql> create database test;
mysql> use test;
mysql> create table t1(c1 int primary key, c2 text not null);
mysql> insert into t1 values (1, 'Luis');
mysql> select * from t1; 
3.2 复制组添加新实例server2
3.2.1 修改/etc/my.cnf 配置文件
添加:
server_id = 182
gtid_mode = ON
enforce_gtid_consistency = ON
master_info_repository = TABLE
relay_log_info_repository = TABLE
binlog_checksum = NONE
log_slave_updates = ON
log_bin = binlog
binlog_format= ROW
transaction_write_set_extraction = XXHASH64
loose-group_replication_group_name = 'ce9be252-2b71-11e6-b8f4-00212844f856'
loose-group_replication_start_on_boot = off
loose-group_replication_local_address = '192.168.200.112:33062'
loose-group_replication_group_seeds ='192.168.200.111:33061,192.168.200.112:33062,192.168.200.113:33063'
loose-group_replication_bootstrap_group = off3.2.2 用户凭证
mysql> set SQL_LOG_BIN=0;
mysql> create user repl@'%' identified by 'Love88me=-.,';
mysql> grant replication slave on *.* to repl@'%';
mysql> flush privileges;
mysql> SET SQL_LOG_BIN=1;
mysql> change master to master_user='repl',master_password='Love88me=-.,' for channel 'group_replication_recovery';
-- 安装group replication插件
mysql> install PLUGIN group_replication SONAME 'group_replication.so';
3.2.3 把实例添回到之前的复制组
mysql> set global group_replication_allow_local_disjoint_gtids_join=ON;
mysql> start group_replication;
3.2.4 在server2上查看复制组状态
mysql> select * from performance_schema.replication_group_members;
查看是不是有两个组以及MEMBER_STATE 是不是都是ONLINE,是则成功
不是则服务器之间通道未打开
3.2.5 在新回的实例上查看数据库发现test库和t1表已经同步
mysql> show databases;
3.3 以同样方法添加server3
详细步骤请参考3.2, 这里只给出配置文件/etc/my.cnf:
server_id = 183
gtid_mode = ON
enforce_gtid_consistency = ON
master_info_repository = TABLE
relay_log_info_repository = TABLE
binlog_checksum = NONE
log_slave_updates = ON
log_bin = binlog
binlog_format= ROWtransaction_write_set_extraction = XXHASH64
loose-group_replication_group_name = 'ce9be252-2b71-11e6-b8f4-00212844f856'
loose-group_replication_start_on_boot = offloose-group_replication_local_address = '192.168.200.113:33063'
loose-group_replication_group_seeds ='192.168.200.111:33061,192.168.200.112:33062,192.168.200.113:33063'
loose-group_replication_bootstrap_group = off3.4 查看复制组状态
mysql> select * from performance_schema.replication_group_members;
最后三个状态都是ONLINE