MySQL MGR 叢集搭建(單主模式)

梓沐發表於2019-09-27

1.背景
MySQL Group Replication(MGR)是MySQL官方在5.7.17版本引進的一個資料庫高可用與高擴充套件的解決方案,以外掛形式提供,實現了分散式下資料的最終一致性,總結MGR特點如下:
* 高一致性:基於分散式paxos協議實現組複製,保證資料一致性;
* 高容錯性:自動檢測機制,只要不是大多數節點都當機就可以繼續工作,內建防腦裂保護機制;
* 高擴充套件性:節點的增加與移除會自動更新組成員資訊,新節點加入後,自動從其他節點同步增量資料,直到與其他節點資料一致;
* 高靈活性:提供單主模式和多主模式,單主模式在主庫當機後能夠自動選主,所有寫入都在主節點進行,多主模式支援多節點寫入。
2. 環境:
3臺伺服器搭建3節點MGR叢集,MySQL版本5.7.27,作業系統版本CentOS 7.3。
* 192.168.8.71
* 192.168.8.72
* 192.168.8.73
3.安裝步驟
3.1 安裝MySQL5.7
MySQL官方版5.7.27,採用Linux Generic安裝方式,直接下載壓縮包,解壓到目錄/usr/local/mysql。
3.2 修改配置檔案
編輯配置檔案/etc/my.cnf,3個節點除了server_id、loose-group_replication_local_address、report_host 三個引數不一樣外,其他保持一致。
[mysqld]
port=3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
binlog_checksum=NONE
log_bin=binlog
log_slave_updates=ON
binlog_format=ROW
master_info_repository=TABLE
relay_log_info_repository=TABLE
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= "192.168.8.71:33061"
loose-group_replication_group_seeds= "192.168.8.71:33061,192.168.8.72:33061,192.168.8.73:33061"
loose-group_replication_bootstrap_group=OFF
report_host=192.168.8.71
report_port=3306
3.3 初始化資料庫(所有節點執行)
3.4 啟動資料庫,安裝MGR外掛,設定複製賬號(所有節點執行)
--安裝MGR外掛
mysql>INSTALL PLUGIN group_replication SONAME 'group_replication.so';
--設定複製賬號
mysql> SET SQL_LOG_BIN=0;
mysql> CREATE USER repl@'%' IDENTIFIED BY 'repl';
mysql> GRANT REPLICATION SLAVE ON *.* TO repl@'%';
mysql> FLUSH PRIVILEGES;
mysql> SET SQL_LOG_BIN=1;
mysql> CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='repl' FOR CHANNEL 'group_replication_recovery';
3.5 啟動MGR單主模式
# 啟動MGR,在主庫(192.168.8.71)上執行
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
# 檢視MGR組資訊
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+--------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST  | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+--------------+-------------+--------------+
| group_replication_applier | 7777f2fe-bd6b-11e9-aad5-08002789d401 | 192.168.8.71 |        3306 | ONLINE       |
+---------------------------+--------------------------------------+--------------+-------------+--------------+
1 row in set (0.00 sec)
# 其他節點加入MGR,在從庫(192.168.8.72,192.168.8.73)上執行
mysql> START GROUP_REPLICATION;
# 檢視MGR組資訊
mysql> SELECT channel_name,
    ->        member_id,
    ->        member_host,
    ->        member_port,
    ->        member_state,
    ->        IF(global_status.variable_name IS NOT NULL, 'PRIMARY', 'SECONDARY') AS member_role
    ->   FROM performance_schema.replication_group_members
    ->   LEFT JOIN performance_schema.global_status
    ->     ON global_status.variable_name = 'group_replication_primary_member'
    ->    AND global_status.variable_value = replication_group_members.member_id;
