MGR全称MySQL Group Replication(Mysql组复制),是MySQL官方于2016年12月推出的一个全新的高可用与高扩展的解决方案。MGR提供了高可用、高扩展、高可靠的MySQL集群服务。在MGR出现之前,用户常见的MySQL高可用方式,无论怎么变化架构,本质就是Master-Slave架构。MySQL 5.7版本开始支持无损半同步复制(lossless semi-sync replication),从而进一步提示数据复制的强一致性。
MGR是MySQL数据库未来发展的一个重要方向。
(1).MGR的特性
高一致性。基于原生复制及paxos协议的组复制技术,并以插件的方式提供,提供一致数据安全保证;
高容错性。只要不是大多数节点坏掉就可以继续工作,有自动检测机制,当不同节点产生资源争用冲突时,不会出现错误,按照先到者优先原则进行处理,并且内置了自动化脑裂防护机制;
高扩展性。节点的新增和移除都是自动的,新节点加入后,会自动从其他节点上同步状态,直到新节点和其他节点保持一致,如果某节点被移除了,其他节点自动更新组信息,自动维护新的组信息;
高灵活性。有单主模式和多主模式,单主模式下,会自动选主,所有更新操作都在主上进行;多主模式下,所有server都可以同时处理更新操作。
(2).搭建MGR的基础结构要求和使用限制(重点)
基础结构要求:1.引擎必须为innodb,因为需事务支持在commit时对各节点进行冲突检查;2.每个表必须有主键,在进行事务冲突检测时需要利用主键值对比;3.必须开启binlog且为row格式;4.开启GTID,且主从状态信息存于表中(--master-info-repository=TABLE 、--relay-log-info-repository=TABLE),--log-slave-updates打开;5.一致性检测设置--transaction-write-set-extraction=XXHASH64。
使用限制:1.RP和普通复制binlog校验不能共存,需设置--binlog-checksum=none;2.不支持gap lock(间隙锁),隔离级别需设置为read_committed;3.不支持对表进行锁操作(lock /unlock table),不会发送到其他节点执行 ,影响需要对表进行加锁操作的情况,列入mysqldump全表备份恢复操作;4.不支持serializable(序列化)隔离级别;5.DDL语句不支持原子性,不能检测冲突,执行后需自行校验是否一;6.多主模式下不支持外键,单主模式下支持外键;最多9个节点,超过9台无法加入集群
(3).实验环境
youxi1 192.168.1.6 CentOS7.6 Mysql5.7.26 端口号3306 server-id=1
youxi2 192.168.1.7 CentOS7.6 Mysql5.7.26 端口号3306 server-id=1
youxi3 192.168.1.8 CentOS7.6 Mysql5.7.26 端口号3306 server-id=1
另外,三台服务器上都进行IP的映射
[root@youxi1 ~]# vim /etc/hosts 192.168.1.6 youxi1.cn youxi1 //长域名和短域名只要映射一个即可 192.168.1.7 youxi2.cn youxi2 192.168.1.8 youxi3.cn youxi3
(1).单主模式
我是在空数据库下操作,如果主数据集已存在数据,需要将主数据库的数据导出再导入到从数据库。另外还需保证引擎为innodb,每个表必须存在主键。
1)以youxi1作为主服务器,对youxi1进行配置
创建一个复制用的用户
mysql> grant replication slave on *.* to 'repl'@'192.168.1.%' identified by '12345678'; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
修改配置文件,加入到[mysqld]模块下。然后重启mysqld
[root@youxi1 ~]# vim /etc/my.cnf server-id=1 //Mysql服务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 //不自动启用组复制集群 loose-group_replication_local_address='youxi1:33061' //以本机端口33061接受来自组中成员的传入连接 loose-group_replication_group_seeds='youxi1:33061,youxi2:33062,youxi3:33063' //组中成员访问表 loose-group_replication_bootstrap_group=off //不启用引导组
[root@youxi1 ~]# systemctl restart mysqld
修改master信息,构建组复制(group replication)集群信息
mysql> change master to master_user='repl',master_password='12345678' for channel 'group_replication_recovery'; Query OK, 0 rows affected, 2 warnings (0.04 sec)
安装组复制(group replication)插件,并查看组件信息
mysql> install PLUGIN group_replication SONAME 'group_replication.so'; Query OK, 0 rows affected (0.21 sec) mysql> show plugins; //查看组件信息 +----------------------------+----------+--------------------+----------------------+---------+ | Name | Status | Type | Library | License | +----------------------------+----------+--------------------+----------------------+---------+ | binlog | ACTIVE | STORAGE ENGINE | NULL | GPL | | mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL | | sha256_password | ACTIVE | AUTHENTICATION | NULL | GPL | | CSV | ACTIVE | STORAGE ENGINE | NULL | GPL | | MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL | | InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL | | INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_LOCKS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_LOCK_WAITS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMPMEM | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMPMEM_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMP_PER_INDEX | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMP_PER_INDEX_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_PAGE | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_TEMP_TABLE_INFO | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_METRICS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_DEFAULT_STOPWORD | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_BEING_DELETED | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_CONFIG | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_INDEX_CACHE | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_FT_INDEX_TABLE | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_TABLES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_TABLESTATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_COLUMNS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_FIELDS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_FOREIGN | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_FOREIGN_COLS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_TABLESPACES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_DATAFILES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_VIRTUAL | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL | | MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL | | PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL | | ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL | | BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL | | FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL | | partition | ACTIVE | STORAGE ENGINE | NULL | GPL | | ngram | ACTIVE | FTPARSER | NULL | GPL | | validate_password | DISABLED | VALIDATE PASSWORD | validate_password.so | GPL | | group_replication | ACTIVE | GROUP REPLICATION | group_replication.so | GPL | +----------------------------+----------+--------------------+----------------------+---------+ 46 rows in set (0.00 sec)
作为主服务器需要由这台服务器开启引导,开启组复制(group replication)集群
mysql> set global group_replication_bootstrap_group=on; //开启组复制引导 Query OK, 0 rows affected (0.00 sec) mysql> start group_replication; //开启组复制 Query OK, 0 rows affected (2.24 sec) mysql> set global group_replication_bootstrap_group=off; //关闭组复制引导 Query OK, 0 rows affected (0.00 sec)
查看到添加到组复制集群的服务器信息
mysql> select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | c9e3662b-9020-11e9-94aa-000c29721e89 | youxi1 | 3306 | ONLINE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ 1 row in set (0.00 sec)
注意:如果防火墙是打开的,记得添加mysql的端口号。
[root@youxi1 ~]# firewall-cmd --permanent --zone=public --add-port=3306/tcp success [root@youxi1 ~]# firewall-cmd --permanent --zone=public --add-port=33061/tcp success [root@youxi1 ~]# firewall-cmd --reload success [root@youxi1 ~]# firewall-cmd --zone=public --list-ports 3306/tcp 33061/tcp
2)对youxi2进行配置
创建一个复制用的用户
mysql> grant replication slave on *.* to 'repl'@'192.168.1.%' identified by '12345678'; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
修改配置文件,加入到[mysqld]模块下。然后重启mysqld
[root@youxi2 ~]# vim /etc/my.cnf server-id=2 //修改 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='youxi2:33062' //修改 loose-group_replication_group_seeds='youxi1:33061,youxi2:33062,youxi3:33063' loose-group_replication_bootstrap_group=off [root@youxi2 ~]# systemctl restart mysqld
修改master信息,构建组复制(group replication)集群信息
mysql> change master to master_user='repl',master_password='12345678' for channel 'group_replication_recovery'; Query OK, 0 rows affected, 2 warnings (0.05 sec)
安装组复制(group replication)插件
mysql> install PLUGIN group_replication SONAME 'group_replication.so'; Query OK, 0 rows affected (0.35 sec)
把youxi2加到之前的组复制(group replication)
mysql> set global group_replication_allow_local_disjoint_gtids_join=ON; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> start group_replication; Query OK, 0 rows affected, 1 warning (5.92 sec)
查看到添加到组复制集群的服务器信息
mysql> select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | 8fbf8b41-84fe-11e9-897e-000c29f27e52 | youxi2 | 3306 | ONLINE | | group_replication_applier | c9e3662b-9020-11e9-94aa-000c29721e89 | youxi1 | 3306 | ONLINE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ 2 rows in set (0.00 sec)
注意:如果防火墙是打开的,记得添加mysql的端口号。
[root@youxi2 ~]# firewall-cmd --permanent --zone=public --add-port=3306/tcp success [root@youxi2 ~]# firewall-cmd --permanent --zone=public --add-port=33062/tcp success [root@youxi2 ~]# firewall-cmd --reload success [root@youxi2 ~]# firewall-cmd --zone=public --list-ports 3306/tcp 33062/tcp
3)youxi3的配置与youxi2的配置几乎一样,只需在/etc/my.cnf修改server-id和loose-group_replication_local_address即可。
查看一下组复制集群信息
mysql> select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | 74df3399-91b8-11e9-be5f-000c299fdf40 | youxi3 | 3306 | ONLINE | | group_replication_applier | 8fbf8b41-84fe-11e9-897e-000c29f27e52 | youxi2 | 3306 | ONLINE | | group_replication_applier | c9e3662b-9020-11e9-94aa-000c29721e89 | youxi1 | 3306 | ONLINE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ 3 rows in set (0.01 sec)
注意:如果防火墙是打开的,记得添加mysql的端口号。
[root@youxi3 ~]# firewall-cmd --permanent --zone=public --add-port=3306/tcp success [root@youxi3 ~]# firewall-cmd --permanent --zone=public --add-port=33063/tcp success [root@youxi3 ~]# firewall-cmd --reload success [root@youxi3 ~]# firewall-cmd --zone=public --list-ports 3306/tcp 33063/tcp
4)测试
在youxi1上创建数据
mysql> create database test_db; Query OK, 1 row affected (0.00 sec) mysql> use test_db; Database changed mysql> create table user_tb(id int key,name varchar(20)); //id是主键,引擎默认是innodb Query OK, 0 rows affected (0.02 sec) mysql> insert into user_tb values(1,'zhangsan'); Query OK, 1 row affected (0.08 sec)
在youxi2上查看
mysql> select * from test_db.user_tb; +----+----------+ | id | name | +----+----------+ | 1 | zhangsan | +----+----------+ 1 row in set (0.00 sec)
在youxi3上查看
mysql> select * from test_db.user_tb; +----+----------+ | id | name | +----+----------+ | 1 | zhangsan | +----+----------+ 1 row in set (0.00 sec)
5)那么怎么区分主从服务器
MGR区分主从服务器使用show variables like '%read_only%';查看read_only相关参数。如果是主服务器(youxi1),会显示如下:
mysql> show variables like '%read_only%'; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | innodb_read_only | OFF | | read_only | OFF | | super_read_only | OFF | | transaction_read_only | OFF | | tx_read_only | OFF | +-----------------------+-------+ 5 rows in set (0.00 sec)
如果是从服务器(youxi2)会显示如下:
mysql> show variables like '%read_only%'; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | innodb_read_only | OFF | | read_only | ON | | super_read_only | ON | | transaction_read_only | OFF | | tx_read_only | OFF | +-----------------------+-------+ 5 rows in set (0.01 sec)
6)查看当前服务器的组复制集群参数设置列表
mysql> show variables like 'group_replication%'; +----------------------------------------------------+----------------------------------------+ | Variable_name | Value | +----------------------------------------------------+----------------------------------------+ | group_replication_allow_local_disjoint_gtids_join | OFF | | group_replication_allow_local_lower_version_join | OFF | | group_replication_auto_increment_increment | 7 | | group_replication_bootstrap_group | OFF | | group_replication_components_stop_timeout | 31536000 | | group_replication_compression_threshold | 1000000 | | group_replication_enforce_update_everywhere_checks | OFF | | group_replication_exit_state_action | READ_ONLY | | group_replication_flow_control_applier_threshold | 25000 | | group_replication_flow_control_certifier_threshold | 25000 | | group_replication_flow_control_mode | QUOTA | | group_replication_force_members | | | group_replication_group_name | ce9be252-2b71-11e6-b8f4-00212844f856 | | group_replication_group_seeds | youxi1:33061,youxi2:33062,youxi3:33063 | | group_replication_gtid_assignment_block_size | 1000000 | | group_replication_ip_whitelist | AUTOMATIC | | group_replication_local_address | youxi1:33061 | | group_replication_member_weight | 50 | | group_replication_poll_spin_loops | 0 | | group_replication_recovery_complete_at | TRANSACTIONS_APPLIED | | group_replication_recovery_reconnect_interval | 60 | | group_replication_recovery_retry_count | 10 | | group_replication_recovery_ssl_ca | | | group_replication_recovery_ssl_capath | | | group_replication_recovery_ssl_cert | | | group_replication_recovery_ssl_cipher | | | group_replication_recovery_ssl_crl | | | group_replication_recovery_ssl_crlpath | | | group_replication_recovery_ssl_key | | | group_replication_recovery_ssl_verify_server_cert | OFF | | group_replication_recovery_use_ssl | OFF | | group_replication_single_primary_mode | ON | | group_replication_ssl_mode | DISABLED | | group_replication_start_on_boot | OFF | | group_replication_transaction_size_limit | 0 | | group_replication_unreachable_majority_timeout | 0 | +----------------------------------------------------+----------------------------------------+ 36 rows in set (0.00 sec)
7)如果主服务器出问题,MGR会自动切换主服务器
将youxi1的mysqld人为关闭
[root@youxi1 ~]# systemctl stop mysqld
之后前往youxi2和youxi3查看谁是主服务器
mysql> show variables like '%read_only%'; //这是youxi2的 +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | innodb_read_only | OFF | | read_only | ON | | super_read_only | ON | | transaction_read_only | OFF | | tx_read_only | OFF | +-----------------------+-------+ 5 rows in set (0.01 sec) mysql> show variables like '%read_only%'; //这是youxi3的 +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | innodb_read_only | OFF | | read_only | OFF | | super_read_only | OFF | | transaction_read_only | OFF | | tx_read_only | OFF | +-----------------------+-------+ 5 rows in set (0.02 sec)
可以看到youxi3变成了主服务器,那么尝试在youxi3中插入数据
mysql> insert into test_db.user_tb values(2,'lisi'); Query OK, 1 row affected (0.14 sec)
再到youxi2中查看数据
mysql> select * from test_db.user_tb; +----+----------+ | id | name | +----+----------+ | 1 | zhangsan | | 2 | lisi | +----+----------+ 2 rows in set (0.00 sec)
(2).多主模式
1)在创建时就启用多主模式
在创建时就启用多主模式只需在修改配置文件/etc/my.cnf时多加两行参数。其余保持不变
group_replication_single_primary_mode=off //关闭单master模式 group_replication_enforce_update_everywhere_checks=ON //多主一致性检查
2)由单主模式改为多主模式
由单主改为多主时,一样需要所有服务器配置信息增加两行参数,为了下次开启就是多主模式
group_replication_single_primary_mode=off //关闭单master模式 group_replication_enforce_update_everywhere_checks=ON //开启多主一致性检查
然后全部服务器停止组复制(GROUP_REPLICATION)集群,并设置参数
mysql> stop GROUP_REPLICATION; Query OK, 0 rows affected (9.51 sec) mysql> set global group_replication_single_primary_mode=off; //关闭单主模式 Query OK, 0 rows affected (0.00 sec) mysql> set global group_replication_enforce_update_everywhere_checks=ON; //开启多主一致性检查 Query OK, 0 rows affected (0.00 sec)
选择其中一台引导组复制(GROUP_REPLICATION)集群
mysql> SET GLOBAL group_replication_bootstrap_group=ON; Query OK, 0 rows affected (0.00 sec) mysql> START GROUP_REPLICATION; Query OK, 0 rows affected (2.13 sec) mysql> SET GLOBAL group_replication_bootstrap_group=OFF; Query OK, 0 rows affected (0.01 sec)
剩下的开启组复制(GROUP_REPLICATION)即可
mysql> start group_replication; Query OK, 0 rows affected, 1 warning (6.05 sec)
最后查看非引导组复制的服务器
mysql> select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | 74df3399-91b8-11e9-be5f-000c299fdf40 | youxi3 | 3306 | ONLINE | | group_replication_applier | 8fbf8b41-84fe-11e9-897e-000c29f27e52 | youxi2 | 3306 | ONLINE | | group_replication_applier | c9e3662b-9020-11e9-94aa-000c29721e89 | youxi1 | 3306 | ONLINE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ 3 rows in set (0.00 sec) mysql> show variables like '%read_only%'; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | innodb_read_only | OFF | | read_only | OFF | | super_read_only | OFF | | transaction_read_only | OFF | | tx_read_only | OFF | +-----------------------+-------+ 5 rows in set (0.01 sec)
(3).如果宕机了
(后期补)