mysql8搭建innodb_cluster集群


mysql版本 : mysql8
集群所需最少實例 : 三台

服務器IP及主機名:
10.10.10.10 test10
10.10.10.11 test11
10.10.10.12 test12

集群管理工具 : mysql-shell / mysql-route


  • 添加hosts記錄(添加集群以及數據同步通過主機名交互數據)
cat >> /etc/hosts << EOF
10.10.10.10 mysql10 test10
10.10.10.11 mysql11 test11
10.10.10.12 mysql12 test12
EOF
  • 修改mysql配置項(添加集群以及組復制設置)
###修改配置,允許組建集群
performance_schema = 1
server_id = 10
gtid_mode = on
enforce_gtid_consistency = 1
###配置組復制設置
cat >> /usr/local/mysql/etc/my.cnf << EOF
####innodb clust
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 = "10.10.10.10:33306"
loose-group_replication_group_seeds = "10.10.10.10:33306,10.10.10.11:33306,10.10.10.12:33306"
loose-group_replication_bootstrap_group = off
loose-group_replication_ip_whitelist = "10.10.10.0/24"
#loose-group_replication_single_primary_mode = on
EOF

根據不同服務器設備不同的配置項{server_id , loose-group_replication_local_address}
loose-group_replication_single_primary_mode = on 單主模式, off 多主模式

  • 創建集群所需賬戶(三台主機)
mysql -uroot -p123456
 
set sql_log_bin=0;
CREATE USER 'cluster'@'10.10.10.%' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'cluster'@'10.10.10.%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
set sql_log_bin=1;

停用binlog日志創建用戶
因為該賬戶需要創建其他集群所需賬戶,所以需要賦予管理權限WITH GRANT OPTION
需要加強權限策略,建議查詢具體權限要求。

  • 管理機上安裝集群管理工具
rpm -ivh mysql-router*
rpm -ivh mysql-shell*
  • 設置mysql集群
###配置各服務器為集群模式
shell.connect('cluster@mysql10:3306');
dba.configureLocalInstance();
 
shell.connect('cluster@mysql11:3306');
dba.configureLocalInstance();
 
shell.connect('cluster@mysql12:3306');
dba.configureLocalInstance();
 
#######################################
 
###創建集群組,並將添加示例進集群組
shell.connect('cluster@mysql10:3306');
var cluster = dba.createCluster('sqlcluster');
#將另外兩台實例添加至集群中
cluster.addInstance('cluster@mysql11:3306');
cluster.addInstance('cluster@mysql12:3306');
cluster.status();         #查看集群狀態
  • 設置集群路由
mysqlrouter --bootstrap cluster@mysql10:3306 --user=cluster
  • 查看集群狀態
#無權查看集群狀態
dba.getCluster();     #查看創建的集群
cluster=dba.getCluster();    #獲取當前集群
cluster.status();         #查看集群狀態
  • 查詢節點名稱()
mysql -uroot -p123456
 
> SELECT clusters.cluster_id,clusters.cluster_name from mysql_innodb_cluster_metadata.clusters
節點狀態 狀態描述
ONLINE 節點狀態正常。
OFFLINE 實例在運行,但沒有加入任何Cluster。
RECOVERING 實例已加入Cluster,正在同步數據。
ERROR 同步數據發生異常。
UNREACHABLE 與其他節點通訊中斷,可能是網絡問題,可能是節點crash。
MISSING 節點已加入集群,但未啟動group replication
集群狀態 狀態描述
OK 所有節點處於online狀態,有冗余節點。
OK_PARTIAL 有節點不可用,但仍有冗余節點。
OK_NO_TOLERANCE 有足夠的online節點,但沒有冗余,例如:兩個節點的Cluster,其中一個掛了,集群就不可用了。
NO_QUORUM 節點處於online狀態,但達不到法定節點數,此狀態下Cluster無法寫入,只能讀取。
UNKNOWN 不是online或recovering狀態,嘗試連接其他實例查看狀態。
UNAVAILABLE 組內節點全是offline狀態,但實例在運行,可能實例剛重啟還沒加入Cluster。


mysqlsh常用命令 (mysqlsh的JS語法)

dba.checkInstanceConfiguration("root@hostname:3306")     #檢查節點配置實例,用於加入cluster之前
 
dba.rebootClusterFromCompleteOutage('myCluster');        #重啟
 
dba.dropMetadataSchema();                                #刪除schema
 
var cluster = dba.getCluster('myCluster')                #獲取當前集群
 
cluster.checkInstanceState("root@hostname:3306")         #檢查cluster里節點狀態
 
cluster.rejoinInstance("root@hostname:3306")             #重新加入節點,我本地測試的時候發現rejoin一直無效,每次是delete后
 
addcluster.dissolve({force:true})                       #刪除集群
 
cluster.addInstance("root@hostname:3306")                #增加節點
 
cluster.removeInstance("root@hostname:3306")             #刪除節點
 
cluster.removeInstance('root@host:3306',{force:true})    #強制刪除節點
 
cluster.dissolve({force:true})                           #解散集群
 
cluster.describe();                                      #集群描述


常見故障處理

  • 節點服務器重啟后未加入集群(重新加入集群)(多出現主節點)

現象 : "status": "(MISSING)"
執行cluster.rejoinInstance

shell.connect('cluster@mysql10:3306');
cluster=dba.getCluster();
cluster.rejoinInstance("root@mysql12:3306")
  • 集群中所有服務器重啟,所有節點都offline,直接獲取集群信息失敗

查詢數據庫SELECT * FROM performance_schema.replication_group_members;
僅顯示單機 'MEMBER_STATE' = 'offline'
使用SELECT clusters.cluster_id,clusters.cluster_name from mysql_innodb_cluster_metadata.clusters活着集群名稱
執行rebootClusterFromCompleteOutage命令,恢復集群

shell.connect('cluster@mysql10:3306');
dba.rebootClusterFromCompleteOutage('sqlcluster');


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM