MongoDB 4.2副本集自動故障轉移(一主一副一仲裁)
一、檢查副本集狀態
handong1:PRIMARY> rs.status() { "set" : "handong1", "date" : ISODate("2021-03-18T11:43:08.167Z"), "myState" : 1, "term" : NumberLong(3), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1616067784, 1), "t" : NumberLong(3) }, "lastCommittedWallTime" : ISODate("2021-03-18T11:43:04.894Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1616067784, 1), "t" : NumberLong(3) }, "readConcernMajorityWallTime" : ISODate("2021-03-18T11:43:04.894Z"), "appliedOpTime" : { "ts" : Timestamp(1616067784, 1), "t" : NumberLong(3) }, "durableOpTime" : { "ts" : Timestamp(1616067784, 1), "t" : NumberLong(3) }, "lastAppliedWallTime" : ISODate("2021-03-18T11:43:04.894Z"), "lastDurableWallTime" : ISODate("2021-03-18T11:43:04.894Z") }, "lastStableRecoveryTimestamp" : Timestamp(1616067764, 1), "lastStableCheckpointTimestamp" : Timestamp(1616067764, 1), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2021-03-18T09:05:47.089Z"), "electionTerm" : NumberLong(3), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1616055916, 1), "t" : NumberLong(2) }, "numVotesNeeded" : 1, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "newTermStartDate" : ISODate("2021-03-18T09:05:47.098Z"), "wMajorityWriteAvailabilityDate" : ISODate("2021-03-18T09:05:47.129Z") }, "members" : [ { "_id" : 0, "name" : "172.16.254.131:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 9444, "optime" : { "ts" : Timestamp(1616067784, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2021-03-18T11:43:04Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1616058347, 1), "electionDate" : ISODate("2021-03-18T09:05:47Z"), "configVersion" : 6, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "mongo2:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 8089, "optime" : { "ts" : Timestamp(1616067784, 1), "t" : NumberLong(3) }, "optimeDurable" : { "ts" : Timestamp(1616067784, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2021-03-18T11:43:04Z"), "optimeDurableDate" : ISODate("2021-03-18T11:43:04Z"), "lastHeartbeat" : ISODate("2021-03-18T11:43:07.136Z"), "lastHeartbeatRecv" : ISODate("2021-03-18T11:43:07.113Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "172.16.254.131:27017", "syncSourceHost" : "172.16.254.131:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 6 }, { "_id" : 2, "name" : "mongo3:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 8062, "lastHeartbeat" : ISODate("2021-03-18T11:43:07.135Z"), "lastHeartbeatRecv" : ISODate("2021-03-18T11:43:06.741Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 6 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1616067784, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1616067784, 1) }
二、測試複製
172.16.254.131
handong1:PRIMARY> use db3 switched to db db3 handong1:PRIMARY> db.db3.insert({"name":"jianghe"}) WriteResult({ "nInserted" : 1 }) handong1:PRIMARY> db.db3.find() { "_id" : ObjectId("6051d08ac74d627f4b561349"), "name" : "mongodb" } { "_id" : ObjectId("6053225bdebdeda2639f026e"), "name" : "mongodb4.2" } { "_id" : ObjectId("60533d88504b2400d4ae3091"), "name" : "jianghe" }
172.16.254.132
handong1:SECONDARY> rs.secondaryOk() handong1:SECONDARY> db.db3.find() { "_id" : ObjectId("6051d08ac74d627f4b561349"), "name" : "mongodb" } { "_id" : ObjectId("6053225bdebdeda2639f026e"), "name" : "mongodb4.2" } { "_id" : ObjectId("60533d88504b2400d4ae3091"), "name" : "jianghe" }
目前測試正常。
三、測試故障轉移
1、關閉主節點伺服器
2、檢視叢集狀態
172.16.254.132
handong1:PRIMARY> rs.status() { "set" : "handong1", "date" : ISODate("2021-03-18T13:05:51.862Z"), "myState" : 1, "term" : NumberLong(5), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1616071857, 1), "t" : NumberLong(3) }, "lastCommittedWallTime" : ISODate("2021-03-18T12:50:57.574Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1616071857, 1), "t" : NumberLong(3) }, "readConcernMajorityWallTime" : ISODate("2021-03-18T12:50:57.574Z"), "appliedOpTime" : { "ts" : Timestamp(1616072744, 1), "t" : NumberLong(5) }, "durableOpTime" : { "ts" : Timestamp(1616072744, 1), "t" : NumberLong(5) }, "lastAppliedWallTime" : ISODate("2021-03-18T13:05:44.997Z"), "lastDurableWallTime" : ISODate("2021-03-18T13:05:44.997Z") }, "lastStableRecoveryTimestamp" : Timestamp(1616071857, 1), "lastStableCheckpointTimestamp" : Timestamp(1616071857, 1), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2021-03-18T12:51:14.081Z"), "electionTerm" : NumberLong(5), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(1616071857, 1), "t" : NumberLong(3) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1616071857, 1), "t" : NumberLong(3) }, "numVotesNeeded" : 2, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "numCatchUpOps" : NumberLong(0), "newTermStartDate" : ISODate("2021-03-18T12:51:14.471Z") }, "members" : [ { "_id" : 0, "name" : "172.16.254.131:27017", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDurable" : { "ts" : Timestamp(0, 0), "t" : NumberLong(-1) }, "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2021-03-18T13:05:42.846Z"), "lastHeartbeatRecv" : ISODate("2021-03-18T12:51:03.822Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "Error connecting to 172.16.254.131:27017 :: caused by :: No route to host", "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : -1 }, { "_id" : 1, "name" : "mongo2:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 14396, "optime" : { "ts" : Timestamp(1616072744, 1), "t" : NumberLong(5) }, "optimeDate" : ISODate("2021-03-18T13:05:44Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1616071874, 1), "electionDate" : ISODate("2021-03-18T12:51:14Z"), "configVersion" : 6, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 2, "name" : "mongo3:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 13029, "lastHeartbeat" : ISODate("2021-03-18T13:05:51.848Z"), "lastHeartbeatRecv" : ISODate("2021-03-18T13:05:51.782Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 6 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1616072744, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1616072744, 1) }
可以看到原來的primary已經not reachable, 172.16.254.132原來昨晚recondary(副本)現在變成了primary。
測試副本集可用性
[mongodb@mongo2 ~]$ mongo --port 27017 handong1:PRIMARY> db.db3.insert({"name":"dandan"}) WriteResult({ "nInserted" : 1 }) handong1:PRIMARY> db.db3.find() { "_id" : ObjectId("6051d08ac74d627f4b561349"), "name" : "mongodb" } { "_id" : ObjectId("6053225bdebdeda2639f026e"), "name" : "mongodb4.2" } { "_id" : ObjectId("60533d88504b2400d4ae3091"), "name" : "jianghe" } { "_id" : ObjectId("60534e662b429cdfe156324a"), "name" : "dandan" }
可以看到當前的primary已經可以進行讀寫操作。
此時啟動 172.16.254.131伺服器,並啟動mongodb服務。
在 172.16.254.131檢視副本集資訊:
handong1:PRIMARY> rs.status() { "set" : "handong1", "date" : ISODate("2021-03-18T13:35:29.218Z"), "myState" : 1, "term" : NumberLong(6), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1616074523, 1), "t" : NumberLong(6) }, "lastCommittedWallTime" : ISODate("2021-03-18T13:35:23.542Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1616074523, 1), "t" : NumberLong(6) }, "readConcernMajorityWallTime" : ISODate("2021-03-18T13:35:23.542Z"), "appliedOpTime" : { "ts" : Timestamp(1616074523, 1), "t" : NumberLong(6) }, "durableOpTime" : { "ts" : Timestamp(1616074523, 1), "t" : NumberLong(6) }, "lastAppliedWallTime" : ISODate("2021-03-18T13:35:23.542Z"), "lastDurableWallTime" : ISODate("2021-03-18T13:35:23.542Z") }, "lastStableRecoveryTimestamp" : Timestamp(1616074513, 1), "lastStableCheckpointTimestamp" : Timestamp(1616074513, 1), "electionCandidateMetrics" : { "lastElectionReason" : "priorityTakeover", "lastElectionDate" : ISODate("2021-03-18T13:34:33.453Z"), "electionTerm" : NumberLong(6), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(1616074466, 1), "t" : NumberLong(5) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1616074466, 1), "t" : NumberLong(5) }, "numVotesNeeded" : 2, "priorityAtElection" : 2, "electionTimeoutMillis" : NumberLong(10000), "priorPrimaryMemberId" : 1, "numCatchUpOps" : NumberLong(0), "newTermStartDate" : ISODate("2021-03-18T13:34:33.518Z"), "wMajorityWriteAvailabilityDate" : ISODate("2021-03-18T13:34:34.476Z") }, "members" : [ { "_id" : 0, "name" : "172.16.254.131:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 71, "optime" : { "ts" : Timestamp(1616074523, 1), "t" : NumberLong(6) }, "optimeDate" : ISODate("2021-03-18T13:35:23Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1616074473, 1), "electionDate" : ISODate("2021-03-18T13:34:33Z"), "configVersion" : 6, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "mongo2:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 67, "optime" : { "ts" : Timestamp(1616074523, 1), "t" : NumberLong(6) }, "optimeDurable" : { "ts" : Timestamp(1616074523, 1), "t" : NumberLong(6) }, "optimeDate" : ISODate("2021-03-18T13:35:23Z"), "optimeDurableDate" : ISODate("2021-03-18T13:35:23Z"), "lastHeartbeat" : ISODate("2021-03-18T13:35:27.692Z"), "lastHeartbeatRecv" : ISODate("2021-03-18T13:35:29.131Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "172.16.254.131:27017", "syncSourceHost" : "172.16.254.131:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 6 }, { "_id" : 2, "name" : "mongo3:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 67, "lastHeartbeat" : ISODate("2021-03-18T13:35:27.692Z"), "lastHeartbeatRecv" : ISODate("2021-03-18T13:35:28.445Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 6 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1616074523, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1616074523, 1) }
可以看到
172.16.254.131上的主節點又變成了primary。
在 172.16.254.131上插入資料:
handong1:PRIMARY> use db3 switched to db db3 handong1:PRIMARY> db.db3.insert({"name":"doudou"}) WriteResult({ "nInserted" : 1 }) handong1:PRIMARY> db.db3.find() { "_id" : ObjectId("6051d08ac74d627f4b561349"), "name" : "mongodb" } { "_id" : ObjectId("6053225bdebdeda2639f026e"), "name" : "mongodb4.2" } { "_id" : ObjectId("60533d88504b2400d4ae3091"), "name" : "jianghe" } { "_id" : ObjectId("60534e662b429cdfe156324a"), "name" : "dandan" } { "_id" : ObjectId("605359a1ab6994b3520da444"), "name" : "doudou" }
在
172.16.254.132查詢資料:
handong1:SECONDARY> use db3 switched to db db3 handong1:SECONDARY> rs.secondaryOk() handong1:SECONDARY> db.db3.find() { "_id" : ObjectId("6051d08ac74d627f4b561349"), "name" : "mongodb" } { "_id" : ObjectId("6053225bdebdeda2639f026e"), "name" : "mongodb4.2" } { "_id" : ObjectId("60533d88504b2400d4ae3091"), "name" : "jianghe" } { "_id" : ObjectId("60534e662b429cdfe156324a"), "name" : "dandan" } { "_id" : ObjectId("605359a1ab6994b3520da444"), "name" : "doudou" }
可以看到,副本集資料恢復了同步。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30135314/viewspace-2763687/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MongoDB 4.2副本集新增/刪除副本(一主一副一仲裁)MongoDB
- MongoDB副本集(一主一備+仲裁)環境部署-運維操作記錄MongoDB運維
- MongoDB副本集(一主兩從)讀寫分離、故障轉移功能環境部署記錄MongoDB
- MongoDB副本集新增和刪除仲裁節點一例MongoDB
- 4.2.13 主備庫實現自動故障轉移
- mongodb主從仲裁節點配置MongoDB
- keepalive配置mysql自動故障轉移MySql
- MongoDB副本集故障恢復機制概述MongoDB
- Windows Server2012 故障轉移叢集之動態仲裁(Dynamic Quorum)WindowsServer
- MongoDB主從複製,副本集, ShardingMongoDB
- MongoDB日常運維-05副本集故障切換MongoDB運維
- MongoDB副本集MongoDB
- mongodb 啟動故障MongoDB
- 銀河麒麟系統安裝mongodb副本集故障處理MongoDB
- mongoDB研究筆記:複製集故障轉移機制MongoDB筆記
- MongoDB 副本集搭建MongoDB
- MongoDB部署副本集MongoDB
- MongoDB 副本集管理MongoDB
- MongoDB之副本集MongoDB
- 再看MongoDB副本集MongoDB
- 搭建MongoDB副本集MongoDB
- Mongodb主從複製/ 副本集/分片叢集介紹MongoDB
- MongoDB副本集節點的優先值修改一例MongoDB
- MongoDB - 副本集簡介MongoDB
- MongoDB副本集實踐MongoDB
- 如何配置 MongoDB 副本集MongoDB
- 【ASK_ORACLE】手動配置DataGuard的自動化Client Failover(故障轉移)的serviceOracleclientAI
- MongoDB副本集replica set (二)--副本集環境搭建MongoDB
- mongodb 4.0副本集搭建MongoDB
- MongoDB 副本集原理及管理MongoDB
- MongoDB 副本集切換方法MongoDB
- 006.MongoDB副本集MongoDB
- MongoDB 6.0.3副本集搭建MongoDB
- Zookeeper的一次遷移故障
- 小丸子學MongoDB系列之——部署MongoDB副本集MongoDB
- 各種主機部署故障轉移Failover的詳細配置AI
- 從一次故障聊聊前端 UI 自動化測試前端UI
- PostgreSQL中利用驅動程式實現故障轉移SQL