一、概述
MogDB的CM特性:
??CM(Cluster Manager)是一款數(shù)據(jù)庫集群資源管理軟件,提供數(shù)據(jù)庫主備狀態(tài)監(jiān)控、網(wǎng)絡(luò)通信故障監(jiān)控、文件系統(tǒng)故障監(jiān)控、故障自動主備切換等能力,支持集群、節(jié)點、實例級的啟停,集群狀態(tài)查詢、主備切換、日志管理等功能。分為四大模塊:cm_server, cm_agent, om_monitor, cm_ctl。它們的關(guān)系如下圖所示:
![image.png]
各個模塊的功能簡單介紹:
cm_ctl:CM集群管理軟件的客戶端工具.
cm_server:
??(1)向各個節(jié)點上cm_agent發(fā)送指令(如數(shù)據(jù)庫實例的啟動、停止、狀態(tài)查詢、switchover和failover),并接收響應(yīng);
??(2)接收各個節(jié)點上cm_agent上報的數(shù)據(jù)庫實例狀態(tài)信息;
??(3)各個節(jié)點上cm_server自身高可用仲裁;
??(4)各個節(jié)點上數(shù)據(jù)庫實例的高可用仲裁。
cm_agent:
??(1)接收并執(zhí)行cm_server下發(fā)的指令,如數(shù)據(jù)庫實例的啟動、停止、狀態(tài)查詢、switchover和failover;
??(2)監(jiān)控本節(jié)點上運行的數(shù)據(jù)庫實例的狀態(tài)并上報給cm_server。
注意:一主一備模式下,CM只支持基本的安裝,啟停,檢測能力,其他功能不支持。
二、安裝部署MogDB3.0.1一主兩備環(huán)境
ptk安裝部署MogDB一主兩備環(huán)境:
??對于MogDB安裝前的操作系統(tǒng)配置這里就不做贅述,這里直接使用ptk快速搭建MogDB一主兩備的集群環(huán)境,如果在安裝之前,你不確定自己的配置是否全面,是否會影響MogDB的安裝,那么可以通過以下命令來對操作系統(tǒng)相關(guān)配置做檢查和修復(fù):
# 1.檢查操作系統(tǒng)配置,并生成修復(fù)腳本,執(zhí)行完之后會在當(dāng)前目錄下生成一個shell腳本
# 當(dāng)然,該腳本只會在命令執(zhí)行的主機上產(chǎn)生,其他主機則需要手工拷貝過去
ptk checkos --gen-warning-fix -f config.yaml
# 2.拷貝腳本文件到其他主機
scp root_fix_os.2022.0815.154346.sh 10.0.0.202:~
scp root_fix_os.2022.0815.154346.sh 10.0.0.203:~
# 3.在每臺主機上執(zhí)行該腳本:
sh root_fix_os.2022.0815.154346.sh
1.下載ptk:
[root@mogdb-test1 ~]$ curl --proto '=https' --tlsv1.2 -sSf https://cdn-mogdb.enmotech.com/ptk/install.sh | sh
Downloading ptk package...
Detected shell: bash
Shell profile: /root/.bash_profile
ptk has been added to PATH in /root/.bash_profile
open a new terminal or source /root/.bash_profile to active it
Installed path: /root/.ptk/bin/ptk
# 重新加載配置文件.bash_profile,使得環(huán)境變量PATH設(shè)置成功
[root@mogdb-test1 ~]$ source .bash_profile
# 查看一下ptk的版本信息:
[root@mogdb-test1 ~]$ ptk -v
PTK Version: v0.3.4
Go Version: go1.17.1
Build Date: 2022-08-13T08:41:23Z
Git Hash: 273b260
2.生成安裝拓?fù)渑渲梦募?/h4>
# ptk template命令的作用就是打印出默認(rèn)的模板配置信息。然后我們將輸出信息
# 重定向到config.yaml文件中
ptk template > config.yaml
# 編輯config.yaml文件(一主兩備配置文件)
vim config.yaml
global:
cluster_name: "mogdb_cluster"
user: "omm"
group: "omm"
db_port: 26000
# 如果需要安裝CM工具,需要設(shè)置該參數(shù)的值為yes,默認(rèn)不安裝CM工具
enable_cm: yes
cm_server_port: 16000
base_dir: "/opt/mogdb3.0.1"
cm_dir: "/opt/mogdb3.0.1/cm"
# 由于其他主機的root用戶和密碼都是一樣的,所以這里就將ssh連接信息配置在global下共用。
# 如果連接信息不一樣,需要在每個host下單獨配置。
ssh_option:
port: 22
user: root
## SSH password, use 'ptk encrypt' to encrypt it if not empty,root用戶密碼需要通過提示命令加密之后復(fù)制過來
password: "pTk6ZTIzYTM5NGY8RD1BPUE/QTIxbVRMYkpnNFVBd3lvUkI5QXh5UHQydXYwWk5YaHRPOTVBMnowQlZIc1U="
db_servers:
- host: "10.0.0.201"
db_port: 26000
ha_port: 26001
# # database role, options: primary, standby, cascade_standby
role: "primary"
- host: "10.0.0.202"
db_port: 26000
ha_port: 26001
role: "standby"
- host: "10.0.0.203"
db_port: 26000
ha_port: 26001
role: "standby"
3.執(zhí)行安裝
# 如果沒有通過-p選項來指定MogDB安裝包的路徑,那么默認(rèn)會到MogDB官網(wǎng)上下載并安裝
# 默認(rèn)版本的MogDB安裝包。(ptk install -h命令輸出中:--db-version選項描述中有默認(rèn)版本信息)
ptk install -f config.yaml
# 如果輸出信息最后顯示如下,表示安裝成功(省略了前面大部分信息):
INFO[2022-08-15T16:10:28.476] [stage=launch]: successful host=10.0.0.201
INFO[2022-08-15T16:10:28.476] primary startuped, start build current db host=10.0.0.202
INFO[2022-08-15T16:10:28.476] build 10.0.0.202 database by gs_ctl host=10.0.0.202
? executing...INFO[2022-08-15T16:10:28.476] primary startuped, start build current db host=10.0.0.203
INFO[2022-08-15T16:10:28.476] build 10.0.0.203 database by gs_ctl host=10.0.0.203
INFO[2022-08-15T16:10:50.609] [stage=launch]: successful host=10.0.0.203
INFO[2022-08-15T16:10:55.042] [stage=launch]: successful host=10.0.0.202
INFO[2022-08-15T16:10:55.042] Time elapsed: 4m25s
cluste_name | host | user | port | stage | status | message
----------------+------------+------+-------+--------+---------------+----------
mogdb_cluster | 10.0.0.201 | omm | 26000 | launch | start_success | success
| 10.0.0.203 | omm | 26000 | launch | start_success | success
| 10.0.0.202 | omm | 26000 | launch | start_success | success
# 從輸出信息可以看到,在我的機器上一主兩備的搭建也是花費了4m25s
# ptk template命令的作用就是打印出默認(rèn)的模板配置信息。然后我們將輸出信息
# 重定向到config.yaml文件中
ptk template > config.yaml
# 編輯config.yaml文件(一主兩備配置文件)
vim config.yaml
global:
cluster_name: "mogdb_cluster"
user: "omm"
group: "omm"
db_port: 26000
# 如果需要安裝CM工具,需要設(shè)置該參數(shù)的值為yes,默認(rèn)不安裝CM工具
enable_cm: yes
cm_server_port: 16000
base_dir: "/opt/mogdb3.0.1"
cm_dir: "/opt/mogdb3.0.1/cm"
# 由于其他主機的root用戶和密碼都是一樣的,所以這里就將ssh連接信息配置在global下共用。
# 如果連接信息不一樣,需要在每個host下單獨配置。
ssh_option:
port: 22
user: root
## SSH password, use 'ptk encrypt' to encrypt it if not empty,root用戶密碼需要通過提示命令加密之后復(fù)制過來
password: "pTk6ZTIzYTM5NGY8RD1BPUE/QTIxbVRMYkpnNFVBd3lvUkI5QXh5UHQydXYwWk5YaHRPOTVBMnowQlZIc1U="
db_servers:
- host: "10.0.0.201"
db_port: 26000
ha_port: 26001
# # database role, options: primary, standby, cascade_standby
role: "primary"
- host: "10.0.0.202"
db_port: 26000
ha_port: 26001
role: "standby"
- host: "10.0.0.203"
db_port: 26000
ha_port: 26001
role: "standby"
# 如果沒有通過-p選項來指定MogDB安裝包的路徑,那么默認(rèn)會到MogDB官網(wǎng)上下載并安裝
# 默認(rèn)版本的MogDB安裝包。(ptk install -h命令輸出中:--db-version選項描述中有默認(rèn)版本信息)
ptk install -f config.yaml
# 如果輸出信息最后顯示如下,表示安裝成功(省略了前面大部分信息):
INFO[2022-08-15T16:10:28.476] [stage=launch]: successful host=10.0.0.201
INFO[2022-08-15T16:10:28.476] primary startuped, start build current db host=10.0.0.202
INFO[2022-08-15T16:10:28.476] build 10.0.0.202 database by gs_ctl host=10.0.0.202
? executing...INFO[2022-08-15T16:10:28.476] primary startuped, start build current db host=10.0.0.203
INFO[2022-08-15T16:10:28.476] build 10.0.0.203 database by gs_ctl host=10.0.0.203
INFO[2022-08-15T16:10:50.609] [stage=launch]: successful host=10.0.0.203
INFO[2022-08-15T16:10:55.042] [stage=launch]: successful host=10.0.0.202
INFO[2022-08-15T16:10:55.042] Time elapsed: 4m25s
cluste_name | host | user | port | stage | status | message
----------------+------------+------+-------+--------+---------------+----------
mogdb_cluster | 10.0.0.201 | omm | 26000 | launch | start_success | success
| 10.0.0.203 | omm | 26000 | launch | start_success | success
| 10.0.0.202 | omm | 26000 | launch | start_success | success
# 從輸出信息可以看到,在我的機器上一主兩備的搭建也是花費了4m25s
注:執(zhí)行完該命令之后,記得觀察輸出信息,通過輸出信息我們會發(fā)現(xiàn)ptk工具在我們的系統(tǒng)上干了哪些事。
補充:
簡單總結(jié)一下,執(zhí)行上述命令大致做了如下事情:
(1)檢查操作系統(tǒng)配置信息
(2)下載MogDB安裝包
(3)詢問你拓?fù)渑渲梦募畔⑹欠裾_
(4)開始安裝(例如:分發(fā)MogDB安裝包,創(chuàng)建用戶和用戶組,創(chuàng)建相關(guān)目錄,初始化數(shù)據(jù)庫等等)
(5)啟動數(shù)據(jù)庫,先啟動主庫,然后重建備庫,并啟動備庫
更加詳細(xì)信息請參考執(zhí)行命令的輸出信息。
??命令執(zhí)行成功之后會創(chuàng)建omm用戶,并在omm用戶家目錄下創(chuàng)建一個名為.ptk_mogdb_env隱藏文件,內(nèi)容為與MogDB相關(guān)的各種環(huán)境變量信息(值得注意的是,該文件中設(shè)置了PGPORT,PGUSER,PGDATABASE環(huán)境變量,使得我們切換到omm用戶只需輸入gsql就可以連接到數(shù)據(jù)庫),并且會在omm用戶家目錄下的.bashrc配置文件中添加一條命令:
[ -f /home/omm/.ptk_mogdb_env ] && . /home/omm/.ptk_mogdb_env # ptk added
如此,我們每次切換到omm用戶時,這些環(huán)境變量就會被設(shè)置。
當(dāng)然為了更好的在gsql端中編輯命令,可以在上述文件中添加一條命令:
alias gsql='gsql -r'
4.查看相關(guān)服務(wù)監(jiān)聽狀態(tài)
su - omm
netstat -lntup | grep -E '(mogdb|cm+)'
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 0.0.0.0:26000 0.0.0.0:* LISTEN 18982/mogdb
tcp 0 0 0.0.0.0:26001 0.0.0.0:* LISTEN 18982/mogdb
tcp 0 0 10.0.0.201:26005 0.0.0.0:* LISTEN 18982/mogdb
tcp 0 0 10.0.0.201:16000 0.0.0.0:* LISTEN 18971/cm_server
tcp 0 0 10.0.0.201:16001 0.0.0.0:* LISTEN 18971/cm_server
tcp6 0 0 :::26000 :::* LISTEN 18982/mogdb
tcp6 0 0 :::26001 :::* LISTEN 18982/mogdb
# 查看CM相關(guān)進(jìn)程信息
ps aux | grep -E c[m]+
omm 7019 0.3 0.5 44564 11620 ? S 09:30 0:08 /opt/mogdb3.0.1/app/bin/om_monitor -L /opt/mogdb3.0.1/log/cm/om_monitor
omm 7020 2.4 0.7 856040 15140 ? Sl 09:30 0:50 /opt/mogdb3.0.1/app/bin/cm_agent
omm 7033 14.0 19.4 5223844 395472 ? Sl 09:30 4:48 /opt/mogdb3.0.1/app/bin/cm_server
注意:通過觀察我們可以發(fā)現(xiàn),om_monitor、cm_agent、cm_server進(jìn)程在每個節(jié)點上都有運行,其中cm_server進(jìn)程在一個節(jié)點上是primary,其他節(jié)點的角色為standby。從而也間接的反應(yīng)了cm_sever不僅可以自身高可用仲裁,也可以保證數(shù)據(jù)庫集群的高可用仲裁。
三、通過CM工具管理MogDB集群
1,查看數(shù)據(jù)庫集群狀態(tài)信息
cm_ctl query -v -C
[ CMServer State ]
node instance state
-------------------------------
1 mogdb-test1 1 Primary
2 mogdb-test2 2 Standby
3 mogdb-test3 3 Standby
[ Cluster State ]
cluster_state : Normal
redistributing : No
balanced : Yes
current_az : AZ_ALL
[ Datanode State ]
node instance state | node instance state | node instance state
---------------------------------------------------------------------------------------------------------------------------------
1 mogdb-test1 6001 P Primary Normal | 2 mogdb-test2 6002 S Standby Normal | 3 mogdb-test3 6003 S Standby Normal
# 注:使用-C選項時,需要加上-v選項
2.通過cm_ctl工具關(guān)閉指定數(shù)據(jù)庫實例
cm_ctl stop -n 1 -D /opt/mogdb3.0.1/data/
# 注:-n選項用于指定節(jié)點id,也就是查看數(shù)據(jù)庫集群狀態(tài)信息時node項對應(yīng)的值。
# -D選項用于指定節(jié)點的數(shù)據(jù)目錄,對于單個節(jié)點或?qū)嵗耐V姑畈荒軒?m選項來指定關(guān)閉模式。
# 再次查看數(shù)據(jù)庫集群的狀態(tài)
cm_ctl query -v -C
[ CMServer State ]
node instance state
-------------------------------
1 mogdb-test1 1 Primary
2 mogdb-test2 2 Standby
3 mogdb-test3 3 Standby
[ Cluster State ]
cluster_state : Degraded
redistributing : No
balanced : No
current_az : AZ_ALL
[ Datanode State ]
node instance state | node instance state | node instance state
---------------------------------------------------------------------------------------------------------------------------------
1 mogdb-test1 6001 P Down Manually stopped | 2 mogdb-test2 6002 S Primary Normal | 3 mogdb-test3 6003 S Standby Normal
# 可以發(fā)現(xiàn),節(jié)點1已經(jīng)關(guān)閉,其他兩節(jié)點運行正常,整個數(shù)據(jù)庫集群的狀態(tài)變?yōu)镈egraded。節(jié)點2的角色由standby變?yōu)閜rimary.
# 節(jié)點2可以正常寫入數(shù)據(jù)。此時cm_server在節(jié)點1上仍處于運行的狀態(tài),且角色還是為primary.
# 上述表明了,cm_server可以對數(shù)據(jù)庫集群進(jìn)行高可用仲裁。
補充: 當(dāng)集群中主庫(節(jié)點1)down掉之后,節(jié)點2上的cm_server進(jìn)程會向cm_agent進(jìn)程發(fā)送指令進(jìn)行故障轉(zhuǎn)移,cm_agent就會調(diào)用gs_ctl工具進(jìn)行故障轉(zhuǎn)移。故障轉(zhuǎn)移成功之后節(jié)點2就變成了主庫。在節(jié)點3上,cm_server會向cm_agent發(fā)送指令,重新啟動節(jié)點3,并會調(diào)用gs_ctl工具執(zhí)行notify子命令,探知當(dāng)前集群主庫在哪個服務(wù)器上,然后重新指定主備關(guān)系。節(jié)點1(原主庫)啟動過程和節(jié)點2一樣,角色會變?yōu)閟tandby. cm_agent進(jìn)程對于系統(tǒng)工具的調(diào)用日志會記錄在/opt/mogdb3.0.1/cm/cm_agent/log/system_call-current.log文件下。
3.啟動指定的數(shù)據(jù)庫實例
cm_ctl start -n 1 -D /opt/mogdb3.0.1/data
# 再次查看數(shù)據(jù)庫集群的狀態(tài)
cm_ctl query -v -C
[ CMServer State ]
node instance state
-------------------------------
1 mogdb-test1 1 Primary
2 mogdb-test2 2 Standby
3 mogdb-test3 3 Standby
[ Cluster State ]
cluster_state : Normal
redistributing : No
balanced : No
current_az : AZ_ALL
[ Datanode State ]
node instance state | node instance state | node instance state
---------------------------------------------------------------------------------------------------------------------------------
1 mogdb-test1 6001 P Standby Normal | 2 mogdb-test2 6002 S Primary Normal | 3 mogdb-test3 6003 S Standby Normal
# 節(jié)點1的角色變?yōu)閟tandby
4.停止整個數(shù)據(jù)庫集群
cm_ctl stop -z AZ1 -m smart
# -z選項指定整個數(shù)據(jù)庫集群的AZ名字,-m選項指定關(guān)閉的模式(smart表示等待用戶業(yè)務(wù)結(jié)束之后,關(guān)閉所有的數(shù)據(jù)庫實例)
??更多詳細(xì)信息請查看cm_ctl幫助信息(cm_ctl --help)。




