作者:楊濤濤
資深資料庫專家,專研 MySQL 十餘年。擅長 MySQL、PostgreSQL、MongoDB 等開源資料庫相關的備份恢復、SQL 調優、監控運維、高可用架構設計等。目前任職於愛可生,為各大運營商及銀行金融企業提供 MySQL 相關技術支持、MySQL 相關課程培訓等工作。
本文來源:原創投稿 *愛可生開源社區出品,原創內容未經授權不得隨意使用,轉載請聯繫小編並註明來源。
本篇是《innodb-cluster 掃盲-安裝篇》的續篇。
我們知道,InnoDB Cluster 是 Oralce 官方發布的用來管理 MySQL 組複製的一套工具,有了 InnoDB Cluster,MySQL 原生組複製的部署、運維、開發等將會變得非常簡單。
InnoDB Cluster 測試環境如下:
節點 A:192.168.2.210:3601(主節點)
節點 B:192.168.2.210:3602(從節點)
節點 C:192.168.2.210:3603(從節點)
接下來,看看 InnoDB Cluster 提供的一系列內置函數如何簡化組複製的日常運維管理。
1、get_cluster 方法
獲取集群對象,方便後續管理。
MySQL debian-ytt1:3601 ssl Py > c1 = dba.get_cluster();MySQL debian-ytt1:3601 ssl Py > c1
2、describe 方法
用來獲取集群目前狀態,輸出極簡結果。
輸出集群節點拓撲圖,集群各個節點 IP 地址等。
MySQL debian-ytt1:3601 ssl Py > c1.describe(){ "clusterName": "ytt_mgr", "defaultReplicaSet": { "name": "default", "topology": [ { "address": "127.0.0.1:3601", "label": "127.0.0.1:3601", "role": "HA" }, { "address": "127.0.0.1:3602", "label": "127.0.0.1:3602", "role": "HA" }, { "address": "127.0.0.1:3603", "label": "127.0.0.1:3603", "role": "HA" } ], "topologyMode": "Single-Primary" }}
3、list_routers 方法
獲取集群當前路由信息。
輸出當前 mysql router 路由信息,包含讀寫埠,版本等信息。
MySQL debian-ytt1:3601 ssl Py > c1.list_routers(){ "clusterName": "ytt_mgr", "routers": { "debian-ytt1::system": { "hostname": "debian-ytt1", "lastCheckIn": "2020-07-15 11:43:18", "roPort": 6447, "roXPort": 64470, "rwPort": 6446, "rwXPort": 64460, "version": "8.0.21" } }}
4、set_option 方法
用來設置集群的全局參數。
比如改變集群的名字:名字由原來 ytt_mgr 更改為 ytt_mgr_sandbox。如下圖:
再比如更改集群事務一致性由默認最終一致性級別更改為強一致性級別,如下圖:
5、options 方法
獲取集群當前所有運行參數的結果,過濾後來驗證下效果,如下圖:
當然也可以從 mysql router 層來驗證效果。如下圖,分別獲取寫節點和讀節點的數據一致性參數結果。
6、set_instance_option 方法
單獨設置某個節點的參數,不過目前還是不完善,不是每個參數都可以設置。
比如設置每個實例的標籤,設置為可讀性較強的標籤,三個節點分別設置為 node_a、node_b、node_c。如下圖:
查看設置好的標籤,查看鍵值為 label 的值。
7、set_primary_instance 方法
提升一個從節點為主節點。
比如把節點 B 提升為主,節點 A 降級為從。如下圖:
來驗證下:此時 node_b 為主節點,node_a 降為從節點。
這時候 mysql router 會自動把寫請求發送到新的主節點 B 上:
root@debian-ytt1:/home/ytt# mysql -uroot -proot -P6446 -hdebian-ytt1 -e "select @@port"mysql: [Warning] Using a password on the command line interface can be insecure.+--------+| @@port |+--------+| 3602 |+--------+
8、switch_to_multi_primary_mode 方法
設置集群模式為多主:
MySQL debian-ytt1:3601 ssl Py > c1.switch_to_multi_primary_mode()Switching cluster 'ytt_mgr_sandbox' to Multi-Primary mode...Instance '127.0.0.1:3601' was switched from SECONDARY to PRIMARY.Instance '127.0.0.1:3602' remains PRIMARY.Instance '127.0.0.1:3603' was switched from SECONDARY to PRIMARY.The cluster successfully switched to Multi-Primary mode.
驗證下當前的集群模式。
所有節點均變為主,如下圖:
9、switch_to_single_primary_mode 方法
切換集群為單主模式,如下圖:
10、status 方法
可以查看集群更加詳細的運行時信息,避免對 performance_schema.replication* 等一系列性能表進行額外查詢。
默認 c1.status 等價於 c1.status({"extended":0})
status() 方法參數為 JSON 對象有四個值:0,1,2,3。
0:禁止列印詳細信息(默認)
1:列印集群元數據相關信息,集群組協議的版本,成員等簡單的信息。
2:列印集群重放線程的相關事務執行信息。
3:列印集群內每個節點所屬詳細的信息。
下圖是執行 c1.status({"extended":2}) 的結果
從結果可以看到目前節點 B 的延遲時間,事務重放的數量,目前重放的 GTID 位置等等。
11、dissolve() 方法
用來解散集群。
dissolve 會刪除集群所有元數據、組複製相關配置數據、組複製相關的日誌文件(重放日誌,恢復日誌等)。
MySQL debian-ytt1:3601 ssl Py > c1.dissolve()...Are you sure you want to dissolve the cluster? [y/N]: yInstance '127.0.0.1:3602' is attempting to leave the cluster...Instance '127.0.0.1:3603' is attempting to leave the cluster...Instance '127.0.0.1:3601' is attempting to leave the cluster...The cluster was successfully dissolved.Replication was disabled but user data was left intact.
總結
MySQL InnoDB Cluster 是一款非常好用的管理 MySQL 組複製的套件,熟悉它的使用方法會極大的簡化我們對組複製的日常管理。