MySQL Group Replication 學習(部署篇)

yepkeepmoving發表於2017-07-15
  MySQL5.7版本出來有很長時間了,之前也裝了玩了下,大概瞭解了部分功能和特性,但沒有系統的學習和測試其主要新增功能,最近也特意抽出時間想去多瞭解瞭解,學習學習5.7的主要新特性,這裡主要是針對其最突出的功能之一MySQL Group Replication(MGR 組複製)學習下,針對其部署過程做個簡要記錄。
一、環境準備
名稱    版本    備註
作業系統    RHEL6.5_X86_64    
資料庫    5.7.18-15   Percona二進位制版本
複製節點  10.26.7.129 node1
    10.26.7.142 node2
    10.26.7.166 node3
二、MGR詳細部署步驟
1、MYSQL5.7安裝
本次學習實驗採用的是Percona-Server-5.7.18-15-Linux.x86_64.ssl101二進位制版本,具體安裝過程略
2、節點1my.cnf引數配置(主寫節點)
#replicate
server-id=1001
skip-slave-start = false
read-only = false
expire_logs_days = 2
max_binlog_size = 1G
max_binlog_cache_size = 2G
log-bin = /home/mysql/mysql-bin
log-bin-index = /home/mysql/bin-index
binlog_format = row

log-slave-updates = 1
sync_binlog = 1
log-slow-slave-statements = 1
max-relay-log-size = 1G
relay-log = /home/mysql/mysql-relay
relay-log-index = /home/mysql/relay-index
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
#group replication
transaction_write_set_extraction  =XXHASH64         
loose-group_replication_group_name  ="2faa74b8-66bb-11e7-ab7e-b8ca3a6a61ec"  #務必以uuid形式配置
loose-group_replication_start_on_boot  =off
loose-group_replication_local_address  ="10.26.7.129:24001"                                    #不同節點配置不同節點本身的IP地址和埠,區分MYSQL自身的3306埠
loose-group_replication_group_seeds  ="10.26.7.129:24001,10.26.7.142:24001,10.26.7.166:24001"
loose-group_replication_bootstrap_group  =off
3、建立複製賬戶(主寫節點)
set sql_log_bin=0;
create user rpl_user@'%';
grant replication slave on *.* to rpl_user@'%'  identified by 'rpl_pass';
flush privileges;
set sql_log_bin=1;
change master to master_user='rpl_user',master_password='rpl_pass' for channel 'group_replication_recovery';
4、安裝組複製外掛並啟動組複製(主寫節點)
安裝外掛
install plugin group_replication soname 'group_replication.so';
檢查外掛是否正確安裝
show plugins
+-----------------------------+----------+--------------------+----------------------+---------+
| Name                        | Status   | Type               | Library              | License |
+-----------------------------+----------+--------------------+----------------------+---------+
| group_replication           | ACTIVE   | GROUP REPLICATION  | group_replication.so | GPL     |
+-----------------------------+----------+--------------------+----------------------+---------+
啟動組複製
set global group_replication_bootstrap_group=ON;
start group_replication;
set global group_replication_bootstrap_group=OFF;
檢查組複製成員及狀態
select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 2faa74b8-66bb-11e7-ab7e-b8ca3a6a61ec | node1       |        3306 | ONLINE       |
5、新增組複製成員例項node2 和node3
***新增節點前,務必做DNS解析,如果沒有配置DNS解析伺服器,需要在每個節點配置hosts解析  /etc/hosts***
10.26.7.166 node3
10.26.7.142 node2
10.26.7.129 node1

***節點my.cnf引數檔案server-id和loose-group_replication_local_address ="node2:24001需要分別更改"***

node2
set sql_log_bin=0;
create user rpl_user@'%';
grant replication slave on *.* to rpl_user@'%'  identified by 'rpl_pass';
flush privileges;

set sql_log_bin=1;
change master to master_user='rpl_user',master_password='rpl_pass' for channel 'group_replication_recovery';

install plugin group_replication soname 'group_replication.so';
show plugins

set global group_replication_allow_local_disjoint_gtids_join=ON;
start group_replication;

