MongoDB 4.2副本集新增/刪除副本(一主一副一仲裁)
一、修改/etc/hosts
172.16.254.131 mongo1 172.16.254.132 mongo2 172.16.254.133 mongo3 172.16.254.134 mongo4
二、新增副本
172.16.254.131
handong1:PRIMARY> rs.add("mongo4:27017") { "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1616077141, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1616077141, 1) }
檢視副本集資訊:
handong1:PRIMARY> rs.status() { "set" : "handong1", "date" : ISODate("2021-03-18T14:19:08.014Z"), "myState" : 1, "term" : NumberLong(6), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 3, "writeMajorityCount" : 3, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1616077141, 1), "t" : NumberLong(6) }, "lastCommittedWallTime" : ISODate("2021-03-18T14:19:01.368Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1616077141, 1), "t" : NumberLong(6) }, "readConcernMajorityWallTime" : ISODate("2021-03-18T14:19:01.368Z"), "appliedOpTime" : { "ts" : Timestamp(1616077141, 1), "t" : NumberLong(6) }, "durableOpTime" : { "ts" : Timestamp(1616077141, 1), "t" : NumberLong(6) }, "lastAppliedWallTime" : ISODate("2021-03-18T14:19:01.368Z"), "lastDurableWallTime" : ISODate("2021-03-18T14:19:01.368Z") }, "lastStableRecoveryTimestamp" : Timestamp(1616077095, 1), "lastStableCheckpointTimestamp" : Timestamp(1616077095, 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" : 2690, "optime" : { "ts" : Timestamp(1616077141, 1), "t" : NumberLong(6) }, "optimeDate" : ISODate("2021-03-18T14:19:01Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1616074473, 1), "electionDate" : ISODate("2021-03-18T13:34:33Z"), "configVersion" : 7, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "mongo2:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 2686, "optime" : { "ts" : Timestamp(1616077141, 1), "t" : NumberLong(6) }, "optimeDurable" : { "ts" : Timestamp(1616077141, 1), "t" : NumberLong(6) }, "optimeDate" : ISODate("2021-03-18T14:19:01Z"), "optimeDurableDate" : ISODate("2021-03-18T14:19:01Z"), "lastHeartbeat" : ISODate("2021-03-18T14:19:07.382Z"), "lastHeartbeatRecv" : ISODate("2021-03-18T14:19:07.541Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 7 }, { "_id" : 2, "name" : "mongo3:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 2686, "lastHeartbeat" : ISODate("2021-03-18T14:19:07.382Z"), "lastHeartbeatRecv" : ISODate("2021-03-18T14:19:07.389Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 7 }, { "_id" : 3, "name" : "mongo4:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 4, "optime" : { "ts" : Timestamp(1616077141, 1), "t" : NumberLong(6) }, "optimeDurable" : { "ts" : Timestamp(1616077141, 1), "t" : NumberLong(6) }, "optimeDate" : ISODate("2021-03-18T14:19:01Z"), "optimeDurableDate" : ISODate("2021-03-18T14:19:01Z"), "lastHeartbeat" : ISODate("2021-03-18T14:19:07.382Z"), "lastHeartbeatRecv" : ISODate("2021-03-18T14:19:07.680Z"), "pingMs" : NumberLong(1), "lastHeartbeatMessage" : "", "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 7 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1616077141, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1616077141, 1) }
可以看到mongo4已經變成了primary的副本。
三、測試副本集
primary插入測試資料:
handong1:PRIMARY> use db3 switched to db db3 handong1:PRIMARY> db.db3.inesrt({"age":"23"})
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" } { "_id" : ObjectId("605368ffab6994b3520da445"), "age" : "23" }
在副本檢視資料:
172.16.254.132
handong1:SECONDARY> use db3 switched to db db3 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" } { "_id" : ObjectId("605368ffab6994b3520da445"), "age" : "23" }
172.16.254.134
handong1:SECONDARY> use db3 switched to db db3 handong1:SECONDARY> db.db3.find() { "_id" : ObjectId("6051d08ac74d627f4b561349"), "name" : "mongodb" } { "_id" : ObjectId("6053225bdebdeda2639f026e"), "name" : "mongodb4.2" } { "_id" : ObjectId("60533d88504b2400d4ae3091"), "name" : "jianghe" } { "_id" : ObjectId("605359a1ab6994b3520da444"), "name" : "doudou" } { "_id" : ObjectId("60534e662b429cdfe156324a"), "name" : "dandan" } { "_id" : ObjectId("605368ffab6994b3520da445"), "age" : "23" }
可以看到兩個副本節點資料都已經同步。
四、刪除副本
primary
handong1:PRIMARY> rs.remove("mongo4:27017") { "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1616079484, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1616079484, 1) }
檢視副本集資訊
handong1:PRIMARY> rs.status() { "set" : "handong1", "date" : ISODate("2021-03-18T14:58:45.070Z"), "myState" : 1, "term" : NumberLong(6), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1616079517, 1), "t" : NumberLong(6) }, "lastCommittedWallTime" : ISODate("2021-03-18T14:58:37.438Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1616079517, 1), "t" : NumberLong(6) }, "readConcernMajorityWallTime" : ISODate("2021-03-18T14:58:37.438Z"), "appliedOpTime" : { "ts" : Timestamp(1616079517, 1), "t" : NumberLong(6) }, "durableOpTime" : { "ts" : Timestamp(1616079517, 1), "t" : NumberLong(6) }, "lastAppliedWallTime" : ISODate("2021-03-18T14:58:37.438Z"), "lastDurableWallTime" : ISODate("2021-03-18T14:58:37.438Z") }, "lastStableRecoveryTimestamp" : Timestamp(1616079507, 1), "lastStableCheckpointTimestamp" : Timestamp(1616079507, 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" : 5067, "optime" : { "ts" : Timestamp(1616079517, 1), "t" : NumberLong(6) }, "optimeDate" : ISODate("2021-03-18T14:58:37Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1616074473, 1), "electionDate" : ISODate("2021-03-18T13:34:33Z"), "configVersion" : 8, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 1, "name" : "mongo2:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 5063, "optime" : { "ts" : Timestamp(1616079517, 1), "t" : NumberLong(6) }, "optimeDurable" : { "ts" : Timestamp(1616079517, 1), "t" : NumberLong(6) }, "optimeDate" : ISODate("2021-03-18T14:58:37Z"), "optimeDurableDate" : ISODate("2021-03-18T14:58:37Z"), "lastHeartbeat" : ISODate("2021-03-18T14:58:44.264Z"), "lastHeartbeatRecv" : ISODate("2021-03-18T14:58:44.595Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "172.16.254.131:27017", "syncSourceHost" : "172.16.254.131:27017", "syncSourceId" : 0, "infoMessage" : "", "configVersion" : 8 }, { "_id" : 2, "name" : "mongo3:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 5063, "lastHeartbeat" : ISODate("2021-03-18T14:58:44.266Z"), "lastHeartbeatRecv" : ISODate("2021-03-18T14:58:44.263Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "configVersion" : 8 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1616079517, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1616079517, 1) }
可以看到剛才加入的副本成員mongo4已經刪除。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30135314/viewspace-2763689/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MongoDB 4.2副本集自動故障轉移(一主一副一仲裁)MongoDB
- mongodb副本集新增刪除節點MongoDB
- MongoDB 刪除,新增副本集,並修改副本集IP等資訊MongoDB
- MongoDB副本集MongoDB
- MongoDB部署副本集MongoDB
- 搭建MongoDB副本集MongoDB
- MongoDB 副本集搭建MongoDB
- MongoDB 副本集管理MongoDB
- MongoDB副本集replica set (二)--副本集環境搭建MongoDB
- 006.MongoDB副本集MongoDB
- MongoDB - 副本集簡介MongoDB
- mongodb 4.0副本集搭建MongoDB
- MongoDB 副本集切換方法MongoDB
- MongoDB 副本集原理及管理MongoDB
- MongoDB 6.0.3副本集搭建MongoDB
- mongodb副本集用一致性快照方法新增從節點步驟MongoDB
- MongoDB Replica Set 副本集實踐MongoDB
- 手把手教你搭建mongodb副本集MongoDB
- Python連線訪問mongodb副本集PythonMongoDB
- Mongodb分散式叢集副本集+分片MongoDB分散式
- MongoDB副本集(一主兩從)讀寫分離、故障轉移功能環境部署記錄MongoDB
- linux下Mongodb叢集搭建:分片+副本集LinuxMongoDB
- MongoDB日常運維-04副本集搭建MongoDB運維
- 第六章 MongoDB副本集搭建MongoDB
- MongoDB4.2 副本集掃盲說明MongoDB
- mongodb主從仲裁節點配置MongoDB
- MongoDB日常運維-05副本集故障切換MongoDB運維
- MongoDB--副本集基本資訊【面試必備】MongoDB面試
- 單機Linux下搭建MongoDB副本集-三節點LinuxMongoDB
- 使用Docker搭建MongoDB 5.0版本副本集叢集DockerMongoDB
- 分散式文件儲存資料庫之MongoDB副本集分散式資料庫MongoDB
- mysql-刪除和新增主鍵MySql
- 主鍵的建立、新增、刪除操作
- mongo副本集搭建Go
- mongdb副本集搭建
- 副本集部署
- 副本集要點
- 副本集選舉