一、修改/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已经删除。