MySQL 5.7.17 Group Relication(組複製)搭建手冊

神諭丶發表於2017-02-09
本博文介紹了Group Replication的兩種工作模式的架構。
並詳細介紹了
Single-Master Mode的部署過程,以及如何切換到Multi-Master Mode
當然,文末給出了Group Replication的配置要求和一些限制。



〇 結構介紹

在2016年12月釋出的5.7.17版本的MySQL,甲骨文宣佈Group Replication已經GA。

Group Replication(下簡稱GR)有兩個工作模式,分別為S
ingle-Master Mode與Multi-Master Mode:


Single-Master Modefailover圖:

只有primary成員可讀寫,而其他的節點為只讀,在primary成員發生故障時,將會有其他成員頂替成primary。


Multi-Master Mode的failover圖:



所有的成員均可讀可寫。


關於腦裂問題,可發現MySQL Group Replication與MongoDB Relicate Set有相似之處:

和MongoDB的Relicate Set相近,其讀寫庫類似於Primary,只讀庫類似於Secondary。

(下圖來自MySQL官方)


(下圖來自MongoDB 3.4 Manual)







〇 部署

測試例項數量:3臺
版本:MySQL 5.7.17


安裝(此處用的是二進位制包安裝)
mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz


建立資料目錄及日誌目錄:
  1. mkdir -p /data/mysql57/mysql3306
  2. mkdir -p /data/mysql57/mysql3307
  3. mkdir -p /data/mysql57/mysql3308
  4. mkdir -p /data/mysql57/logs/3306
  5. mkdir -p /data/mysql57/logs/3307
  6. mkdir -p /data/mysql57/logs/3308


解壓二進位制包並將其設定為basedir:
  1. mv mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz /data/mysql57
  2. cd /data/mysql57
  3. tar zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
  4. mv mysql-5.7.17-linux-glibc2.5-x86_64 mysql-basedir



〇 新增第一臺例項:
新增3306例項的配置檔案:
  1. vim /data/mysql57/mysql3306/3306.cnf

  2. [client]
  3. prompt = "(\u@\h) [\d]> "

  4. [mysqld]
  5. server_id = 3306
  6. user = mysql
  7. port = 3306
  8. socket = /tmp/mysql3306.sock
  9. basedir = /data/mysql57/mysql-basedir
  10. datadir = /data/mysql57/mysql3306
  11. pid-file = /data/mysql57/mysql3306/3306.pid
  12. log-error = /data/mysql57/logs/3306/error-log
  13. log-bin = /data/mysql57/logs/3306/binlog
  14. log-bin-index = /data/mysql57/logs/3306/binlog.index
  15. relay-log = /data/mysql57/logs/3306/relaylog
  16. relay-log-index = /data/mysql57/logs/3306/relaylog.index

  17. master_info_repository = TABLE
  18. relay_log_info_repository = TABLE
  19. log_slave_updates = ON
  20. binlog_checksum = NONE
  21. binlog_format = ROW
  22. transaction_isolation = READ-COMMITTED
  23. gtid_mode = ON
  24. enforce_gtid_consistency = ON

  25. # GR 配置項 其中loose字首表示若Group Replication plugin未載入 mysql server仍繼續啟動
  26. transaction_write_set_extraction = XXHASH64
  27. loose-group_replication_group_name = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"    # 組名,此處可拿select uuid();生成
  28. loose-group_replication_start_on_boot = off    # 在mysqld啟動時不自動啟動組複製
  29. loose-group_replication_local_address = "127.0.0.1:24901"
  30. loose-group_replication_group_seeds = "127.0.0.1:24901,127.0.0.1:24902,127.0.0.1:24903"    
  31. loose-group_replication_bootstrap_group = off    


初始化3306例項datadir:
  1. /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3306.cnf --initialize-insecure


啟動3306例項:
  1. /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3306.cnf &


透過MySQL Client進入第一個例項(密碼為空)
  1. /data/mysql57/mysql-basedir/bin/mysql -uroot -p -S /tmp/mysql3306.sock


