组复制可以在两种模式下运行。
1.在单主模式下,组复制具有自动选主功能,每次只有一个 server成员接受更新。
2.在多主模式下,所有的 server 成员都可以同时接受更新.
组复制与异步主从复制区别.
1.传统mysql主从复制,是在主节点执行和提交事务,然后把他们异步的发送到从节点,行复制的重新执行主节点的SQL语句,这是一个 shared-nothing 的系统,默认情况下所有 server 成员都有一个完整的数据副本。
2.半同步复制,它在协议中添加了一个同步步骤。 这意味着主节点在提交时需要等待从节点确认它已经接收到事务。只有这样,主节点才能继续提交操作。
3.MySQL 组复制实现了基于复制协议的多主更新。
1)复制组由多个 server成员构成,并且组中的每个 server 成员可以独立地执行事务。但所有读写(RW)事务只有在冲突检测成功后才会提交。只读(RO)事务不需要在冲突检测,可以立即提交。
2)换句话说,对于任何 RW 事务,提交操作并不是由始发 server 单向决定的,而是由组来决定是否提交。准确地说,在始发 server 上,当事务准备好提交时,该 server 会广播写入值(已改变的行)和对应的写入集(已更新的行的唯一标识符)。然后会为该事务建立一个全局的顺序。最终,这意味着所有 server 成员以相同的顺序接收同一组事务。因此,所有 server 成员以相同的顺序应用相同的更改,以确保组内一致。
3)组复制使您能够根据在一组 server 中复制系统的状态来创建具有冗余的容错系统。因此,只要它不是全部或多数 server 发生故障,即使有一些 server 故障,系统仍然可用,最多只是性能和可伸缩性降低,但它仍然可用。server 故障是孤立并且独立的。它们由组成员服务来监控,组成员服务依赖于分布式故障检测系统,其能够在任何 server 自愿地或由于意外停止而离开组时发出信号。
4)他们是由一个分布式恢复程序来确保当有 server 加入组时,它们会自动更新组信息到最新。并且多主更新确保了即使在单个服务器故障的情况下也不会阻止更新,不必进行 server故障转移。因此,MySQL 组复制保证数据库服务持续可用。
5)值得注意的一点是,尽管数据库服务可用,但当有一个 server 崩溃时,连接到它的客户端必须定向或故障转移到不同的 server。
这不是组复制要解决的问题。连接器,负载均衡器,路由器或其他形式的中间件更适合处理这个问题。
总之,MySQL 组复制提供了高可用性,高弹性,可靠的 MySQL 服务。
MGR的限制
仅支持InnoDB表,并且每张表一定要有一个主键,用于做write set的冲突检测;
必须打开GTID特性,二进制日志格式必须设置为ROW,用于选主与write set
COMMIT可能会导致失败,类似于快照事务隔离级别的失败场景
目前一个MGR集群最多支持9个节点
不支持外键于save point特性,无法做全局间的约束检测与部分部分回滚
二进制日志不支持binlog event checksum
下面展示,Mysql组复制之多主模式
安装mysql 5.7.18
在三台db服务器上面设置/etc/hosts映射,如下:
cat /etc/hosts
192.168.1.88 mgr88
192.168.1.89 mgr89
192.168.1.90 mgr90
server*:
组内每台主机,都需要先安装组复制插件.否则会导致启动失败.
mysql>INSTALL PLUGIN group_replication SONAME 'group_replication.so';
server* (88,89,90)
[client] port = 3306 socket = /usr/local/mysql/tmp/mysql.sock [mysqld] port = 3306 socket = /usr/local/mysql/tmp/mysql.sock back_log = 80 basedir = /usr/local/mysql tmpdir = /tmp datadir = /usr/local/mysql/data #-------------------gobal variables------------# 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 = /usr/local/mysql/log/mysql-bin transaction_write_set_extraction = XXHASH64 #以便在server收集写集合的同时将其记录到二进制日志。写集合基于每行的主键,并且是行更改后的唯一标识此标识将用于检测冲突。 loose-group_replication_group_name = 'ce9be252-2b71-11e6-b8f4-00212889f856' #组的名字可以随便起,但不能用主机的GTID loose-group_replication_start_on_boot = off #为了避免每次启动自动引导具有相同名称的第二个组,所以设置为OFF。 loose-group_replication_bootstrap_group = off #同上 loose-group_replication_local_address = '192.168.1.88:33061' #写自己主机所在IP loose-group_replication_group_seeds ='192.168.1.88:33061,192.168.1.89:33061,192.168.1.90:33061' loose-group_replication_single_primary_mode = off #关闭单主模式的参数 loose-group_replication_enforce_update_everywhere_checks = on #开启多主模式的参数 max_connect_errors = 20000 max_connections = 2000 wait_timeout = 3600 interactive_timeout = 3600 net_read_timeout = 3600 net_write_timeout = 3600 table_open_cache = 1024 table_definition_cache = 1024 thread_cache_size = 512 open_files_limit = 10000 character-set-server = utf8 collation-server = utf8_bin skip_external_locking performance_schema = 1 user = mysql myisam_recover_options = DEFAULT skip-name-resolve local_infile = 0 lower_case_table_names = 0 #--------------------innoDB------------# innodb_buffer_pool_size = 2000M innodb_data_file_path = ibdata1:200M:autoextend innodb_flush_log_at_trx_commit = 1 innodb_io_capacity = 600 innodb_lock_wait_timeout = 120 innodb_log_buffer_size = 8M innodb_log_file_size = 200M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 85 innodb_read_io_threads = 8 innodb_write_io_threads = 8 innodb_support_xa = 1 innodb_thread_concurrency = 32 innodb_file_per_table innodb_rollback_on_timeout #------------session variables-------# join_buffer_size = 8M key_buffer_size = 256M bulk_insert_buffer_size = 8M max_heap_table_size = 96M tmp_table_size = 96M read_buffer_size = 8M sort_buffer_size = 2M max_allowed_packet = 64M read_rnd_buffer_size = 32M #------------MySQL Log----------------# log-bin = my3306-bin binlog_format = row #组复制依赖基于行的复制格式 sync_binlog = 1 expire_logs_days = 15 max_binlog_cache_size = 128M max_binlog_size = 500M binlog_cache_size = 64k slow_query_log log-slow-admin-statements log_warnings = 1 long_query_time = 0.25 #---------------replicate--------------# relay-log-index = relay3306.index relay-log = relay3306 server-id =88 #每个db的id唯一 init_slave = 'set sql_mode=STRICT_ALL_TABLES' log-slave-updates [myisamchk] key_buffer = 512M sort_buffer_size = 512M read_buffer = 8M write_buffer = 8M [mysqlhotcopy] interactive-timeout [mysqld_safe] open-files-limit = 8192 log-error = /usr/local/mysql/log/mysqld_error.log
server88:
mysql>SET GLOBAL group_replication_bootstrap_group=ON;
mysql> CHANGE MASTER TO MASTER_USER='repl',MASTER_PASSWORD='repl' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.27 sec)
mysql> set global group_replication_ip_whitelist="127.0.0.1/32,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,192.168.1.0/24";
Query OK, 0 rows affected (0.00 sec)
#这句只有server88,在第一次执行引导组的时候执行.
mysql>GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.%' IDENTIFIED BY 'repl';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CHANGE MASTER TO MASTER_USER='repl',MASTER_PASSWORD='repl' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.27 sec)
mysql> set global group_replication_ip_whitelist="127.0.0.1/32,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,192.168.1.0/24";
Query OK, 0 rows affected (0.00 sec)
mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (1.32 sec)
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 715e9af4-6c93-11e7-aabf-000c291abb1f | mgr88 | 3306 | ONLINE
+---------------------------+--------------------------------------+-------------+-------------+--------------+
1 row in set (0.05 sec)
配置 group_replication_recovery 通道的恢复凭据
server 89,90:
mysql> CHANGE MASTER TO MASTER_USER='repl',MASTER_PASSWORD='repl' FOR CHANNEL 'group_replication_recovery';
Query OK, 0 rows affected, 2 warnings (0.56 sec)
mysql> set global group_replication_ip_whitelist="127.0.0.1/32,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,192.168.1.0/24";
Query OK, 0 rows affected (0.01 sec)
Query OK, 0 rows affected, 2 warnings (0.56 sec)
mysql> set global group_replication_ip_whitelist="127.0.0.1/32,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,192.168.1.0/24";
Query OK, 0 rows affected (0.01 sec)
mysql> set global group_replication_allow_local_disjoint_gtids_join=ON;
Query OK, 0 rows affected (0.01 sec)
mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (4.02 sec)
Query OK, 0 rows affected (0.01 sec)
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 | 715e9af4-6c93-11e7-aabf-000c291abb1f | mgr88 | 3306 | ONLINE
| group_replication_applier | f308d2d6-6c93-11e7-a5b1-000c2979c7bf | mgr90 | 3306 | ONLINE
| group_replication_applier | f39c169e-6c93-11e7-a550-000c291d7bdb | mgr89 | 3306 | ONLINE
+---------------------------+--------------------------------------+-------------+-------------+--------------+
3 rows in set (0.00 sec)
检验
1: server88、89、90(分别创建test1、test2和test3,t1、t2、t3测试)
mysql> create database test;
Query OK, 1 row affected (0.15 sec)
mysql> use test;
Database changed
mysql> CREATE TABLE t1 (c1 INT PRIMARY KEY, c2 varchar(20));
Query OK, 0 rows affected (0.94 sec)mysql> INSERT INTO t1 VALUES (1, 'lalala');
Query OK, 1 row affected (0.46 sec)
mysql> select * from t1;
+----+--------+
| c1 | c2 |
+----+--------+
| 1 | lalala |
+----+--------+
1 row in set (0.00 sec)
在server89,90 可以看见组复制,同步过来的数
可以在performance_schema中看见组成员
2:模拟宕机一个节点验证
(1)关闭88的数据库实例
(2)在89节点操作
查询组成员,发现88已不在组中
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | f308d2d6-6c93-11e7-a5b1-000c2979c7bf | mgr90 | 3306 | ONLINE |
| group_replication_applier | f39c169e-6c93-11e7-a550-000c291d7bdb | mgr89 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
2 rows in set (0.00 sec)
(3)写入数据
mysql> insert into t1 values(2,'Mr.Li');
Query OK, 1 row affected (0.02 sec)
mysql> select * from t1;
+----+----------+
| c1 | c2 |
+----+----------+
| 1 | lalala |
| 2 | Mr.Li |
+----+----------+
5 rows in set (0.00 sec)
(4)启动88的数据库
修改配置文件,自动启动组复制
loose-group_replication_start_on_boot = on
group_replication_allow_local_disjoint_gtids_join = on
(5)再次查看组成员,发现88已重新加入组
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 715e9af4-6c93-11e7-aabf-000c291abb1f | mgr88 | 3306 | ONLINE |
| group_replication_applier | f308d2d6-6c93-11e7-a5b1-000c2979c7bf | mgr90 | 3306 | ONLINE |
| group_replication_applier | f39c169e-6c93-11e7-a550-000c291d7bdb | mgr89 | 3306 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
3 rows in set (0.00 sec)
(6)查看数据,发现数据已同步
mysql> use test1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t1;
+----+----------+
| c1 | c2 |
+----+----------+
| 1 | lalala |
| 2 | Mr.Li |
+----+----------+
5 rows in set (0.00 sec)
|
1:没有设置白名单网段:需要添加自己所在网段.
报错:'[GCS] The member is leaving a group without being on one.'
解决:set global group_replication_ip_whitelist="127.0.0.1/32,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,192.168.1.0/24"; START GROUP_REPLICATION;
2:ERROR 3092 (HY000): The server is not configured properly to be an active member of the group. Please see more details on error log.1
解决方案:
根据提示打开group_replication_allow_local_disjoint_gtids_join选项,mysql命令行执行:
mysql> set global group_replication_allow_local_disjoint_gtids_join=ON;
再次启动组复制
mysql> START GROUP_REPLICATION;