+---------------------------+--------------------------------------+--------------+-------------+--------------+-------------+
| channel_name              | member_id                            | member_host  | member_port | member_state | member_role |
+---------------------------+--------------------------------------+--------------+-------------+--------------+-------------+
| group_replication_applier | 7777f2fe-bd6b-11e9-aad5-08002789d401 | 192.168.8.71 |        3306 | ONLINE       | PRIMARY     |
| group_replication_applier | 7777f2fe-bd6b-11e9-aad5-08002789d402 | 192.168.8.72 |        3306 | ONLINE       | SECONDARY   |
| group_replication_applier | 7777f2fe-bd6b-11e9-aad5-08002789d403 | 192.168.8.73 |        3306 | ONLINE       | SECONDARY   |
+---------------------------+--------------------------------------+--------------+-------------+--------------+-------------+
3 rows in set (0.00 sec)
可以看到,3個節點狀態為online,並且主節點為192.168.8.71,只有主節點可以寫入,其他節點只讀,MGR單主模式搭建成功
3.6 切換到多主模式
MGR切換模式需要重新啟動組複製,因些需要在所有節點上先關閉組複製,設定 group_replication_single_primary_mode=OFF 等引數,再啟動組複製。
# 停止組複製(所有節點執行):
mysql> stop group_replication;
mysql> set global group_replication_single_primary_mode=OFF;
mysql> set global group_replication_enforce_update_everywhere_checks=ON;
# 隨便選擇某個節點執行
mysql> SET GLOBAL group_replication_bootstrap_group=ON;
mysql> START GROUP_REPLICATION;
mysql> SET GLOBAL group_replication_bootstrap_group=OFF;
# 其他節點執行
mysql> START GROUP_REPLICATION;
# 檢視組資訊,所有節點的 MEMBER_ROLE 都為 PRIMARY
mysql> SELECT channel_name,
    ->        member_id,
    ->        member_host,
    ->        member_port,
    ->        member_state,
    ->        IF(global_status.variable_name IS NOT NULL, 'PRIMARY', 'SECONDARY') AS member_role
    ->   FROM performance_schema.replication_group_members
    ->   LEFT JOIN performance_schema.global_status
    ->     ON global_status.variable_name = 'group_replication_primary_member'
    ->    AND global_status.variable_value = replication_group_members.member_id;
+---------------------------+--------------------------------------+--------------+-------------+--------------+-------------+
| channel_name              | member_id                            | member_host  | member_port | member_state | member_role |
+---------------------------+--------------------------------------+--------------+-------------+--------------+-------------+
| group_replication_applier | 7777f2fe-bd6b-11e9-aad5-08002789d401 | 192.168.8.71 |        3306 | ONLINE       | PRIMARY     |
| group_replication_applier | 7777f2fe-bd6b-11e9-aad5-08002789d402 | 192.168.8.72 |        3306 | ONLINE       | PRIMARY     |
| group_replication_applier | 7777f2fe-bd6b-11e9-aad5-08002789d403 | 192.168.8.73 |        3306 | ONLINE       | PRIMARY     |
+---------------------------+--------------------------------------+--------------+-------------+--------------+-------------+
3 rows in set (0.00 sec)
可以看到所有節點狀態都是online,角色都是PRIMARY,MGR多主模式搭建成功。
3.7 切回單主模式
# 所有節點執行
mysql> stop group_replication;
mysql> set global group_replication_enforce_update_everywhere_checks=OFF;
mysql> set global group_replication_single_primary_mode=ON;
# 主節點(192.168.8.71)執行
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION;
SET GLOBAL group_replication_bootstrap_group=OFF;
# 從節點(192.168.8.72、192.168.8.73)執行
START GROUP_REPLICATION;
# 檢視MGR組資訊
mysql> SELECT channel_name,
    ->        member_id,
    ->        member_host,
    ->        member_port,
    ->        member_state,
    ->        IF(global_status.variable_name IS NOT NULL, 'PRIMARY', 'SECONDARY') AS member_role
    ->   FROM performance_schema.replication_group_members
    ->   LEFT JOIN performance_schema.global_status
    ->     ON global_status.variable_name = 'group_replication_primary_member'
    ->    AND global_status.variable_value = replication_group_members.member_id;
+---------------------------+--------------------------------------+--------------+-------------+--------------+-------------+
| channel_name              | member_id                            | member_host  | member_port | member_state | member_role |
+---------------------------+--------------------------------------+--------------+-------------+--------------+-------------+
| group_replication_applier | 7777f2fe-bd6b-11e9-aad5-08002789d401 | 192.168.8.71 |        3306 | ONLINE       | PRIMARY     |
| group_replication_applier | 7777f2fe-bd6b-11e9-aad5-08002789d402 | 192.168.8.72 |        3306 | ONLINE       | SECONDARY   |
| group_replication_applier | 7777f2fe-bd6b-11e9-aad5-08002789d403 | 192.168.8.73 |        3306 | ONLINE       | SECONDARY   |
+---------------------------+--------------------------------------+--------------+-------------+--------------+-------------+

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

相關文章