建立複製使用者與授權,並讓其作為group的第一個成員:
  1. SET SQL_LOG_BIN=0;
  2. CREATE USER repl@'%';
  3. GRANT REPLICATION SLAVE ON *.* TO repl@'%' IDENTIFIED BY 'repl@123';
  4. FLUSH PRIVILEGES;
  5. SET SQL_LOG_BIN=1;
  6. CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='repl@123' FOR CHANNEL 'group_replication_recovery';


安裝GR外掛:
  1. (root@localhost) [(none)]> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
  2. Query OK, 0 rows affected (0.07 sec)


可以檢查一下是否安裝成功:
  1. (root@localhost) [(none)]> SELECT * FROM information_schema.plugins WHERE PLUGIN_NAME LIKE '%group%'\G
  2. *************************** 1. row ***************************
  3.            PLUGIN_NAME: group_replication
  4.         PLUGIN_VERSION: 1.0
  5.          PLUGIN_STATUS: ACTIVE
  6.            PLUGIN_TYPE: GROUP REPLICATION
  7.    PLUGIN_TYPE_VERSION: 1.1
  8.         PLUGIN_LIBRARY: group_replication.so
  9. PLUGIN_LIBRARY_VERSION: 1.7
  10.          PLUGIN_AUTHOR: ORACLE
  11.     PLUGIN_DESCRIPTION: Group Replication (1.0.0)
  12.         PLUGIN_LICENSE: GPL
  13.            LOAD_OPTION: ON
  14. 1 row in set (0.00 sec)


開啟第一個組複製:
  1. (root@localhost) [(none)]> SET GLOBAL group_replication_bootstrap_group=ON;
  2. Query OK, 0 rows affected (0.00 sec)

  3. (root@localhost) [(none)]> START GROUP_REPLICATION;
  4. Query OK, 0 rows affected (1.20 sec)

  5. # 啟動組複製之後將group_replication_bootstrap_group設定為OFF
  6. # This option must only be set on one server and only when starting the group for the first time or restarting the entire group. After the group has been bootstrapped, set this option to OFF. It should be set to OFF both dynamically and in the configuration files

  7. (root@localhost) [(none)]> SET GLOBAL group_replication_bootstrap_group=OFF;
  8. Query OK, 0 rows affected (0.01 sec)


檢查一下組複製成員,其中member_id就是@@server_uuid的值
  1. (root@localhost) [(none)]> SELECT * FROM performance_schema.replication_group_members;
  2. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  3. | CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
  4. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  5. | group_replication_applier | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y      | 3306        | ONLINE       |
  6. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  7. 1 row in set (0.00 sec)


新增測試資料:
  1. (root@localhost) [(none)]> CREATE DATABASE test;
  2. Query OK, 1 row affected (0.00 sec)

  3. (root@localhost) [(none)]> USE test;
  4. Database changed

  5. (root@localhost) [test]> CREATE TABLE tb_test(id int PRIMARY KEY, name varchar(20)) CHARACTER SET utf8;
  6. Query OK, 0 rows affected (0.04 sec)

  7. (root@localhost) [test]> INSERT INTO tb_test VALUES(1,'風暴之靈');
  8. Query OK, 1 row affected (0.00 sec)

  9. (root@localhost) [test]> INSERT INTO tb_test VALUES(2,'影之靈龕');
  10. Query OK, 1 row affected (0.01 sec)

  11. (root@localhost) [test]> UPDATE tb_test SET name='斯嘉蒂之眼' WHERE id=2;
  12. Query OK, 1 row affected (0.02 sec)
  13. Rows matched: 1 Changed: 1 Warnings: 0

  14. (root@localhost) [test]> SELECT * FROM tb_test;
  15. +----+-----------------+
  16. | id | name            |
  17. +----+-----------------+
  18. | 1  | 風暴之靈         |
  19. | 2  | 斯嘉蒂之眼       |
  20. +----+-----------------+
  21. 2 rows in set (0.00 sec)


