4.1、基本信息
服务器地址:192.168.11.45
副本集名称:rs0
容器节点及端口映射:
mongo1 27018:27017
mongo2 27019:27017
mongo3 27020:27017
4.2、启动三个节点
docker run -d -p 27018:27017 --name mongo1 mongo --replSet "rs0"
docker run -d -p 27019:27017 --name mongo2 mongo --replSet "rs0"
docker run -d -p 27020:27017 --name mongo3 mongo --replSet "rs0"
4.3、选择在主节点进行副本集配置
docker exec -it 65 mongo # 进入主节点mongo
# 进行副本集配置
> var config={
_id:"rs0",
members:[
{_id:0,host:"192.168.11.45:27018"},
{_id:1,host:"192.168.11.45:27019"},
{_id:2,host:"192.168.11.45:27020"}
]};
# 启动新的副本集
> rs.initiate(config)
{ "ok" : 1 }
此时命令提示符已经发生变化,由原来的 > 变成了 rs:SECONDARY>
# 查看副本集配置信息
rs0:SECONDARY> rs.conf()
{
"_id" : "rs0", # 副本集名称
"version" : 1,
"term" : 1,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "192.168.11.45:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false, # 是否为隐藏节点
"priority" : 1, # 优先级
"tags" : {
},
"slaveDelay" : NumberLong(0), # 延迟,单位秒
"votes" : 1 # 1:可以投票,0:不能投票
},
{
"_id" : 1,
"host" : "192.168.11.45:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.11.45:27020",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("616fb8d3123d0cb36cfa6d4e")
}
}
# 查看副本集状态
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2021-10-20T06:37:05.142Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1634711824, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2021-10-20T06:37:04.306Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1634711824, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2021-10-20T06:37:04.306Z"),
"appliedOpTime" : {
"ts" : Timestamp(1634711824, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1634711824, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2021-10-20T06:37:04.306Z"),
"lastDurableWallTime" : ISODate("2021-10-20T06:37:04.306Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1634711774, 3),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2021-10-20T06:36:14.279Z"),
"electionTerm" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1634711763, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2021-10-20T06:36:14.297Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2021-10-20T06:36:15.001Z")
},
"members" : [
{
"_id" : 0,
"name" : "192.168.11.45:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", # 节点类型
"uptime" : 683,
"optime" : {
"ts" : Timestamp(1634711824, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-10-20T06:37:04Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1634711774, 1),
"electionDate" : ISODate("2021-10-20T06:36:14Z"),
"configVersion" : 1,
"configTerm" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.11.45:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", # 节点类型
"uptime" : 61,
"optime" : {
"ts" : Timestamp(1634711814, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1634711814, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-10-20T06:36:54Z"),
"optimeDurableDate" : ISODate("2021-10-20T06:36:54Z"),
"lastHeartbeat" : ISODate("2021-10-20T06:37:04.284Z"),
"lastHeartbeatRecv" : ISODate("2021-10-20T06:37:03.289Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "192.168.11.45:27018",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 1
},
{
"_id" : 2,
"name" : "192.168.11.45:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", # 节点类型
"uptime" : 61,
"optime" : {
"ts" : Timestamp(1634711814, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1634711814, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2021-10-20T06:36:54Z"),
"optimeDurableDate" : ISODate("2021-10-20T06:36:54Z"),
"lastHeartbeat" : ISODate("2021-10-20T06:37:04.284Z"),
"lastHeartbeatRecv" : ISODate("2021-10-20T06:37:03.289Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "192.168.11.45:27018",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 1
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1634711824, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1634711824, 1)
}
4.4、同步测试
在主节点mongo1-27018上新增一条记录,可在mongo2-27019、mongo3-27020上看到