目录
3.1 single-primary mode(单写或单主模式)
3.2 multi-primary mode(多写或多主模式)
1 MySQL的主从复制介绍
MySQL Group Replication(简称 MGR )是 MySQL 官方于 2016 年 12 月推出的一个全新的高可用与高扩 展的解决方案
组复制是 MySQL 5.7.17 版本出现的新特性,它提供了高可用、高扩展、高可靠的 MySQL 集群服务
MySQL 组复制分单主模式和多主模式,传统的mysql复制技术仅解决了数据同步的问题,
MGR 对属于同一组的服务器自动进行协调。对于要提交的事务,组成员必须就全局事务序列中给定事务的顺序达成一致
提交或回滚事务由每个服务器单独完成,但所有服务器都必须做出相同的决定
如果存在网络分区,导致成员无法达成事先定义的分割策略,则在解决此问题之前系统不会继续进行,这是一种内置的自动裂脑保护机制
MGR由组通信系统( Group Communication System ,GCS ) 协议支持
该系统提供故障检测机制、组成员服务以及安全且有序的消息传递
2 组复制流程
首先我们将多个节点共同组成一个复制组,在执行读写(RW)事务的时候,需要通过一致性协议层 (Consensus 层)的同意,也就是读写事务想要进行提交,必须要经过组里“大多数人”(对应 Node 节点)的同意,大多数指的是同意的节点数量需要大于 (N/2+1),这样才可以进行提交,而不是原发起方一个说了算。而针对只读(RO)事务则不需要经过组内同意,直接 提交 即可
节点数量不能超过 9 台
3 组复制单主和多主模式
3.1 single-primary mode(单写或单主模式)
单写模式 group 内只有一台节点可写可读,其他节点只可以读。当主服务器失败时,会自动选择新的主服务器
3.2 multi-primary mode(多写或多主模式)
组内的所有机器都是 primary 节点,同时可以进行读写操作,并且数据是最终一致的。
4 实现mysql组复制
为了避免出错,在所有节点中从新生成数据库数据
编辑主配置文件:
4.1 MASTER 1
[root@mysql-01 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server_id=10
gtid_mode=ON
enforce-gtid-consistency=ON
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
# 将主从复制的信息存储在表中 提高性能很重要
master_info_repository=TABLE
relay_log_info_repository=TABLE
# 禁止对二进制日志校验
binlog_checksum=NONE
# 开启从服务器的日志更新记录
log_slave_updates=ON
# 二进制日志文件名
log_bin=binlog
# 二进制日志格式
binlog_format=ROW
# 加载 Group Replication 插件
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64
# 设置 Group Replication 的集群名称
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
# 控制 MySQL 服务启动时是否自动启动 Group Replication
group_replication_start_on_boot=off
# 指定本地实例的地址和端口,用于 Group Replication
group_replication_local_address="192.168.239.210:33061"
# 指定集群种子成员列表,用于新成员加入集群
group_replication_group_seeds="192.168.239.210:33061,192.168.239.220:33061,192.168.239.230:33061"
# 设置允许连接到 Group Replication 的 IP 白名单
group_replication_ip_whitelist="192.168.239.0/24,127.0.0.1/8"
# 控制是否将当前实例作为集群的第一个成员启动
group_replication_bootstrap_group=off
# 控制是否启用单主模式 禁用单主那就是多主
group_replication_single_primary_mode=OFF
# 强制执行跨所有实例的更新检查
group_replication_enforce_update_everywhere_checks=ON
# 允许具有不连续 GTID 的实例加入集群
group_replication_allow_local_disjoint_gtids_join=1
mysql> alter user root@'localhost' identified by 'Openlab123!';
-- 禁用二进制日志记录,以避免在创建用户时记录日志
mysql> set sql_log_bin=0;
mysql> create user shuyan@'%' identified by 'Openlab123!';
-- 授予 shuyan 用户复制从属权限,允许其从任何主机连接
mysql> grant replication slave on *.* to shuyan@'%';
mysql> flush privileges;
-- 重新启用二进制日志记录
mysql> set sql_log_bin=1;
-- 设置复制源(Master)的用户名和密码,用于 Group Replication 的恢复过程
mysql> change master to
-> master_user='shuyan',
-> master_password='Openlab123!'
-> for channel 'group_replication_recovery';
-- 允许当前实例作为集群的第一个成员启动 Group Replication
mysql> set global group_replication_bootstrap_group=on;
-- 启动 Group Replicatio
mysql> start group_replication;
-- 禁止当前实例作为集群的第一个成员启动 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 | 14ffce7c-61e9-11ef-bec7-000c29a51779 | mysql-01 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
4.2 MASTER 2
[root@mysql-02 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server_id=20
gtid_mode=ON
enforce-gtid-consistency=ON
#禁用指定存储引擎
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
# 打开数据库中继
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
# 加载组复制插件
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64 #把每个事件编码为加密散列
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address="192.168.239.220:33061"
group_replication_group_seeds="192.168.239.210:33061,192.168.239.220:33061,192.168.239.230:33061"
group_replication_ip_whitelist="192.168.239.0/24,127.0.0.1/8"
group_replication_bootstrap_group=off
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
group_replication_allow_local_disjoint_gtids_join=1
mysql> alter user 'root'@'localhost' identified by 'Openlab123';
mysql> set sql_log_bin=0;
mysql> create user shuyan@'%' identified by 'Openlab123!';
mysql> grant replication slave on *.* to shuyan@'%';
mysql> flush privileges;
mysql> set sql_log_bin=1;
mysql> change master to
-> master_user='shuyan',
-> master_password='Openlab123!'
-> for channel 'group_replication_recovery';
mysql> start group_replication;
mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 14ffce7c-61e9-11ef-bec7-000c29a51779 | mysql-01 | 3306 | ONLINE |
| group_replication_applier | 51a6613a-61eb-11ef-bdb3-000c299efdf0 | mysql-02 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
4.3 MASTER 3
[root@mysql-03 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
symbolic-links=0
server-id=30
gtid_mode=ON
enforce-gtid-consistency=ON
#禁用指定存储引擎
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
# 打开数据库中继
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW
# 加载组复制插件
plugin_load_add='group_replication.so'
transaction_write_set_extraction=XXHASH64 #把每个事件编码为加密散列
group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
group_replication_start_on_boot=off
group_replication_local_address="192.168.239.230:33061"
group_replication_group_seeds="192.168.239.210:33061,192.168.239.220:33061,192.168.239.230:33061"
group_replication_ip_whitelist="192.168.239.0/24,127.0.0.1/8"
group_replication_bootstrap_group=off
group_replication_single_primary_mode=OFF
group_replication_enforce_update_everywhere_checks=ON
group_replication_allow_local_disjoint_gtids_join=1
mysql> set sql_log_bin=0;
mysql> create user 'shuyan'@'%' identified by 'Openlab123!';
mysql> grant replication slave on *.* to shuyan@'%';
mysql> flush privileges;
mysql> set sql_log_bin=1;
mysql> change master to
-> master_user='shuyan',
-> master_password='Openlab123!'
-> for channel 'group_replication_recovery';
mysql> start group_replication;
4.4 MASTER 1 检验
[root@mysql-01 ~]# mysql -uroot -pOpenlab123! -e "select * from performance_schema.replication_group_members;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 14ffce7c-61e9-11ef-bec7-000c29a51779 | mysql-01 | 3306 | ONLINE |
| group_replication_applier | 51a6613a-61eb-11ef-bdb3-000c299efdf0 | mysql-02 | 3306 | ONLINE |
| group_replication_applier | 6d1255c0-61ec-11ef-99d9-000c298f2a8a | mysql-03 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+