新增第二個例項(3307)
修改3307配置檔案,將3306改成3307,並且將loose-group_replication_local_address的埠從24901改成24902
  1. cp /data/mysql57/3306.cnf /data/mysql57/3307.cnf
  2. sed -i "s/3306/3307/g" /data/mysql57/3307.cnf
  3. sed -i "s/24901\"/24902\"/g" 3307.cnf


初始化3307例項:
  1. /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3307.cnf --initialize-insecure


啟動3307例項:
  1. /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3307.cnf &


透過MySQL Client進入3307例項:
  1. /data/mysql57/mysql-basedir/bin/mysql -uroot -p -S /tmp/mysql3307.sock


重複在3306例項的操作:
  1. SET SQL_LOG_BIN=0;
  2. CREATE USER repl@'%';
  3. GRANT REPLICATION SLAVE ON *.* TO repl@'%' IDENTIFIED BY 'repl@123';
  4. FLUSH PRIVILEGES;
  5. SET SQL_LOG_BIN=1;
  6. CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='repl@123' FOR CHANNEL 'group_replication_recovery';


在3307例項上安裝GR外掛,開啟組複製:
  1. (root@localhost) [(none)]> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
  2. Query OK, 0 rows affected (0.00 sec)

  3. (root@localhost) [(none)]> START GROUP_REPLICATION;
  4. Query OK, 0 rows affected (5.61 sec)


檢查一下成員狀態:
  1. (root@localhost) [(none)]> SELECT * FROM performance_schema.replication_group_members;
  2. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  3. | CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
  4. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  5. | group_replication_applier | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y      | 3306        | ONLINE       |
  6. | group_replication_applier | 6bddae0d-eca1-11e6-b9d5-000c297f23b7 | sAno1y      | 3307        | RECOVERING   |
  7. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  8. 2 rows in set (0.00 sec)


過了一陣子再檢查,仍然是RECOVERING。


再過一陣子檢查,發現member_state被置為ERROR:
  1. (root@localhost) [(none)]> SELECT * FROM performance_schema.replication_group_members;
  2. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  3. | CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
  4. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  5. | group_replication_applier | 6bddae0d-eca1-11e6-b9d5-000c297f23b7 | sAno1y      | 3307        | ERROR        |
  6. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  7. 1 row in set (0.01 sec)


此時檢查3306例項的組複製情況,發現檢查不到另一個例項的資訊了:
  1. (root@localhost) [(none)]> SELECT * FROM performance_schema.replication_group_members;
  2. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  3. | CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
  4. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  5. | group_replication_applier | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y      | 3306        | ONLINE       |
  6. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  7. 1 row in set (0.00 sec)


開多一個終端,檢查3307例項的error log發現:
  1. 2017-02-07T12:06:21.674093Z 47 [ERROR] Slave I/O for channel 'group_replication_recovery': error connecting to master 'repl@sAno1y:3306' - retry-time: 60 retries: 1, Error_code: 2005


應該是解析的問題,修改hosts檔案,在末尾加上主機名:
  1. [root@sAno1y 3307]# hostname
  2. sAno1y
  3. [root@sAno1y 3307]# cat /etc/hosts
  4. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 sAno1y
  5. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 sAno1y


重新操作3307例項:
  1. (root@localhost) [(none)]> STOP GROUP_REPLICATION;
  2. Query OK, 0 rows affected (0.00 sec)

  3. (root@localhost) [(none)]> START GROUP_REPLICATION;
  4. Query OK, 0 rows affected (7.80 sec)


檢查組複製狀態,發現兩個例項的狀態均為ONLINE了:
  1. (root@localhost) [(none)]> SELECT * FROM performance_schema.replication_group_members;
  2. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  3. | CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
  4. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  5. | group_replication_applier | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y      | 3306        | ONLINE       |
  6. | group_replication_applier | 6bddae0d-eca1-11e6-b9d5-000c297f23b7 | sAno1y      | 3307        | ONLINE       |
  7. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  8. 2 rows in set (0.00 sec)


在3307上檢查一下同步狀態:
  1. (root@localhost) [(none)]> SELECT * FROM test.tb_test;
  2. +----+-----------------+
  3. | id | name            |
  4. +----+-----------------+
  5. | 1  | 風暴之靈         |
  6. | 2  | 斯嘉蒂之眼       |
  7. +----+-----------------+
  8. 2 rows in set (0.00 sec)


