MySQL 5.7.17 Group Relication(組複製)搭建手冊
本博文介紹了Group Replication的兩種工作模式的架構。
並詳細介紹了Single-Master Mode的部署過程,以及如何切換到Multi-Master Mode。
當然,文末給出了Group Replication的配置要求和一些限制。
〇 結構介紹
在2016年12月釋出的5.7.17版本的MySQL,甲骨文宣佈Group Replication已經GA。
Group Replication(下簡稱GR)有兩個工作模式,分別為Single-Master Mode與Multi-Master Mode:
Single-Master Mode的failover圖:
只有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
建立資料目錄及日誌目錄:
解壓二進位制包並將其設定為basedir:
〇 新增第一臺例項:
新增3306例項的配置檔案:
初始化3306例項datadir:
啟動3306例項:
透過MySQL Client進入第一個例項(密碼為空)
建立複製使用者與授權,並讓其作為group的第一個成員:
安裝GR外掛:
可以檢查一下是否安裝成功:
開啟第一個組複製:
檢查一下組複製成員,其中member_id就是@@server_uuid的值
新增測試資料:
〇 新增第二個例項(3307)
修改3307配置檔案,將3306改成3307,並且將loose-group_replication_local_address的埠從24901改成24902:
初始化3307例項:
啟動3307例項:
透過MySQL Client進入3307例項:
重複在3306例項的操作:
在3307例項上安裝GR外掛,開啟組複製:
檢查一下成員狀態:
過了一陣子再檢查,仍然是RECOVERING。
再過一陣子檢查,發現member_state被置為ERROR:
此時檢查3306例項的組複製情況,發現檢查不到另一個例項的資訊了:
開多一個終端,檢查3307例項的error log發現:
應該是解析的問題,修改hosts檔案,在末尾加上主機名:
重新操作3307例項:
檢查組複製狀態,發現兩個例項的狀態均為ONLINE了:
在3307上檢查一下同步狀態:
〇 新增3308例項:
修改3308配置檔案:
然後初始化並啟動3308例項:
同樣進入3308例項:
在3308例項上重複操作:
繼續重複操作,安裝GR外掛並啟動它:
最後再檢查一下組複製成員的狀態:
當然在3308例項上也已將3306的事務apply過來了:
root@localhost使用者在上述操作中為空密碼,可以給root@localhost加個密碼……
因為三個例項都在一個GR組裡,所以對3306例項操作就行了:
當然ALTER操作會被記錄到3306的binlog裡,並同步到3307和3308例項上。
可以檢視一下三臺例項的read_only和super-read-only值:
可以發現只有3306例項也就是第一個例項屬於可寫例項,而3307和3308均為read-only模式。
決定因素為第一個加入該GR組的成員,之後加入該GR組的均為ro,在該模式與MongoDB Replicate Set很相似。
當然如果要確定哪一個成員是primary,可以在三個成員中的任意一個執行:
至此,Group Replication預設的single-master mode已經搭建完畢。
〇 將Single-Master Mode修改為Multi-Master Mode
如果要將Single-Master Mode修改為Multi-Master Mode,也比較簡單。
考慮到此時的Primary成員是3306,並且假定3306例項在對外提供寫服務,我這邊的操作如下:
首先停掉兩個secondary的組複製,在3307和3308例項上操作:
再在3306例項上重複以上操作:
然後在3306上作為第一個成員啟動組複製:
在停啟組複製的過程中,3306例項仍對外提供服務,此處模擬修改:
再3307和3308兩個例項上分別開啟組複製:
並檢查3307和3308是否將3306的事務應用過來:
當然可以看到,在3306上做的修改,在3307和3308開啟組複製之後也已經同步過來了。
那麼再檢查一下3307和3308是否可寫:
顯然和Single-Master Mode不一樣的是,除了3306例項,另外兩個成員也就是3307和3308例項均為可寫成員了。
也就是所謂的Multi-Master Mode。
當然可以測試一下:
在3307例項上做insert,在3308例項上update,最後在3306上查詢:
至此,已經成功將Single-Master Mode修改為Multi-Master Mode。
P.S. 在多主模式中,已經不能透過下述SQL來查詢primary member是哪一臺例項了……雖然不明白為毛,可能在後續版本會改進???(猜測)
但總之在多主模式中,每一臺status為online的成員都是primary。
總之……看起來很好用的樣子。
從零開始搭建Multi-Master Mode的GR同樣也很簡單,可以參考:
兩種工作模式在配置引數上的核心差別為:
〇 要求和限制
僅可用於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
作者微信公眾號(持續更新)
並詳細介紹了Single-Master Mode的部署過程,以及如何切換到Multi-Master Mode。
當然,文末給出了Group Replication的配置要求和一些限制。
〇 結構介紹
在2016年12月釋出的5.7.17版本的MySQL,甲骨文宣佈Group Replication已經GA。
Group Replication(下簡稱GR)有兩個工作模式,分別為Single-Master Mode與Multi-Master Mode:
Single-Master Mode的failover圖:
只有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
建立資料目錄及日誌目錄:
-
mkdir -p /data/mysql57/mysql3306
-
mkdir -p /data/mysql57/mysql3307
-
mkdir -p /data/mysql57/mysql3308
-
mkdir -p /data/mysql57/logs/3306
-
mkdir -p /data/mysql57/logs/3307
- mkdir -p /data/mysql57/logs/3308
解壓二進位制包並將其設定為basedir:
-
mv mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz /data/mysql57
-
cd /data/mysql57
-
tar zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
- mv mysql-5.7.17-linux-glibc2.5-x86_64 mysql-basedir
〇 新增第一臺例項:
新增3306例項的配置檔案:
-
vim /data/mysql57/mysql3306/3306.cnf
-
-
[client]
- prompt = "(\u@\h) [\d]> "
-
-
[mysqld]
-
server_id = 3306
-
user = mysql
-
port = 3306
-
socket = /tmp/mysql3306.sock
-
basedir = /data/mysql57/mysql-basedir
-
datadir = /data/mysql57/mysql3306
-
pid-file = /data/mysql57/mysql3306/3306.pid
-
log-error = /data/mysql57/logs/3306/error-log
-
log-bin = /data/mysql57/logs/3306/binlog
-
log-bin-index = /data/mysql57/logs/3306/binlog.index
-
relay-log = /data/mysql57/logs/3306/relaylog
-
relay-log-index = /data/mysql57/logs/3306/relaylog.index
-
-
master_info_repository = TABLE
-
relay_log_info_repository = TABLE
-
log_slave_updates = ON
-
binlog_checksum = NONE
-
binlog_format = ROW
- transaction_isolation = READ-COMMITTED
-
gtid_mode = ON
-
enforce_gtid_consistency = ON
-
-
# GR 配置項 其中loose字首表示若Group Replication plugin未載入 mysql server仍繼續啟動
-
transaction_write_set_extraction = XXHASH64
-
loose-group_replication_group_name = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" # 組名,此處可拿select uuid();生成
-
loose-group_replication_start_on_boot = off # 在mysqld啟動時不自動啟動組複製
-
loose-group_replication_local_address = "127.0.0.1:24901"
-
loose-group_replication_group_seeds = "127.0.0.1:24901,127.0.0.1:24902,127.0.0.1:24903"
- loose-group_replication_bootstrap_group = off
初始化3306例項datadir:
- /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3306.cnf --initialize-insecure
啟動3306例項:
- /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3306.cnf &
透過MySQL Client進入第一個例項(密碼為空)
- /data/mysql57/mysql-basedir/bin/mysql -uroot -p -S /tmp/mysql3306.sock
建立複製使用者與授權,並讓其作為group的第一個成員:
-
SET SQL_LOG_BIN=0;
-
CREATE USER repl@'%';
-
GRANT REPLICATION SLAVE ON *.* TO repl@'%' IDENTIFIED BY 'repl@123';
-
FLUSH PRIVILEGES;
-
SET SQL_LOG_BIN=1;
- CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='repl@123' FOR CHANNEL 'group_replication_recovery';
安裝GR外掛:
-
(root@localhost) [(none)]> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
- Query OK, 0 rows affected (0.07 sec)
可以檢查一下是否安裝成功:
-
(root@localhost) [(none)]> SELECT * FROM information_schema.plugins WHERE PLUGIN_NAME LIKE '%group%'\G
-
*************************** 1. row ***************************
-
PLUGIN_NAME: group_replication
-
PLUGIN_VERSION: 1.0
-
PLUGIN_STATUS: ACTIVE
-
PLUGIN_TYPE: GROUP REPLICATION
-
PLUGIN_TYPE_VERSION: 1.1
-
PLUGIN_LIBRARY: group_replication.so
-
PLUGIN_LIBRARY_VERSION: 1.7
-
PLUGIN_AUTHOR: ORACLE
-
PLUGIN_DESCRIPTION: Group Replication (1.0.0)
-
PLUGIN_LICENSE: GPL
-
LOAD_OPTION: ON
- 1 row in set (0.00 sec)
開啟第一個組複製:
-
(root@localhost) [(none)]> SET GLOBAL group_replication_bootstrap_group=ON;
-
Query OK, 0 rows affected (0.00 sec)
-
-
(root@localhost) [(none)]> START GROUP_REPLICATION;
-
Query OK, 0 rows affected (1.20 sec)
-
-
# 啟動組複製之後將group_replication_bootstrap_group設定為OFF
-
# 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
-
-
(root@localhost) [(none)]> SET GLOBAL group_replication_bootstrap_group=OFF;
- Query OK, 0 rows affected (0.01 sec)
檢查一下組複製成員,其中member_id就是@@server_uuid的值
-
(root@localhost) [(none)]> SELECT * FROM performance_schema.replication_group_members;
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
-
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
-
| group_replication_applier | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y | 3306 | ONLINE |
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
- 1 row in set (0.00 sec)
新增測試資料:
-
(root@localhost) [(none)]> CREATE DATABASE test;
-
Query OK, 1 row affected (0.00 sec)
-
-
(root@localhost) [(none)]> USE test;
-
Database changed
-
-
(root@localhost) [test]> CREATE TABLE tb_test(id int PRIMARY KEY, name varchar(20)) CHARACTER SET utf8;
-
Query OK, 0 rows affected (0.04 sec)
-
-
(root@localhost) [test]> INSERT INTO tb_test VALUES(1,'風暴之靈');
-
Query OK, 1 row affected (0.00 sec)
-
-
(root@localhost) [test]> INSERT INTO tb_test VALUES(2,'影之靈龕');
-
Query OK, 1 row affected (0.01 sec)
-
-
(root@localhost) [test]> UPDATE tb_test SET name='斯嘉蒂之眼' WHERE id=2;
-
Query OK, 1 row affected (0.02 sec)
-
Rows matched: 1 Changed: 1 Warnings: 0
-
-
(root@localhost) [test]> SELECT * FROM tb_test;
-
+----+-----------------+
-
| id | name |
-
+----+-----------------+
-
| 1 | 風暴之靈 |
-
| 2 | 斯嘉蒂之眼 |
-
+----+-----------------+
- 2 rows in set (0.00 sec)
〇 新增第二個例項(3307)
修改3307配置檔案,將3306改成3307,並且將loose-group_replication_local_address的埠從24901改成24902:
-
cp /data/mysql57/3306.cnf /data/mysql57/3307.cnf
-
sed -i "s/3306/3307/g" /data/mysql57/3307.cnf
- sed -i "s/24901\"/24902\"/g" 3307.cnf
初始化3307例項:
- /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3307.cnf --initialize-insecure
啟動3307例項:
- /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3307.cnf &
透過MySQL Client進入3307例項:
- /data/mysql57/mysql-basedir/bin/mysql -uroot -p -S /tmp/mysql3307.sock
重複在3306例項的操作:
-
SET SQL_LOG_BIN=0;
-
CREATE USER repl@'%';
-
GRANT REPLICATION SLAVE ON *.* TO repl@'%' IDENTIFIED BY 'repl@123';
-
FLUSH PRIVILEGES;
-
SET SQL_LOG_BIN=1;
- CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='repl@123' FOR CHANNEL 'group_replication_recovery';
在3307例項上安裝GR外掛,開啟組複製:
-
(root@localhost) [(none)]> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
-
Query OK, 0 rows affected (0.00 sec)
-
-
(root@localhost) [(none)]> START GROUP_REPLICATION;
- Query OK, 0 rows affected (5.61 sec)
檢查一下成員狀態:
-
(root@localhost) [(none)]> SELECT * FROM performance_schema.replication_group_members;
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
-
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
-
| group_replication_applier | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y | 3306 | ONLINE |
-
| group_replication_applier | 6bddae0d-eca1-11e6-b9d5-000c297f23b7 | sAno1y | 3307 | RECOVERING |
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
- 2 rows in set (0.00 sec)
過了一陣子再檢查,仍然是RECOVERING。
再過一陣子檢查,發現member_state被置為ERROR:
-
(root@localhost) [(none)]> SELECT * FROM performance_schema.replication_group_members;
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
-
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
-
| group_replication_applier | 6bddae0d-eca1-11e6-b9d5-000c297f23b7 | sAno1y | 3307 | ERROR |
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
- 1 row in set (0.01 sec)
此時檢查3306例項的組複製情況,發現檢查不到另一個例項的資訊了:
-
(root@localhost) [(none)]> SELECT * FROM performance_schema.replication_group_members;
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
-
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
-
| group_replication_applier | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y | 3306 | ONLINE |
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
- 1 row in set (0.00 sec)
開多一個終端,檢查3307例項的error log發現:
- 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檔案,在末尾加上主機名:
-
[root@sAno1y 3307]# hostname
-
sAno1y
-
[root@sAno1y 3307]# cat /etc/hosts
-
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 sAno1y
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 sAno1y
重新操作3307例項:
-
(root@localhost) [(none)]> STOP GROUP_REPLICATION;
-
Query OK, 0 rows affected (0.00 sec)
-
-
(root@localhost) [(none)]> START GROUP_REPLICATION;
- Query OK, 0 rows affected (7.80 sec)
檢查組複製狀態,發現兩個例項的狀態均為ONLINE了:
-
(root@localhost) [(none)]> SELECT * FROM performance_schema.replication_group_members;
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
-
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
-
| group_replication_applier | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y | 3306 | ONLINE |
-
| group_replication_applier | 6bddae0d-eca1-11e6-b9d5-000c297f23b7 | sAno1y | 3307 | ONLINE |
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
- 2 rows in set (0.00 sec)
在3307上檢查一下同步狀態:
-
(root@localhost) [(none)]> SELECT * FROM test.tb_test;
-
+----+-----------------+
-
| id | name |
-
+----+-----------------+
-
| 1 | 風暴之靈 |
-
| 2 | 斯嘉蒂之眼 |
-
+----+-----------------+
- 2 rows in set (0.00 sec)
〇 新增3308例項:
修改3308配置檔案:
-
cp /data/mysql57/3306.cnf /data/mysql57/3308.cnf
-
sed -i "s/3306/3308/g" 3308.cnf
- sed -i "s/24901\"/24093\"/g" 3308.cnf
然後初始化並啟動3308例項:
-
/data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3308.cnf --initialize-insecure
- /data/mysql57/mysql-basedir/bin/mysqld --defaults-file=/data/mysql57/3308.cnf &
同樣進入3308例項:
- /data/mysql57/mysql-basedir/bin/mysql -uroot -p -S /tmp/mysql3308.sock
在3308例項上重複操作:
-
SET SQL_LOG_BIN=0;
-
CREATE USER repl@'%';
-
GRANT REPLICATION SLAVE ON *.* TO repl@'%' IDENTIFIED BY 'repl@123';
-
FLUSH PRIVILEGES;
-
SET SQL_LOG_BIN=1;
- CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='repl@123' FOR CHANNEL 'group_replication_recovery';
繼續重複操作,安裝GR外掛並啟動它:
-
INSTALL PLUGIN group_replication SONAME 'group_replication.so';
- START GROUP_REPLICATION;
最後再檢查一下組複製成員的狀態:
-
(root@localhost) [(none)]> SELECT * FROM performance_schema.replication_group_members;
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
-
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
-
| group_replication_applier | 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y | 3306 | ONLINE |
-
| group_replication_applier | 6bddae0d-eca1-11e6-b9d5-000c297f23b7 | sAno1y | 3307 | ONLINE |
-
| group_replication_applier | 6ce8c980-ed4a-11e6-bf00-000c297f23b7 | sAno1y | 3308 | ONLINE |
-
+---------------------------+--------------------------------------+-------------+-------------+--------------+
- 3 rows in set (0.00 sec)
當然在3308例項上也已將3306的事務apply過來了:
-
(root@localhost) [(none)]> SELECT * FROM test.tb_test;
-
+----+-----------------+
-
| id | name |
-
+----+-----------------+
-
| 1 | 風暴之靈 |
-
| 2 | 斯嘉蒂之眼 |
-
+----+-----------------+
- 2 rows in set (0.00 sec)
root@localhost使用者在上述操作中為空密碼,可以給root@localhost加個密碼……
因為三個例項都在一個GR組裡,所以對3306例項操作就行了:
- /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值:
-
[root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3306.sock -e "SELECT @@read_only, @@super_read_only";
-
mysql: [Warning] Using a password on the command line interface can be insecure.
-
+-------------+-------------------+
-
| @@read_only | @@super_read_only |
-
+-------------+-------------------+
-
| 0 | 0 |
- +-------------+-------------------+
-
-
[root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "SELECT @@read_only, @@super_read_only";
-
mysql: [Warning] Using a password on the command line interface can be insecure.
-
+-------------+-------------------+
-
| @@read_only | @@super_read_only |
-
+-------------+-------------------+
-
| 1 | 1 |
- +-------------+-------------------+
-
-
[root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3308.sock -e "SELECT @@read_only, @@super_read_only";
-
mysql: [Warning] Using a password on the command line interface can be insecure.
-
+-------------+-------------------+
-
| @@read_only | @@super_read_only |
-
+-------------+-------------------+
-
| 1 | 1 |
- +-------------+-------------------+
可以發現只有3306例項也就是第一個例項屬於可寫例項,而3307和3308均為read-only模式。
決定因素為第一個加入該GR組的成員,之後加入該GR組的均為ro,在該模式與MongoDB Replicate Set很相似。
當然如果要確定哪一個成員是primary,可以在三個成員中的任意一個執行:
-
SELECT b.member_id, b.member_host, b.member_port
-
FROM performance_schema.global_status a
-
JOIN performance_schema.replication_group_members b
-
ON a.variable_value = b.member_id
-
WHERE a.variable_name= 'group_replication_primary_member';
-
-
# 輸出結果:
-
+--------------------------------------+-------------+-------------+
-
| member_id | member_host | member_port |
-
+--------------------------------------+-------------+-------------+
-
| 3dc9cf4a-ec61-11e6-bdf3-000c297f23b7 | sAno1y | 3306 |
-
+--------------------------------------+-------------+-------------+
- 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例項上操作:
-
STOP GROUP_REPLICATION;
-
SET GLOBAL group_replication_single_primary_mode=FALSE;
- SET GLOBAL group_replication_enforce_update_everywhere_checks=TRUE;
再在3306例項上重複以上操作:
-
STOP GROUP_REPLICATION;
-
SET GLOBAL group_replication_single_primary_mode=FALSE;
- SET GLOBAL group_replication_enforce_update_everywhere_checks=TRUE;
然後在3306上作為第一個成員啟動組複製:
-
(root@localhost) [(none)]> SET GLOBAL group_replication_bootstrap_group=on;
-
Query OK, 0 rows affected (0.00 sec)
-
-
(root@localhost) [(none)]> START GROUP_REPLICATION;
-
Query OK, 0 rows affected (1.05 sec)
-
-
(root@localhost) [(none)]> SET GLOBAL group_replication_bootstrap_group=off;
- Query OK, 0 rows affected (0.00 sec)
在停啟組複製的過程中,3306例項仍對外提供服務,此處模擬修改:
-
(root@localhost) [(none)]> UPDATE test.tb_test SET name='靈魂守衛' WHERE id=1;
-
Query OK, 1 row affected (0.02 sec)
-
Rows matched: 1 Changed: 1 Warnings: 0
-
-
(root@localhost) [(none)]> INSERT INTO test.tb_test VALUES(3,'幻影斧');
-
Query OK, 1 row affected (0.00 sec)
-
-
(root@localhost) [(none)]> SELECT * FROM test.tb_test;
-
+----+-----------------+
-
| id | name |
-
+----+-----------------+
-
| 1 | 靈魂守衛 |
-
| 2 | 斯嘉蒂之眼 |
-
| 3 | 幻影斧 |
-
+----+-----------------+
- 3 rows in set (0.00 sec)
再3307和3308兩個例項上分別開啟組複製:
- START GROUP_REPLICATION;
並檢查3307和3308是否將3306的事務應用過來:
-
[root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "START GROUP_REPLICATION;"
- mysql: [Warning] Using a password on the command line interface can be insecure.
-
[root@sAno1y mysql57]#
-
[root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3308.sock -e "START GROUP_REPLICATION;"
-
mysql: [Warning] Using a password on the command line interface can be insecure.
-
[root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "SELECT * FROM test.tb_test;"
-
mysql: [Warning] Using a password on the command line interface can be insecure.
-
+----+-----------------+
-
| id | name |
-
+----+-----------------+
-
| 1 | 靈魂守衛 |
-
| 2 | 斯嘉蒂之眼 |
-
| 3 | 幻影斧 |
-
+----+-----------------+
-
[root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3308.sock -e "SELECT * FROM test.tb_test;"
-
mysql: [Warning] Using a password on the command line interface can be insecure.
-
+----+-----------------+
-
| id | name |
-
+----+-----------------+
-
| 1 | 靈魂守衛 |
-
| 2 | 斯嘉蒂之眼 |
-
| 3 | 幻影斧 |
- +----+-----------------+
那麼再檢查一下3307和3308是否可寫:
-
[root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "SELECT @@read_only, @@super_read_only;"
-
mysql: [Warning] Using a password on the command line interface can be insecure.
-
+-------------+-------------------+
-
| @@read_only | @@super_read_only |
-
+-------------+-------------------+
-
| 0 | 0 |
-
+-------------+-------------------+
-
[root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3308.sock -e "SELECT @@read_only, @@super_read_only;"
-
mysql: [Warning] Using a password on the command line interface can be insecure.
-
+-------------+-------------------+
-
| @@read_only | @@super_read_only |
-
+-------------+-------------------+
-
| 0 | 0 |
- +-------------+-------------------+
也就是所謂的Multi-Master Mode。
當然可以測試一下:
在3307例項上做insert,在3308例項上update,最後在3306上查詢:
-
[root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3307.sock -e "INSERT INTO test.tb_test VALUES(4,'吸血面具')";
-
mysql: [Warning] Using a password on the command line interface can be insecure.
-
[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;"
-
mysql: [Warning] Using a password on the command line interface can be insecure.
-
[root@sAno1y mysql57]# /data/mysql57/mysql-basedir/bin/mysql -uroot -proot123 -S /tmp/mysql3306.sock -e "SELECT * FROM test.tb_test;"
-
mysql: [Warning] Using a password on the command line interface can be insecure.
-
+----+-----------------+
-
| id | name |
-
+----+-----------------+
-
| 1 | 靈魂守衛 |
-
| 2 | 斯嘉蒂之眼 |
-
| 3 | 幻影斧 |
-
| 4 | 撒旦之邪力 |
- +----+-----------------+
至此,已經成功將Single-Master Mode修改為Multi-Master Mode。
P.S. 在多主模式中,已經不能透過下述SQL來查詢primary member是哪一臺例項了……雖然不明白為毛,可能在後續版本會改進???(猜測)
但總之在多主模式中,每一臺status為online的成員都是primary。
-
SELECT b.member_id, b.member_host, b.member_port
-
FROM performance_schema.global_status a
-
JOIN performance_schema.replication_group_members b
-
ON a.variable_value = b.member_id
- WHERE a.variable_name= 'group_replication_primary_member';
總之……看起來很好用的樣子。
從零開始搭建Multi-Master Mode的GR同樣也很簡單,可以參考:
兩種工作模式在配置引數上的核心差別為:
-
loose-group_replication_single_primary_mode=FALSE
-
loose-group_replication_enforce_update_everywhere_checks=TRUE
- (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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL 5.7.17 組複製(group replication)的要求和限制MySql
- 【Mysql】MySQL5.7.17- Group Replication搭建MySql
- MySQL 5.7組複製(group replication)的要求和限制MySql
- Mysql 5.7 基於組複製(MySQL Group Replication) - 運維小結MySql運維
- MySQL 組複製MySql
- MySQL組複製(MGR)全解析 Part 6 監控MySQL組複製MySql
- MYSQL PROXY搭建手冊MySql
- MySQL組複製(MGR)全解析 Part 1 組複製背景MySql
- mysql主從複製搭建MySql
- MySQL 5.5 複製搭建流程MySql
- MySQL 8 複製(八)——組複製安裝部署MySql
- MySQL 8 複製(九)——組複製聯機配置MySql
- MySQL 8 複製(十)——組複製效能與限制MySql
- 流複製管理手冊總結
- MySQL 8 複製(七)——組複製基本原理MySql
- 系統變數group_replication_group_seeds為空導致MySQL節點無法啟動組複製變數MySql
- MySQL 5.7搭建多源複製MySql
- MySQL 複製介紹及搭建MySql
- MYSQL主從複製的搭建MySql
- MYSQL主主複製的搭建MySql
- MySQL組複製(MGR)全解析 Part 3 組複製機制細節MySql
- MySQL5.7主從複製-半同步複製搭建MySql
- MySQL8.0.11 組複製配置MySql
- MySQL的Group By分組MySql
- Mysql主從複製原理及搭建MySql
- MySQL 5.5 主主複製搭建流程MySql
- 簡單搭建MySQL主從複製MySql
- 【Mysql】Mysql5.7的多源複製搭建MySql
- MySQL高可用之組複製技術(3):配置多主模型的組複製MySql模型
- MySQL高可用之組複製技術(2):配置單主模型的組複製MySql模型
- MySQL組複製MGR(一)-- 技術概述MySql
- mysql主從複製的理解和搭建MySql
- mysql 5.7 主從複製搭建及原理MySql
- MySQL組複製(MGR)全解析 Part 2 常用複製技術介紹MySql
- oracle高階複製的詳細手冊(轉)Oracle
- MySQL組複製的要求和限制歸納MySql
- MySQL 組複製故障恢復的有效策略MySql
- docker-compase搭建mysql主從複製DockerMySql