组复制有两种模式:单主模式和多主模式。
1.在单主模式下,组复制具有自动选主功能,每次只有一个 server成员接受更新。
2.在多主模式下,所有的 server 成员都可以同时接受更新。
在三台db服务器上面配置映射/etc/hosts:
[root@master ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.91.21 master
192.168.91.26 node2
192.168.91.27 node1
数据库配置文件(三台服务器):
# Group Replication
server_id = 100 #服务ID
gtid_mode = ON #全局事务
enforce_gtid_consistency = ON #强制GTID的一致性
master_info_repository = TABLE #将master.info元数据保存在系统表中
relay_log_info_repository = TABLE #将relay.info元数据保存在系统表中
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 #为了避免每次启动自动引导具有相同名称的第二个组,所以设置为OFF。
loose-group_replication_local_address = 'master:33061' #以本机端口33061接受来自组中成员的传入连接 根据实际情况填写
loose-group_replication_group_seeds ='master:33061, node1:33062, node2:33063' #组中成员访问表 根据实际情况填写
loose-group_replication_bootstrap_group = off #不启用引导组
loose-group_replication_single_primary_mode = off #关闭单master模式
loose-group_replication_enforce_update_everywhere_checks = ON #多主一致性检查
重启数据库:
[root@master ~]# systemctl restart mysqld
[root@node1 ~]# systemctl restart mysqld
[root@node2 ~]# systemctl restart mysqld
安装插件(三台都需要安装):
mysql> install PLUGIN group_replication SONAME 'group_replication.so';
-- 查看group replication组件
mysql> show plugins;
master操作:
mysql> set SQL_LOG_BIN=0; #停掉日志记录
mysql> grant replication slave on *.* to repl@'192.168.91.%' identified by 'kavl7kAkkle!';
mysql> flush privileges;
mysql> set SQL_LOG_BIN=1; #开启日志记录
mysql> change master to master_user='repl',master_password='kavl7kAkkle!' for channel 'group_replication_recovery'; #构建group replication集群
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 775dec89-ac6e-11e9-bfe6-000c29de2cf7 | master | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
node1和node2上操作:
mysql> set SQL_LOG_BIN=0; #停掉日志记录
mysql> grant replication slave on *.* to repl@'192.168.91.%' identified by 'kavl7kAkkle!';
mysql> flush privileges;
mysql> set SQL_LOG_BIN=1; #开启日志记录
mysql> change master to master_user='repl',master_password='kavl7kAkkle!' for channel 'group_replication_recovery'; #构建group replication集群
mysql> set global group_replication_allow_local_disjoint_gtids_join=ON;
Query OK, 0 rows affected (0.01 sec)
mysql> reset master;
mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (4.02 sec)
mysql> SELECT * FROM performance_schema.replication_group_members; 查看模块是否完成
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 775dec89-ac6e-11e9-bfe6-000c29de2cf7 | master | 3306 | ONLINE |
| group_replication_applier | cb087360-ac2c-11e9-9d6c-000c29cf4f9a | node2 | 3306 | ONLINE |
| group_replication_applier | e919c066-ac2c-11e9-be9e-000c29f2dbd7 | node1 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
3 rows in set (0.00 sec)
至此组搭建完成,组中有三个成员。
测试:
在三个成员上分别创建master、node1、node2库。
初始库:
库已经同步了。
在master上创建数据:
mysql> use master;
Database changed
mysql> create table t1 (id int primary key,name varchar(20));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t1 values(1,'master');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1 values(2,'node1');
Query OK, 1 row affected (0.00 sec)
mysql> insert into t1 values(3,'node3');
Query OK, 1 row affected (0.01 sec)
在另外两台数据库上查看,数据已经同步了。
模拟一个人节点宕机:
[root@master ~]# systemctl stop mysqld
在node1上查看组成员信息发现master节点已经不再组中了:
mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | cb087360-ac2c-11e9-9d6c-000c29cf4f9a | node2 | 3306 | ONLINE |
| group_replication_applier | e919c066-ac2c-11e9-be9e-000c29f2dbd7 | node1 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
2 rows in set (0.00 sec)
在node1上写入数据:
mysql> insert into t1 values(4,'test1');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+----+--------+
| id | name |
+----+--------+
| 1 | master |
| 2 | node1 |
| 3 | node3 |
| 4 | test1 |
+----+--------+
4 rows in set (0.00 sec)
启动master节点的数据库:
[root@master ~]# systemctl start mysqld
mysql> use master;
Database changed
查看数据此时数据没有同步过来
mysql> select * from t1;
+----+--------+
| id | name |
+----+--------+
| 1 | master |
| 2 | node1 |
| 3 | node3 |
+----+--------+
3 rows in set (0.00 sec)
加入组中:
mysql> start group_replication;
Query OK, 0 rows affected (3.22 sec)
mysql> select * from performance_schema.replication_group_members; 此时master节点已经加入到组中了。
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 775dec89-ac6e-11e9-bfe6-000c29de2cf7 | master | 3306 | ONLINE |
| group_replication_applier | cb087360-ac2c-11e9-9d6c-000c29cf4f9a | node2 | 3306 | ONLINE |
| group_replication_applier | e919c066-ac2c-11e9-be9e-000c29f2dbd7 | node1 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
3 rows in set (0.00 sec)
查看数据,此时已经同步了。
mysql> select * from t1;
+----+--------+
| id | name |
+----+--------+
| 1 | master |
| 2 | node1 |
| 3 | node3 |
| 4 | test1 |
+----+--------+
4 rows in set (0.00 sec)