新增3308例項:
修改3308配置檔案:

  1. cp /data/mysql57/3306.cnf /data/mysql57/3308.cnf
  2. sed -i "s/3306/3308/g" 3308.cnf
  3. sed -i "s/24901\"/24093\"/g" 3308.cnf


然後初始化並啟動3308例項:
  1. /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3308.cnf --initialize-insecure
  2. /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3308.cnf &


同樣進入3308例項:
  1. /data/mysql57/mysql-basedir/bin/mysql -uroot -p -S /tmp/mysql3308.sock


在3308例項上重複操作:

  1. SET SQL_LOG_BIN=0;
  2. CREATE USER repl@'%';
  3. GRANT REPLICATION SLAVE ON *.* TO repl@'%' IDENTIFIED BY 'repl@123';
  4. FLUSH PRIVILEGES;
  5. SET SQL_LOG_BIN=1;
  6. CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='repl@123' FOR CHANNEL 'group_replication_recovery';


繼續重複操作,安裝GR外掛並啟動它:
  1. INSTALL PLUGIN group_replication SONAME 'group_replication.so';
  2. START GROUP_REPLICATION;


最後再檢查一下組複製成員的狀態:
  1. (root@localhost) [(none)]> SELECT * FROM performance_schema.replication_group_members;
  2. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  3. | CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
  4. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  5. | group_replication_applier | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y      | 3306        | ONLINE       |
  6. | group_replication_applier | 6bddae0d-eca1-11e6-b9d5-000c297f23b7 | sAno1y      | 3307        | ONLINE       |
  7. | group_replication_applier | 6ce8c980-ed4a-11e6-bf00-000c297f23b7 | sAno1y      | 3308        | ONLINE       |
  8. +---------------------------+--------------------------------------+-------------+-------------+--------------+
  9. 3 rows in set (0.00 sec)


當然在3308例項上也已將3306的事務apply過來了:
  1. (root@localhost) [(none)]> SELECT * FROM test.tb_test;
  2. +----+-----------------+
  3. | id | name            |
  4. +----+-----------------+
  5. | 1  | 風暴之靈         |
  6. | 2  | 斯嘉蒂之眼       |
  7. +----+-----------------+
  8. 2 rows in set (0.00 sec)


root@localhost使用者在上述操作中為空密碼,可以給root@localhost加個密碼……
因為三個例項都在一個GR組裡,所以對3306例項操作就行了:
  1. /data/mysql57/mysql-basedir/bin/mysql -uroot -p -S /tmp/mysql3306.sock -e "ALTER USER root@localhost IDENTIFIED BY 'root123';"


當然ALTER操作會被記錄到3306的binlog裡,並
同步到3307和3308例項上。



