openGauss 主備切換之 switchover 與 failover
switchover
在主備機正常時,出於維護的需要,將備機切換為主機,可保證切換過程中資料不丟失。
檢視叢集例項主備狀態:1 節點為主庫,2 節點為備庫。
$ gs_om -t status --detail
[ Cluster State ]
cluster_state : Normal
redistributing : No
current_az : AZ_ALL
[ Datanode State ]
node node_ip instance state | node node_ip instance state
1 cen7-og1-02 ... 6001 /opt/openGauss/data/dn P Primary Normal | 2 cen7-og1-03 ... 6002 /opt/openGauss/data/dn S Standby Normal
在備節點執行切換主備操作:
$ gs_ctl switchover -D /opt/openGauss/data/dn
[2021-01-15 02:44:55.125][1034][][gs_ctl]: gs_ctl switchover ,datadir is /opt/openGauss/data/dn
[2021-01-15 02:44:55.125][1034][][gs_ctl]: switchover term (1)
[2021-01-15 02:44:55.137][1034][][gs_ctl]: waiting for server to switchover..............
[2021-01-15 02:45:06.250][1034][][gs_ctl]: done
[2021-01-15 02:45:06.250][1034][][gs_ctl]: switchover completed (/opt/openGauss/data/dn)
確認叢集主備狀態:1 節點為備庫,2 節點為主庫。
$ gs_om -t status --detail
[ Cluster State ]
cluster_state : Normal
redistributing : No
current_az : AZ_ALL
[ Datanode State ]
node node_ip instance state | node node_ip instance state
1 cen7-og1-02 ... 6001 /opt/openGauss/data/dn P Standby Normal | 2 cen7-og1-03 ... 6002 /opt/openGauss/data/dn S Primary Normal
儲存資料庫主備機器資訊:確保 gs_om -t refreshconf 命令執行成功,否則再次重啟會影響資料庫狀態。
$ gs_om -t refreshconf
Generating dynamic configuration file for all nodes.
Successfully generated dynamic configuration file.
failover
在主機異常時,將備機切換為主機。
檢視叢集例項主備狀態:1 節點為備庫,2 節點為主庫。
$ gs_om -t status --detail
[ Cluster State ]
cluster_state : Normal
redistributing : No
current_az : AZ_ALL
[ Datanode State ]
node node_ip instance state | node node_ip instance state
1 cen7-og1-02 ... 6001 /opt/openGauss/data/dn P Standby Normal | 2 cen7-og1-03 ... 6002 /opt/openGauss/data/dn S Primary Normal
備節點執行主備切換操作:
$ gs_ctl failover -D /opt/openGauss/data/dn
[2021-01-15 03:33:13.803][17292][][gs_ctl]: gs_ctl failover ,datadir is /opt/openGauss/data/dn
[2021-01-15 03:33:13.803][17292][][gs_ctl]: failover term (1)
[2021-01-15 03:33:13.810][17292][][gs_ctl]: waiting for server to failover...
[2021-01-15 03:33:14.823][17292][][gs_ctl]: done
[2021-01-15 03:33:14.823][17292][][gs_ctl]: failover completed (/opt/openGauss/data/dn)
確認叢集主備狀態:雙主狀態。
$ gs_om -t status --detail
[ Cluster State ]
cluster_state : Unavailable
redistributing : No
current_az : AZ_ALL
[ Datanode State ]
node node_ip instance state | node node_ip instance state
1 cen7-og1-02 ... 6001 /opt/openGauss/data/dn P Primary Normal | 2 cen7-og1-03 ... 6002 /opt/openGauss/data/dn S Primary Normal
在確定降為備機的節點關閉並以 standy 模式啟動 openGauss 服務:
$ gs_ctl start -D /opt/openGauss/data/dn -M standby
[2021-01-15 03:03:08.695][9126][][gs_ctl]: gs_ctl started,datadir is /opt/openGauss/data/dn
[2021-01-15 03:03:08.864][9126][][gs_ctl]: waiting for server to start...
......
[2021-01-15 03:03:10.921][9126][][gs_ctl]: done
[2021-01-15 03:03:10.921][9126][][gs_ctl]: server started (/opt/openGauss/data/dn)
確認叢集狀態:1 節點為主庫,2 節點為備庫,但備庫需要修復。
$ gs_om -t status --detail
[ Cluster State ]
cluster_state : Degraded
redistributing : No
current_az : AZ_ALL
[ Datanode State ]
node node_ip instance state | node node_ip instance state
1 cen7-og1-02 ... 6001 /opt/openGauss/data/dn P Primary Normal | 2 cen7-og1-03 ... 6002 /opt/openGauss/data/dn S Standby Need repair(WAL)
在備庫所在節點執行修復命令:
[omm@cen7-og1-03 ~]$ gs_ctl build -D /opt/openGauss/data/dn
[2021-01-15 03:23:57.702][30784][][gs_ctl]: gs_ctl incremental build ,datadir is /opt/openGauss/data/dn
waiting for server to shut down.... done
server stopped
[2021-01-15 03:23:58.726][30784][][gs_ctl]: fopen build pid file "/opt/openGauss/data/dn/gs_build.pid" success
[2021-01-15 03:23:58.726][30784][][gs_ctl]: fprintf build pid file "/opt/openGauss/data/dn/gs_build.pid" success
[2021-01-15 03:23:58.727][30784][][gs_ctl]: fsync build pid file "/opt/openGauss/data/dn/gs_build.pid" success
......
[2021-01-15 03:24:02.833][30784][dn_6001_6002][gs_ctl]: done
[2021-01-15 03:24:02.833][30784][dn_6001_6002][gs_ctl]: server started (/opt/openGauss/data/dn)
[2021-01-15 03:24:02.833][30784][dn_6001_6002][gs_ctl]: fopen build pid file "/opt/openGauss/data/dn/gs_build.pid" success
[2021-01-15 03:24:02.833][30784][dn_6001_6002][gs_ctl]: fprintf build pid file "/opt/openGauss/data/dn/gs_build.pid" success
[2021-01-15 03:24:02.835][30784][dn_6001_6002][gs_ctl]: fsync build pid file "/opt/openGauss/data/dn/gs_build.pid" success
確認叢集狀態:正常。
$ gs_om -t status --detail
[ Cluster State ]
cluster_state : Normal
redistributing : No
current_az : AZ_ALL
[ Datanode State ]
node node_ip instance state | node node_ip instance state
1 cen7-og1-02 ... 6001 /opt/openGauss/data/dn P Primary Normal | 2 cen7-og1-03 ... 6002 /opt/openGauss/data/dn S Standby Normal
儲存資料庫主備機器資訊:確保 gs_om -t refreshconf 命令執行成功,否則再次重啟會影響資料庫狀態。
$ gs_om -t refreshconf
Generating dynamic configuration file for all nodes.
Successfully generated dynamic configuration file.