MGR集群部署
12、15、18主機環境準備
ssh免密碼登錄
\rm -rf .ssh/*
ssh-keygen
ssh-copy-id 127.1
scp -r .ssh 10.0.0.12:/root/
ssh root@10.0.0.12
還原基礎環境
systemctl stop mysqld
\rm -rf /var/lib/mysql/*
id mysql

vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysql/mysqld.log
pid-file=/run/mysqld/mysqld.pid

初始化mysql
mysqld --initialize-insecure --user=mysql --basedir=/usr --datadir=/var/lib/mysql
systemctl start mysqld

設定用戶認證策略
mysql -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';"

配置文件環境
12主機
生成uuid
uuidgen

定制配置文件
cat >> /etc/my.cnf.d/mysql-server.cnf <<- eof
# 為了版本兼容性,更改默認的用戶認證插件
default_authentication_plugin=mysql_native_password
# 設置MySQL插件目錄:MGR基于插件,必須設置插件路徑
plugin_dir=/usr/lib64/mysql/plugin
# 復制框架
server_id=12
# 開啟binlog的GTID模式(MGR強制要求)
gtid_mode=ON
# 開啟后MySQL只允許能夠保障事務安全,并且能夠被日志記錄的SQL語句被執行
enforce_gtid_consistency=ON
# 關閉binlog校驗(MGR強制要求)
binlog_checksum=NONE
log_bin=binlog
log_slave_updates=ON
binlog_format=ROW
master_info_repository=TABLE
relay_log_info_repository=TABLE# 組復制設置
# server必須為每個事務收集寫集合,并使用XXHASH64哈希算法將其編碼為散列
transaction_write_set_extraction=XXHASH64
# 啟用組復制模塊
plugin_load_add='group_replication.so'
# 告知插件加入或創建組命名,UUID
group_replication_group_name="14b78170-6fff-43a9-bf74-ea40051ac164"
# server啟動時不自啟組復制,為了避免每次啟動自動引導具有相同名稱的第二個組,所以設置為OFF。
group_replication_start_on_boot=off
# 告訴插件使用IP地址,端口33061用于接收組中其他成員轉入連接
# 注意:此處可以使用ip地址,也可以使用主機名方式
group_replication_local_address="10.0.0.12:33061"
# 啟動組server,種子server,加入組應該連接這些的ip和端口;其他server要加入組得由組成員同意
# 注意:此處可以使用ip地址,也可以使用主機名方式
group_replication_group_seeds="10.0.0.12:33061,10.0.0.15:33061,10.0.0.18:33061"
# 配置此服務器為引導組,這個選項必須僅在一臺服務器上設置,
# 并且僅當第一次啟動組或者重新啟動整個組時。成功引導組啟動后,將此選項設置為關閉。
group_replication_bootstrap_group=off
# 指定當前節點向集群其他成員報告的自身的主機名或 IP 地址,用于成員間通信和連接
# 這是保證集群成員間正確通信的基礎配置。
report_host=10.0.0.12
report_port=3306
eof

啟動服務
systemctl restart mysqld
ss -tunlp | grep mys

?15主機
定制配置文件
cat >> /etc/my.cnf.d/mysql-server.cnf <<- eof
# 為了版本兼容性,更改默認的用戶認證插件
default_authentication_plugin=mysql_native_password
plugin_dir=/usr/lib64/mysql/plugin
# 復制框架
server_id=15
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
plugin_load_add='group_replication.so'
group_replication_group_name="14b78170-6fff-43a9-bf74-ea40051ac164"
group_replication_start_on_boot=off
group_replication_local_address="10.0.0.15:33061"
group_replication_group_seeds="10.0.0.12:33061,10.0.0.15:33061,10.0.0.18:33061"
group_replication_bootstrap_group=off
report_host=10.0.0.15
report_port=3306
eof

啟動服務

18主機
定制配置文件
cat >> /etc/my.cnf.d/mysql-server.cnf <<- eof
# 為了版本兼容性,更改默認的用戶認證插件
default_authentication_plugin=mysql_native_password
plugin_dir=/usr/lib64/mysql/plugin
# 復制框架
server_id=18
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
plugin_load_add='group_replication.so'
group_replication_group_name="14b78170-6fff-43a9-bf74-ea40051ac164"
group_replication_start_on_boot=off
group_replication_local_address="10.0.0.18:33061"
group_replication_group_seeds="10.0.0.12:33061,10.0.0.15:33061,10.0.0.18:33061"
group_replication_bootstrap_group=off
report_host=10.0.0.18
report_port=3306
eof

啟動服務

集群認證配置
12主機設定腳本
cat > cluster_auth_init.sql <<-eof
# 如下操作不記錄二進制日志
SET SQL_LOG_BIN=0;
# 創建rpl_user賬戶,此賬戶用于實現主從數據同步
CREATE USER rpl_user@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
GRANT CONNECTION_ADMIN ON *.* TO rpl_user@'%';
GRANT BACKUP_ADMIN ON *.* TO rpl_user@'%';
GRANT GROUP_REPLICATION_STREAM ON *.* TO rpl_user@'%';
# 創建一個遠程連接用戶,便于圖形化管理工具使用
create user 'remote'@'%' identified with mysql_native_password by '123456';
grant all privileges on *.* to remote@'%';
FLUSH PRIVILEGES;
# 恢復二進制日志功能并且重置二進制日志
SET SQL_LOG_BIN=1;
RESET MASTER;
eof

執行腳本
mysql -uroot -p'123456' < cluster_auth_init.sql

15主機設定腳本
cat > cluster_auth_init.sql <<-eof
SET SQL_LOG_BIN=0;
CREATE USER rpl_user@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
GRANT CONNECTION_ADMIN ON *.* TO rpl_user@'%';
GRANT BACKUP_ADMIN ON *.* TO rpl_user@'%';
GRANT GROUP_REPLICATION_STREAM ON *.* TO rpl_user@'%';
create user 'remote'@'%' identified with mysql_native_password by '123456';
grant all privileges on *.* to remote@'%';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
RESET MASTER;
eof

執行腳本
mysql -uroot -p'123456' < cluster_auth_init.sql

18主機設定腳本
cat > cluster_auth_init.sql <<-eof
> SET SQL_LOG_BIN=0;
> CREATE USER rpl_user@'%' IDENTIFIED BY '123456';
> GRANT REPLICATION SLAVE ON *.* TO rpl_user@'%';
> GRANT CONNECTION_ADMIN ON *.* TO rpl_user@'%';
> GRANT BACKUP_ADMIN ON *.* TO rpl_user@'%';
> GRANT GROUP_REPLICATION_STREAM ON *.* TO rpl_user@'%';
> create user'remote'@'%' identified with mysql_native_password by '123456';
> grant all privileges on *.* to remote@'%';
> FLUSH PRIVILEGES;
> SET SQL_LOG_BIN=1;
> RESET MASTER;
> eof

執行腳本
mysql -uroot -p'123456' < cluster_auth_init.sql

查看集群插件
12主機
mysql -uroot -p'123456' -e "show plugins;"

15主機
mysql -uroot -p'123456' -e "show plugins;"

18主機
mysql -uroot -p'123456' -e "show plugins;"

啟動集群環境
SET GLOBAL group_replication_bootstrap_group=ON;
START GROUP_REPLICATION USER='rpl_user', PASSWORD='123456';
SET GLOBAL group_replication_bootstrap_group=OFF;