可以檢視一下三臺例項的read_only和super-read-only值:
  1. [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3306.sock -e "SELECT @@read_only, @@super_read_only";
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. +-------------+-------------------+
  4. | @@read_only | @@super_read_only  |
  5. +-------------+-------------------+
  6. | 0           | 0                 |
  7. +-------------+-------------------+

  8. [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "SELECT @@read_only, @@super_read_only";
  9. mysql: [Warning] Using a password on the command line interface can be insecure.
  10. +-------------+-------------------+
  11. | @@read_only | @@super_read_only  |
  12. +-------------+-------------------+
  13. | 1           | 1                 |
  14. +-------------+-------------------+

  15. [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3308.sock -e "SELECT @@read_only, @@super_read_only";
  16. mysql: [Warning] Using a password on the command line interface can be insecure.
  17. +-------------+-------------------+
  18. | @@read_only | @@super_read_only  |
  19. +-------------+-------------------+
  20. | 1           | 1                 |
  21. +-------------+-------------------+


可以發現只有3306例項也就是第一個例項屬於可寫例項,而3307和3308均為read-only模式。
決定因素為第一個加入該GR組的成員,之後加入該GR組的均為ro,在該模式與MongoDB Replicate Set很相似。


當然如果要確定哪一個成員是primary,可以在三個成員中的任意一個執行:
  1. SELECT b.member_id, b.member_host, b.member_port
  2. FROM performance_schema.global_status a
  3. JOIN performance_schema.replication_group_members b
  4. ON a.variable_value = b.member_id
  5. WHERE a.variable_name= 'group_replication_primary_member';

  6. # 輸出結果:
  7. +--------------------------------------+-------------+-------------+
  8. | member_id                            | member_host | member_port |
  9. +--------------------------------------+-------------+-------------+
  10. | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y      | 3306        |
  11. +--------------------------------------+-------------+-------------+
  12. 1 row in set (0.00 sec)


至此,Group Replication預設的single-master mode已經搭建完畢。




〇 將Single-Master Mode修改為Multi-Master Mode

如果要將Single-Master Mode修改為Multi-Master Mode,也比較簡單。
考慮到此時的Primary成員是3306,並且假定3306例項在對外提供寫服務,我這邊的操作如下:


首先停掉兩個secondary的組複製,在3307和3308例項上操作
  1. STOP GROUP_REPLICATION;
  2. SET GLOBAL group_replication_single_primary_mode=FALSE;
  3. SET GLOBAL group_replication_enforce_update_everywhere_checks=TRUE;


再在3306例項上重複以上操作:
  1. STOP GROUP_REPLICATION;
  2. SET GLOBAL group_replication_single_primary_mode=FALSE;
  3. SET GLOBAL group_replication_enforce_update_everywhere_checks=TRUE;


然後在3306上作為第一個成員啟動組複製:
  1. (root@localhost) [(none)]> SET GLOBAL group_replication_bootstrap_group=on;
  2. Query OK, 0 rows affected (0.00 sec)

  3. (root@localhost) [(none)]> START GROUP_REPLICATION;
  4. Query OK, 0 rows affected (1.05 sec)

  5. (root@localhost) [(none)]> SET GLOBAL group_replication_bootstrap_group=off;
  6. Query OK, 0 rows affected (0.00 sec)


在停啟組複製的過程中,3306例項仍對外提供服務,此處模擬修改:
  1. (root@localhost) [(none)]> UPDATE test.tb_test SET name='靈魂守衛' WHERE id=1;
  2. Query OK, 1 row affected (0.02 sec)
  3. Rows matched: 1 Changed: 1 Warnings: 0

  4. (root@localhost) [(none)]> INSERT INTO test.tb_test VALUES(3,'幻影斧');
  5. Query OK, 1 row affected (0.00 sec)

  6. (root@localhost) [(none)]> SELECT * FROM test.tb_test;
  7. +----+-----------------+
  8. | id | name            |
  9. +----+-----------------+
  10. | 1  | 靈魂守衛         |
  11. | 2  | 斯嘉蒂之眼       |
  12. | 3  | 幻影斧           |
  13. +----+-----------------+
  14. 3 rows in set (0.00 sec)


再3307和3308兩個例項上分別開啟組複製:
  1. START GROUP_REPLICATION;


檢查3307和3308是否將3306的事務應用過來
  1. [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "START GROUP_REPLICATION;"
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. [root@sAno1y mysql57]#
  4. [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3308.sock -e "START GROUP_REPLICATION;"
  5. mysql: [Warning] Using a password on the command line interface can be insecure.
  6. [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "SELECT * FROM test.tb_test;"
  7. mysql: [Warning] Using a password on the command line interface can be insecure.
  8. +----+-----------------+
  9. | id | name            |
  10. +----+-----------------+
  11. | 1  | 靈魂守衛         |
  12. | 2  | 斯嘉蒂之眼       |
  13. | 3  | 幻影斧           |
  14. +----+-----------------+
  15. [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3308.sock -e "SELECT * FROM test.tb_test;"
  16. mysql: [Warning] Using a password on the command line interface can be insecure.
  17. +----+-----------------+
  18. | id | name            |
  19. +----+-----------------+
  20. | 1  | 靈魂守衛         |
  21. | 2  | 斯嘉蒂之眼       |
  22. | 3  | 幻影斧           |
  23. +----+-----------------+
當然可以看到,在3306上做的修改,在3307和3308開啟組複製之後也已經同步過來了。


那麼再檢查一下3307和3308是否可寫:
  1. [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "SELECT @@read_only, @@super_read_only;"
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. +-------------+-------------------+
  4. | @@read_only | @@super_read_only |
  5. +-------------+-------------------+
  6. | 0           | 0                 |
  7. +-------------+-------------------+
  8. [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3308.sock -e "SELECT @@read_only, @@super_read_only;"
  9. mysql: [Warning] Using a password on the command line interface can be insecure.
  10. +-------------+-------------------+
  11. | @@read_only | @@super_read_only |
  12. +-------------+-------------------+
  13. | 0           | 0                 |
  14. +-------------+-------------------+
顯然和Single-Master Mode不一樣的是,除了3306例項,另外兩個成員也就是3307和3308例項均為可寫成員了。
也就是所謂的Multi-Master Mode


當然可以測試一下:
在3307例項上做insert,在3308例項上update,最後在3306上查詢:
  1. [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "INSERT INTO test.tb_test VALUES(4,'吸血面具')";
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3308.sock -e "UPDATE test.tb_test SET name='撒旦之邪力' WHERE id=4;"
  4. mysql: [Warning] Using a password on the command line interface can be insecure.
  5. [root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3306.sock -e "SELECT * FROM test.tb_test;"
  6. mysql: [Warning] Using a password on the command line interface can be insecure.
  7. +----+-----------------+
  8. | id | name            |
  9. +----+-----------------+
  10. | 1  | 靈魂守衛         |
  11. | 2  | 斯嘉蒂之眼       |
  12. | 3  | 幻影斧           |
  13. | 4  | 撒旦之邪力       |
  14. +----+-----------------+


至此,已經成功將Single-Master Mode修改為Multi-Master Mode


P.S. 在多主模式中,已經不能透過下述SQL來查詢primary member是哪一臺例項了……雖然不明白為毛,可能在後續版本會改進???(猜測)
但總之在多主模式中,每一臺status為online的成員都是primary。
  1. SELECT b.member_id, b.member_host, b.member_port
  2. FROM performance_schema.global_status a
  3. JOIN performance_schema.replication_group_members b
  4. ON a.variable_value = b.member_id
  5. WHERE a.variable_name= 'group_replication_primary_member';


總之……看起來很好用的樣子。



從零開始搭建Multi-Master Mode的GR同樣也很簡單,可以參考:
 


兩種工作模式在配置引數上的核心差別為:
  1. loose-group_replication_single_primary_mode=FALSE
  2. loose-group_replication_enforce_update_everywhere_checks=TRUE
  3. (single-master mode 這倆個引數的值為TRUE和FALSE)






〇 要求和限制

僅可用於InnoDB儲存引擎(需要事務的支援和行級鎖)
表必須有主鍵(建立無主鍵的表不會報錯,但在插入資料的時候會丟擲:ERROR 3098 (HY000): The table does not comply with the requirements by an external plugin.)
必須啟用GTID
必須開啟二進位制日誌,並且其格式必須為ROW(binlog_format=row)
衝突DDl、DML只能在同一成員上執行成功
在多主結構中,不完全支援外來鍵(單主結構中是沒有問題的)
不支援serializable的事務隔離級別
只支援IPv4,並且需要低延遲,高頻寬的網路環境
GR最大支援9個成員
複製資訊後設資料必須存在系統表(master-info-repository=TABLE、relay-log-info-repository=TABLE)
二進位制日誌checksums必須關閉(binlog-checksum=NONE)
不支援savepoint的使用






〇 參考文件:

MySQL 5.7 Reference Manual - 19.2 Group Replication 

馬楚成 - 使用群組複製實現MySQL高可用性 ()

mysqlha組文件 Docs ? Requirements and Limitations




作者微信公眾號(持續更新)






來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29773961/viewspace-2133314/,如需轉載,請註明出處,否則將追究法律責任。

相關文章