node3同樣執行上述命令

然後檢查組複製資訊
select * from performance_schema.replication_group_members;     
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 2faa74b8-66bb-11e7-ab7e-b8ca3a6a61ec | node1       |        3306 | ONLINE       |
| group_replication_applier | 35e38786-66bb-11e7-bcc3-b8ca3a6a61a4 | node2       |        3306 | ONLINE       |
| group_replication_applier | 3bbedb1e-66bb-11e7-8fc0-b8ca3a6a7c48 | node3       |        3306 | ONLINE       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+

測試組複製是否正常:
(root:localhost:Sat Jul 15 13:26:33 2017)[(none)]>create database dbtest;
Query OK, 1 row affected (0.01 sec)
(root:localhost:Sat Jul 15 13:26:40 2017)[(none)]>use dbtest;
Database changed
(root:localhost:Sat Jul 15 13:26:45 2017)[dbtest]>create table t1(id int primary key);
Query OK, 0 rows affected (0.01 sec)
(root:localhost:Sat Jul 15 13:26:54 2017)[dbtest]>insert into t1 values(1);
Query OK, 1 row affected (0.00 sec)
node2和node3執行查詢
(root:localhost:Sat Jul 15 12:57:32 2017)[db01]>use dbtest;
Database changed
(root:localhost:Sat Jul 15 13:27:26 2017)[dbtest]>select * from t1;
+----+
| id |
+----+
|  1 |
+----+
1 row in set (0.00 sec)
三、錯誤問題以及彙總:
1、錯誤案例01
錯誤資訊:2017-07-15T01:36:06.929941Z 4 [ERROR] Plugin group_replication reported: 'The group name 'group-replication-test' is not a valid UUID'
錯誤原因:loose-group_replication_group_name引數沒有按照UUID格式指定,被認為設定該引數無效
解決方案:更改loose-group_replication_group_name引數值為,loose-group_replication_group_name  ="2faa74b8-66bb-11e7-ab7e-b8ca3a6a61ec"
2、錯誤案例02
錯誤資訊:
2017-07-15T01:29:27.271909Z 0 [Warning] unknown variable 'loose-group_replication_group_name=group-replication-test'
2017-07-15T01:29:27.271926Z 0 [Warning] unknown variable 'loose-group_replication_start_on_boot=off'
2017-07-15T01:29:27.271930Z 0 [Warning] unknown variable 'loose-group_replication_local_address=10.26.7.129:3306'
2017-07-15T01:29:27.271935Z 0 [Warning] unknown variable 'loose-group_replication_group_seeds=10.26.7.129:3306,10.26.7.142:3306,10.26.7.166:3306'
2017-07-15T01:29:27.271939Z 0 [Warning] unknown variable 'loose-group_replication_bootstrap_group=off'
錯誤原因:因為先設定了這些引數,而沒有裝group_replication外掛,導致資料庫例項無法識別這些引數
解決方案:安裝group replication外掛,install plugin group_replication soname 'group_replication.so'; (uninstall plugin group_replication 解除安裝,show plugins檢視)
3、錯誤案例03
錯誤資訊:
2017-07-15T01:54:54.447829Z 0 [Note] Plugin group_replication reported: 'Unable to bind to 0.0.0.0:3306 (socket=60, errno=98)!'
2017-07-15T01:54:54.447948Z 0 [ERROR] Plugin group_replication reported: 'Unable to announce tcp port 3306. Port already in use?'
2017-07-15T01:54:54.448101Z 0 [ERROR] Plugin group_replication reported: '[GCS] Error joining the group while waiting for the network layer to become ready.'
錯誤原因:配置的組複製監聽埠和MYSQL例項埠衝突
解決方案:調整下面引數
loose-group_replication_local_address  ="10.26.7.129:24001"                                    #不同節點配置不同節點本身的IP地址和埠,區分MYSQL自身的3306埠
loose-group_replication_group_seeds  ="10.26.7.129:24001,10.26.7.142:24001,10.26.7.166:24001"
4、錯誤案例04
錯誤資訊:
2017-07-15T04:20:01.249529Z 21 [ERROR] Slave I/O for channel 'group_replication_recovery': error connecting to master 'rpl_user@node2:3306' - retry-time: 60  retries: 1, Error_code: 2005
錯誤原因:沒有配置DNS解析或者hosts解析,節點無法連線其他資料庫
解決方案:配置hosts解析,每個節點/etc/hosts新增如下內容
10.26.7.166 node3
10.26.7.142 node2
10.26.7.129 node1
5、錯誤案例05
錯誤資訊
2017-07-15T03:42:45.395407Z 288 [ERROR] Slave SQL for channel 'group_replication_recovery': Error 'Can't create database 'db01'; database exists' on query. Default database: 'db01'. Query: 'create database db01', Error_code: 1007
2017-07-15T03:42:45.395472Z 288 [Warning] Slave: Can't create database 'db01'; database exists Error_code: 1007
2017-07-15T03:42:45.395503Z 288 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.000005' position 434
錯誤原因:這個錯誤是由於節點由於各種原因退出mgr組,後面又加入了mgr組,但之前存在的資料依舊存在
解決方案:刪除要加入組複製節點存在的資料庫即可,但其他節點不是主寫節點,需要先調整引數set global super_read_only=0;然後執行drop database db01;再重新加入組
set global group_replication_allow_local_disjoint_gtids_join=ON;
start group_replication;
6、錯誤案例06
錯誤資訊:
2017-07-15T03:44:09.982428Z 18 [ERROR] Slave SQL for channel 'group_replication_recovery': Error 'Can't create database 'db01'; database exists' on query. Default database: 'db01'. Query: 'create database db01', Error_code: 1007
2017-07-15T03:44:09.982493Z 18 [Warning] Slave: Can't create database 'db01'; database exists Error_code: 1007
2017-07-15T03:44:09.982522Z 18 [ERROR] Error running query, slave SQL thread aborted. Fix the problem, and restart the slave SQL thread with "SLAVE START". We stopped at log 'mysql-bin.000005' position 434
錯誤原因:同上錯誤案例05
解決方案:同上錯誤案例05
7、錯誤案例07
錯誤資訊:
2017-07-15T03:49:10.370846Z 0 [ERROR] Plugin group_replication reported: 'This member has more executed transactions than those present in the group. Local transactions: 2faa74b8-66bb-11e7-ab7e-b8ca3a6a61ec:1-4,35e38786-66bb-11e7-bcc3-b8ca3a6a61a4:1 > Group transactions: 2faa74b8-66bb-11e7-ab7e-b8ca3a6a61ec:1-16'
錯誤原因:同上錯誤案例05,在從庫執行了多餘的事務
解決方案:同上錯誤案例05,直接重新加入節點即可
set global group_replication_allow_local_disjoint_gtids_join=ON;
start group_replication;
8、錯誤案例08
錯誤資訊
ERROR 3098 (HY000): The table does not comply with the requirements by an external plugin.
錯誤原因:由於主節點建立了表t1,但沒有指定主鍵(此時表結構可以複製到各節點,一旦插入資料DML操作即會報錯)
解決方案:為表增加主鍵,然後做DML操作(MGR需要各表都有主鍵)
alter table t1 add primary key(id);
insert into t1 values(1),(2);    
9、錯誤案例09
錯誤資訊:
mysqldump -R -E --triggers --single-transaction  --master-data=2 -B db01 >db01.sql
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
mysqldump: Couldn't execute 'SAVEPOINT sp': The MySQL server is running with the --transaction-write-set-extraction!=OFF option so it cannot execute this statement (1290)
錯誤原因:mgr不支援mysqldump的事務一致性備份,因為其不支援savepoint
解決方案:透過xtrabackup或者不加--single-transaction備份
10、錯誤案例10
錯誤資訊:
create table t2 as select * from t1;
ERROR 1786 (HY000): Statement violates GTID consistency: CREATE TABLE ... SELECT.
錯誤原因:配置MGR,開啟了GTID,所有GTID不支援的操作,MGR也不支援
解決方案:使用create table t2 like t1; insert into t2 select * from t;分開兩個事務執行

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

相關文章