MongoDB 4.2副本集添加/删除副本(一主一副一仲裁)

一、修改/etc/hosts

1 2 3 4 172.16.254.131 mongo1 172.16.254.132 mongo2 172.16.254.133 mongo3 172.16.254.134 mongo4

二、添加副本

172.16.254.131

1 2 3 4 5 6 7 8 9 10 11 12 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) }

查看副本集信息:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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插入测试数据:

1 2 3 handong1:PRIMARY> use db3 switched to db db3 handong1:PRIMARY> db.db3.inesrt({"age":"23"})
1 2 3 4 5 6 7 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

1 2 3 4 5 6 7 8 9 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

1 2 3 4 5 6 7 8 9 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

1 2 3 4 5 6 7 8 9 10 11 12 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) }

查看副本集信息

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 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已经删除。

 

上一篇:MongoDB副本集配置方法


下一篇:mongodb获取配置参数getParameter