绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
mongodb副本集主从切换
2022-01-06 17:31:35

前文已有mongodb副本集搭建及添加节点的记录

mongodb 4.0副本集搭建

mongodb副本集添加节点

本次进行主从切换及删除节点的操作。

1. 主从切换

1.1 查询当前节点信息及状态

使用rs.status()命令可以查看到当前副本集各节点信息及角色

节点id
节点
角色
node0
192.168.56.198:27017
PRIMARY
node1
192.168.56.199:27018 ARBITER
node2
192.168.56.197:27017 SECONDARY
node3
192.168.56.199:27017
SECONDARY


test12:PRIMARY> rs.status(){  "set" : "test12",  "date" : ISODate("2021-12-16T13:45:28.261Z"),  "myState" : 1,  "term" : NumberLong(3),  "syncingTo" : "",  "syncSourceHost" : "",  "syncSourceId" : -1,  "heartbeatIntervalMillis" : NumberLong(2000),  "majorityVoteCount" : 3,  "writeMajorityCount" : 3,  "optimes" : {    "lastCommittedOpTime" : {      "ts" : Timestamp(1639662318, 1),      "t" : NumberLong(3)    },    "lastCommittedWallTime" : ISODate("2021-12-16T13:45:18.626Z"),    "readConcernMajorityOpTime" : {      "ts" : Timestamp(1639662318, 1),      "t" : NumberLong(3)    },    "readConcernMajorityWallTime" : ISODate("2021-12-16T13:45:18.626Z"),    "appliedOpTime" : {      "ts" : Timestamp(1639662318, 1),      "t" : NumberLong(3)    },    "durableOpTime" : {      "ts" : Timestamp(1639662318, 1),      "t" : NumberLong(3)    },    "lastAppliedWallTime" : ISODate("2021-12-16T13:45:18.626Z"),    "lastDurableWallTime" : ISODate("2021-12-16T13:45:18.626Z")  },  "lastStableRecoveryTimestamp" : Timestamp(1639662308, 1),  "lastStableCheckpointTimestamp" : Timestamp(1639662308, 1),  "electionCandidateMetrics" : {    "lastElectionReason" : "priorityTakeover",    "lastElectionDate" : ISODate("2021-12-14T15:24:57.309Z"),    "electionTerm" : NumberLong(3),    "lastCommittedOpTimeAtElection" : {      "ts" : Timestamp(1639495485, 1),      "t" : NumberLong(2)    },    "lastSeenOpTimeAtElection" : {      "ts" : Timestamp(1639495485, 1),      "t" : NumberLong(2)    },    "numVotesNeeded" : 3,    "priorityAtElection" : 15,    "electionTimeoutMillis" : NumberLong(10000),    "priorPrimaryMemberId" : 3,    "numCatchUpOps" : NumberLong(),    "newTermStartDate" : ISODate("2021-12-14T15:24:57.321Z"),    "wMajorityWriteAvailabilityDate" : ISODate("2021-12-14T15:24:58.285Z")  },  "electionParticipantMetrics" : {    "votedForCandidate" : true,    "electionTerm" : NumberLong(2),    "lastVoteDate" : ISODate("2021-12-14T10:36:13.063Z"),    "electionCandidateMemberId" : 3,    "voteReason" : "",    "lastAppliedOpTimeAtElection" : {      "ts" : Timestamp(1639478166, 1),      "t" : NumberLong(1)    },    "maxAppliedOpTimeInSet" : {      "ts" : Timestamp(1639478166, 1),      "t" : NumberLong(1)    },    "priorityAtElection" : 1  },  "members" : [    {      "_id" : ,      "name" : "192.168.56.198:27017",      "health" : 1,      "state" : 1,      "stateStr" : "PRIMARY",      "uptime" : 186852,      "optime" : {        "ts" : Timestamp(1639662318, 1),        "t" : NumberLong(3)      },      "optimeDate" : ISODate("2021-12-16T13:45:18Z"),      "syncingTo" : "",      "syncSourceHost" : "",      "syncSourceId" : -1,      "infoMessage" : "",      "electionTime" : Timestamp(1639495497, 1),      "electionDate" : ISODate("2021-12-14T15:24:57Z"),      "configVersion" : 7,      "self" : true,      "lastHeartbeatMessage" : ""    },    {      "_id" : 1,      "name" : "192.168.56.199:27018",      "health" : 1,      "state" : 7,      "stateStr" : "ARBITER",      "uptime" : 186375,      "lastHeartbeat" : ISODate("2021-12-16T13:45:27.183Z"),      "lastHeartbeatRecv" : ISODate("2021-12-16T13:45:26.984Z"),      "pingMs" : NumberLong(),      "lastHeartbeatMessage" : "",      "syncingTo" : "",      "syncSourceHost" : "",      "syncSourceId" : -1,      "infoMessage" : "",      "configVersion" : 7    },    {      "_id" : 2,      "name" : "192.168.56.197:27017",      "health" : 1,      "state" : 2,      "stateStr" : "SECONDARY",      "uptime" : 185329,      "optime" : {        "ts" : Timestamp(1639662318, 1),        "t" : NumberLong(3)      },      "optimeDurable" : {        "ts" : Timestamp(1639662318, 1),        "t" : NumberLong(3)      },      "optimeDate" : ISODate("2021-12-16T13:45:18Z"),      "optimeDurableDate" : ISODate("2021-12-16T13:45:18Z"),      "lastHeartbeat" : ISODate("2021-12-16T13:45:27.463Z"),      "lastHeartbeatRecv" : ISODate("2021-12-16T13:45:26.387Z"),      "pingMs" : NumberLong(1),      "lastHeartbeatMessage" : "",      "syncingTo" : "192.168.56.198:27017",      "syncSourceHost" : "192.168.56.198:27017",      "syncSourceId" : ,      "infoMessage" : "",      "configVersion" : 7    },    {      "_id" : 3,      "name" : "192.168.56.199:27017",      "health" : 1,      "state" : 2,      "stateStr" : "SECONDARY",      "uptime" : 12079,      "optime" : {        "ts" : Timestamp(1639662318, 1),        "t" : NumberLong(3)      },      "optimeDurable" : {        "ts" : Timestamp(1639662318, 1),        "t" : NumberLong(3)      },      "optimeDate" : ISODate("2021-12-16T13:45:18Z"),      "optimeDurableDate" : ISODate("2021-12-16T13:45:18Z"),      "lastHeartbeat" : ISODate("2021-12-16T13:45:28.253Z"),      "lastHeartbeatRecv" : ISODate("2021-12-16T13:45:27.555Z"),      "pingMs" : NumberLong(),      "lastHeartbeatMessage" : "",      "syncingTo" : "192.168.56.198:27017",      "syncSourceHost" : "192.168.56.198:27017",      "syncSourceId" : ,      "infoMessage" : "",      "configVersion" : 7    }  ],  "ok" : 1,  "$clusterTime" : {    "clusterTime" : Timestamp(1639662318, 1),    "signature" : {      "hash" : BinData(,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),      "keyId" : NumberLong()    }  },  "operationTime" : Timestamp(1639662318, 1)}


