前面一篇文章簡要介紹了mysql組複製的工做流程、先決條件與限制條件,以及同傳統的異步複製集半同步複製的特色進行對比。本文將介紹mysql組複製的具體配置過程。html
1、環境介紹mysql
操做系統版本:centos linux 7.2 64bitlinux
Mysql版本:mysql-5.7.19-linux-glibc2.12-x86_64 sql
3臺服務器hosts文件(其中vm2做爲mysql組複製的第一臺主機,一般第一臺啓動的爲master)數據庫
# cat /etc/hostsbootstrap
192.168.115.5 vm1centos
192.168.115.6 vm2服務器
192.168.115.7 vm3app
2、配置vm2的my.cnf文件異步
server-id=1155 lower_case_table_names = 1 skip-name-resolve innodb_file_per_table=1 gtid_mode = ON enforce_gtid_consistency = ON slave_parallel_workers=4 master_verify_checksum = 1 slave_sql_verify_checksum = 1 log-slave-updates=true sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES slave-parallel-type=LOGICAL_CLOCK slave-preserve-commit-order=ON master_info_repository = TABLE relay_log_info_repository = TABLE binlog_checksum = NONE transaction_write_set_extraction = XXHASH64 loose-group_replication_group_name = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' loose-group_replication_start_on_boot = off loose-group_replication_local_address = 'vm2:33306' loose-group_replication_group_seeds ='vm1:33306,vm2:33306,vm3:33306' loose-group_replication_bootstrap_group = off
各項配置參數的具體含義可參考mysql手冊(mysql的安裝部署本文省略,具體也可參考mysql手冊):
https://dev.mysql.com/doc/refman/5.7/en/group-replication-configuring-instances.html
3、在vm2上建立複製帳號及權限配置
mysql> set SQL_LOG_BIN=0; Query OK, 0 rows affected (0.01 sec) mysql> create user repl@'%' identified by "123456"; Query OK, 0 rows affected (0.00 sec) mysql> grant replication slave on *.* to repl@'%'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> set SQL_LOG_BIN=1; Query OK, 0 rows affected (0.00 sec) mysql> change master to master_user='repl',master_password='123456' for channel 'group_replication_recovery'; Query OK, 0 rows affected, 2 warnings (0.08 sec) mysql> install PLUGIN group_replication SONAME 'group_replication.so'; Query OK, 0 rows affected (0.53 sec)
4、啓動vm2上的group replication進程
mysql> set global group_replication_bootstrap_group=ON; Query OK, 0 rows affected (0.01 sec) mysql> start group_replication; Query OK, 0 rows affected (2.66 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 | 33025907-6d2d-11e7-8ce6-000c29e07281 | vm2 | 3306 | ONLINE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ 1 row in set (0.00 sec)
5、按照VM2相同的方法配置VM3,注意my.cnf文件中的server-id須要修改
mysql> set SQL_LOG_BIN=0; Query OK, 0 rows affected (0.00 sec) mysql> create user repl@'%' identified by "123456"; Query OK, 0 rows affected (0.01 sec) mysql> grant replication slave on *.* to repl@'%'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> set SQL_LOG_BIN=1; Query OK, 0 rows affected (0.00 sec) mysql> change master to master_user='repl',master_password='123456' for channel 'group_replication_recovery'; Query OK, 0 rows affected, 2 warnings (0.05 sec) mysql> install PLUGIN group_replication SONAME 'group_replication.so'; Query OK, 0 rows affected (0.38 sec) mysql> set global group_replication_allow_local_disjoint_gtids_join=ON; Query OK, 0 rows affected (0.00 sec) mysql> start group_replication; Query OK, 0 rows affected (7.89 sec) mysql> select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ | group_replication_applier | 33025907-6d2d-11e7-8ce6-000c29e07281 | vm2 | 3306 | ONLINE | | group_replication_applier | 35678d62-6d2d-11e7-80a7-000c2909332c | vm3 | 3306 | ONLINE | | group_replication_applier | 8b643791-6d30-11e7-986a-000c29d53b31 | vm1 | 3306 | ONLINE | +---------------------------+--------------------------------------+-------------+-------------+--------------+ 3 rows in set (0.00 sec)
這裏面須要注意的是後面加入組複製的vm3和vm1,須要配置group_replication_allow_local_disjoint_gtids_join參數爲ON,據文檔描述開啓此參數的含義爲假如當前的數據庫上的事務在組複製中不存在,同樣容許加入組複製。