地址规划
关闭27017防火墙端口
更改mongodb主配文件
互相连接查看是否连接成功
定义变量
首先指定 id,id 就是副本集名称,members 里面是一组数组,数组中是各 MongoDB 实例的地址和监听端口
打开admin库,使用 rs.initiate 方法,引用刚刚定义的变量 config,来初始化集群
当副本集创建并初始化完成后,终端提示符会由">“变成"test:PRIMARY>”,tset 是副本集名称,PRIMARY 代表当前实例为 Primary 主节点。一个 MongoDB 副 本集中只有一个 Primary,其它都是 Secondary。并且只有 Primary 能写入数据,Secondary 只能读但不能写。
当副本集初始化完成后,还需要在 Secondary 上声明自己是从节点,才能查看数据
两台从节点上声明
查看从节点延时情况
查看集群状态
在主从节点输入
rs.status()
主:
test:PRIMARY> rs.status()
{
“set” : “test”,
“date” : ISODate(“2022-01-13T02:24:28.304Z”),
“myState” : 1,
“term” : NumberLong(1),
“syncSourceHost” : “”,
“syncSourceId” : -1,
“heartbeatIntervalMillis” : NumberLong(2000),
“majorityVoteCount” : 2,
“writeMajorityCount” : 2,
“votingMembersCount” : 3,
“writableVotingMembersCount” : 3,
“optimes” : {
“lastCommittedOpTime” : {
“ts” : Timestamp(1642040661, 1),
“t” : NumberLong(1)
},
“lastCommittedWallTime” : ISODate(“2022-01-13T02:24:21.535Z”),
“readConcernMajorityOpTime” : {
“ts” : Timestamp(1642040661, 1),
“t” : NumberLong(1)
},
“appliedOpTime” : {
“ts” : Timestamp(1642040661, 1),
“t” : NumberLong(1)
},
“durableOpTime” : {
“ts” : Timestamp(1642040661, 1),
“t” : NumberLong(1)
},
“lastAppliedWallTime” : ISODate(“2022-01-13T02:24:21.535Z”),
“lastDurableWallTime” : ISODate(“2022-01-13T02:24:21.535Z”)
},
“lastStableRecoveryTimestamp” : Timestamp(1642040641, 1),
“electionCandidateMetrics” : {
“lastElectionReason” : “electionTimeout”,
“lastElectionDate” : ISODate(“2022-01-13T02:17:21.396Z”),
“electionTerm” : NumberLong(1),
“lastCommittedOpTimeAtElection” : {
“ts” : Timestamp(1642040230, 1),
“t” : NumberLong(-1)
},
“lastSeenOpTimeAtElection” : {
“ts” : Timestamp(1642040230, 1),
“t” : NumberLong(-1)
},
“numVotesNeeded” : 2,
“priorityAtElection” : 1,
“electionTimeoutMillis” : NumberLong(10000),
“numCatchUpOps” : NumberLong(0),
“newTermStartDate” : ISODate(“2022-01-13T02:17:21.430Z”),
“wMajorityWriteAvailabilityDate” : ISODate(“2022-01-13T02:17:21.531Z”)
},
“members” : [
{
“_id” : 0,
“name” : “192.168.179.172:27017”,
“health” : 1,
“state” : 1,
“stateStr” : “PRIMARY”,
“uptime” : 999,
“optime” : {
“ts” : Timestamp(1642040661, 1),
“t” : NumberLong(1)
},
“optimeDate” : ISODate(“2022-01-13T02:24:21Z”),
“lastAppliedWallTime” : ISODate(“2022-01-13T02:24:21.535Z”),
“lastDurableWallTime” : ISODate(“2022-01-13T02:24:21.535Z”),
“syncSourceHost” : “”,
“syncSourceId” : -1,
“infoMessage” : “”,
“electionTime” : Timestamp(1642040241, 1),
“electionDate” : ISODate(“2022-01-13T02:17:21Z”),
“configVersion” : 1,
“configTerm” : 1,
“self” : true,
“lastHeartbeatMessage” : “”
},
{
“_id” : 1,
“name” : “192.168.179.173:27017”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”,
“uptime” : 438,
“optime” : {
“ts” : Timestamp(1642040661, 1),
“t” : NumberLong(1)
},
“optimeDurable” : {
“ts” : Timestamp(1642040661, 1),
“t” : NumberLong(1)
},
“optimeDate” : ISODate(“2022-01-13T02:24:21Z”),
“optimeDurableDate” : ISODate(“2022-01-13T02:24:21Z”),
“lastAppliedWallTime” : ISODate(“2022-01-13T02:24:21.535Z”),
“lastDurableWallTime” : ISODate(“2022-01-13T02:24:21.535Z”),
“lastHeartbeat” : ISODate(“2022-01-13T02:24:28.041Z”),
“lastHeartbeatRecv” : ISODate(“2022-01-13T02:24:26.598Z”),
“pingMs” : NumberLong(0),
“lastHeartbeatMessage” : “”,
“syncSourceHost” : “192.168.179.172:27017”,
“syncSourceId” : 0,
“infoMessage” : “”,
“configVersion” : 1,
“configTerm” : 1
},
{
“_id” : 2,
“name” : “192.168.179.174:27017”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”,
“uptime” : 438,
“optime” : {
“ts” : Timestamp(1642040661, 1),
“t” : NumberLong(1)
},
“optimeDurable” : {
“ts” : Timestamp(1642040661, 1),
“t” : NumberLong(1)
},
“optimeDate” : ISODate(“2022-01-13T02:24:21Z”),
“optimeDurableDate” : ISODate(“2022-01-13T02:24:21Z”),
“lastAppliedWallTime” : ISODate(“2022-01-13T02:24:21.535Z”),
“lastDurableWallTime” : ISODate(“2022-01-13T02:24:21.535Z”),
“lastHeartbeat” : ISODate(“2022-01-13T02:24:28.077Z”),
“lastHeartbeatRecv” : ISODate(“2022-01-13T02:24:26.599Z”),
“pingMs” : NumberLong(0),
“lastHeartbeatMessage” : “”,
“syncSourceHost” : “192.168.179.172:27017”,
“syncSourceId” : 0,
“infoMessage” : “”,
“configVersion” : 1,
“configTerm” : 1
}
],
“ok” : 1,
“KaTeX parse error: Expected 'EOF', got '}' at position 214: …1642040661, 1) }̲ 从1: test:SECON…clusterTime” : {
“clusterTime” : Timestamp(1642040731, 1),
“signature” : {
“hash” : BinData(0,“AAAAAAAAAAAAAAAAAAAAAAAAAAA=”),
“keyId” : NumberLong(0)
}
},
“operationTime” : Timestamp(1642040731, 1)
}
从2:
test:SECONDARY> rs.status()
{
“set” : “test”,
“date” : ISODate(“2022-01-13T02:26:05.092Z”),
“myState” : 2,
“term” : NumberLong(1),
“syncSourceHost” : “192.168.179.172:27017”,
“syncSourceId” : 0,
“heartbeatIntervalMillis” : NumberLong(2000),
“majorityVoteCount” : 2,
“writeMajorityCount” : 2,
“votingMembersCount” : 3,
“writableVotingMembersCount” : 3,
“optimes” : {
“lastCommittedOpTime” : {
“ts” : Timestamp(1642040761, 1),
“t” : NumberLong(1)
},
“lastCommittedWallTime” : ISODate(“2022-01-13T02:26:01.553Z”),
“readConcernMajorityOpTime” : {
“ts” : Timestamp(1642040761, 1),
“t” : NumberLong(1)
},
“appliedOpTime” : {
“ts” : Timestamp(1642040761, 1),
“t” : NumberLong(1)
},
“durableOpTime” : {
“ts” : Timestamp(1642040761, 1),
“t” : NumberLong(1)
},
“lastAppliedWallTime” : ISODate(“2022-01-13T02:26:01.553Z”),
“lastDurableWallTime” : ISODate(“2022-01-13T02:26:01.553Z”)
},
“lastStableRecoveryTimestamp” : Timestamp(1642040721, 1),
“electionParticipantMetrics” : {
“votedForCandidate” : true,
“electionTerm” : NumberLong(1),
“lastVoteDate” : ISODate(“2022-01-13T02:17:21.397Z”),
“electionCandidateMemberId” : 0,
“voteReason” : “”,
“lastAppliedOpTimeAtElection” : {
“ts” : Timestamp(1642040230, 1),
“t” : NumberLong(-1)
},
“maxAppliedOpTimeInSet” : {
“ts” : Timestamp(1642040230, 1),
“t” : NumberLong(-1)
},
“priorityAtElection” : 1,
“newTermStartDate” : ISODate(“2022-01-13T02:17:21.430Z”),
“newTermAppliedDate” : ISODate(“2022-01-13T02:17:21.581Z”)
},
“members” : [
{
“_id” : 0,
“name” : “192.168.179.172:27017”,
“health” : 1,
“state” : 1,
“stateStr” : “PRIMARY”,
“uptime” : 534,
“optime” : {
“ts” : Timestamp(1642040761, 1),
“t” : NumberLong(1)
},
“optimeDurable” : {
“ts” : Timestamp(1642040761, 1),
“t” : NumberLong(1)
},
“optimeDate” : ISODate(“2022-01-13T02:26:01Z”),
“optimeDurableDate” : ISODate(“2022-01-13T02:26:01Z”),
“lastAppliedWallTime” : ISODate(“2022-01-13T02:26:01.553Z”),
“lastDurableWallTime” : ISODate(“2022-01-13T02:26:01.553Z”),
“lastHeartbeat” : ISODate(“2022-01-13T02:26:04.743Z”),
“lastHeartbeatRecv” : ISODate(“2022-01-13T02:26:04.232Z”),
“pingMs” : NumberLong(0),
“lastHeartbeatMessage” : “”,
“syncSourceHost” : “”,
“syncSourceId” : -1,
“infoMessage” : “”,
“electionTime” : Timestamp(1642040241, 1),
“electionDate” : ISODate(“2022-01-13T02:17:21Z”),
“configVersion” : 1,
“configTerm” : 1
},
{
“_id” : 1,
“name” : “192.168.179.173:27017”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”,
“uptime” : 534,
“optime” : {
“ts” : Timestamp(1642040761, 1),
“t” : NumberLong(1)
},
“optimeDurable” : {
“ts” : Timestamp(1642040761, 1),
“t” : NumberLong(1)
},
“optimeDate” : ISODate(“2022-01-13T02:26:01Z”),
“optimeDurableDate” : ISODate(“2022-01-13T02:26:01Z”),
“lastAppliedWallTime” : ISODate(“2022-01-13T02:26:01.553Z”),
“lastDurableWallTime” : ISODate(“2022-01-13T02:26:01.553Z”),
“lastHeartbeat” : ISODate(“2022-01-13T02:26:04.743Z”),
“lastHeartbeatRecv” : ISODate(“2022-01-13T02:26:04.742Z”),
“pingMs” : NumberLong(0),
“lastHeartbeatMessage” : “”,
“syncSourceHost” : “192.168.179.172:27017”,
“syncSourceId” : 0,
“infoMessage” : “”,
“configVersion” : 1,
“configTerm” : 1
},
{
“_id” : 2,
“name” : “192.168.179.174:27017”,
“health” : 1,
“state” : 2,
“stateStr” : “SECONDARY”,
“uptime” : 1080,
“optime” : {
“ts” : Timestamp(1642040761, 1),
“t” : NumberLong(1)
},
“optimeDate” : ISODate(“2022-01-13T02:26:01Z”),
“lastAppliedWallTime” : ISODate(“2022-01-13T02:26:01.553Z”),
“lastDurableWallTime” : ISODate(“2022-01-13T02:26:01.553Z”),
“syncSourceHost” : “192.168.179.172:27017”,
“syncSourceId” : 0,
“infoMessage” : “”,
“configVersion” : 1,
“configTerm” : 1,
“self” : true,
“lastHeartbeatMessage” : “”
}
],
“ok” : 1,
“$clusterTime” : {
“clusterTime” : Timestamp(1642040761, 1),
“signature” : {
“hash” : BinData(0,“AAAAAAAAAAAAAAAAAAAAAAAAAAA=”),
“keyId” : NumberLong(0)
}
},
“operationTime” : Timestamp(1642040761, 1)
}
验证主从复制
在主节点上插入数据
从节点验证
从1:
从2:
查看延时情况
优先级
MongoDB 副本集的中,Secondary 会不断检测 Primary 的健康状况,一旦 Primary 故障,各 Secondary 会根据优先级来选举一个新的 Primary。在没有指定优先级的情况下,各实例的优先级为 1。在优先级一样的情况下,无法预料到到底哪个 Secondary 会成为 Primary。
获取副本集的配置信息,各实例的优先级默认为 1
设置各实例的优先级,只有在 Primary 上可以更改优先级配置。根据 rs.config().members 中的输出,各实例的索引号从 0 开始,每次递增 1,类似数组。
更新mongodb副本集的配置
查看优先级