1.2 切换主库

将主库切换至node3节点具体步骤为:

  •   查看当前配置

  • 调整node3节点的权重

  • 重新加载配置

因默认情况下,各节点的权重都为1,因此可以调整需要切换为主库的节点的权重为较大的值(建议超过节点数)

test12:PRIMARY> rs.config(){  "_id" : "test12",  "version" : 9,  "protocolVersion" : NumberLong(1),  "writeConcernMajorityJournalDefault" : true,  "members" : [    {      "_id" : ,      "host" : "192.168.56.198:27017",      "arbiterOnly" : false,      "buildIndexes" : true,      "hidden" : false,      "priority" : 1,      "tags" : {
}, "slaveDelay" : NumberLong(), "votes" : 1 }, {      "_id" : 1, "host" : "192.168.56.199:27018", "arbiterOnly" : true, "buildIndexes" : true, "hidden" : false, "priority" : , "tags" : {
}, "slaveDelay" : NumberLong(), "votes" : 1 }, {      "_id" : 2, "host" : "192.168.56.197:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : {
}, "slaveDelay" : NumberLong(), "votes" : 1 }, {      "_id" : 3, "host" : "192.168.56.199:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : {
}, "slaveDelay" : NumberLong(), "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatIntervalMillis" : 2000, "heartbeatTimeoutSecs" : 10, "electionTimeoutMillis" : 10000, "catchUpTimeoutMillis" : -1, "catchUpTakeoverDelayMillis" : 30000, "getLastErrorModes" : {
}, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : }, "replicaSetId" : ObjectId("61b86af0a21015e69623e18d") }}

将node3的节点的权重改为10

var cfg = rs.conf()cfg.members[3].priority = 10

重新加载配置,选主

rs.reconfig(cfg)

通常1分钟内完成切换。

PS:也可以通过rs.stepDown()

PRIMARY> rs.stepDown()       //rs.stepDown(30)   单位:S
这个命令会让primary降级为Secondary节点,并维持60s,如果这段时间内没有新的primary被选举出来,这个节点可以要求重新进行选举

2. 剔除节点

剔除的节点为SECONDARY或ARBITER,可以执行通过rs.remove() 命令剔除对应节点,如为PRIMARY节点,则先进行主从切换,再剔除。
test12:PRIMARYrs.remove("192.168.56.199:27018"){  "ok" : 1,  "$clusterTime" : {    "clusterTime" : Timestamp(1639683041, 1),    "signature" : {      "hash" : BinData(,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),      "keyId" : NumberLong()    }  },  "operationTime" : Timestamp(1639683041, 1)}


分享好友

分享这个小栈给你的朋友们,一起进步吧。

数据库干货铺
创建时间:2021-12-13 09:36:52
致力于分享数据库、大数据、运维等方面相关知识,并通过生产环境遇到的实战案例分享排坑技巧等
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

栈主、嘉宾

查看更多
  • 数据库干货铺
    栈主

小栈成员

查看更多
  • miemieMIA
  • janefengwang
戳我,来吐槽~