mongodb副本集主从切换

数据库干货铺

共 8055字,需浏览 17分钟

 ·

2021-12-20 17:16

点击上方蓝字关注我

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

mongodb 4.0副本集搭建

mongodb副本集添加节点

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

1. 主从切换

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

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

节点id
节点
角色
node0
192.168.56.198:27017
PRIMARY
node1
192.168.56.199:27018ARBITER
node2
192.168.56.197:27017SECONDARY
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(0),    "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" : 0,      "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(0),      "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" : 0,      "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(0),      "lastHeartbeatMessage" : "",      "syncingTo" : "192.168.56.198:27017",      "syncSourceHost" : "192.168.56.198:27017",      "syncSourceId" : 0,      "infoMessage" : "",      "configVersion" : 7    }  ],  "ok" : 1,  "$clusterTime" : {    "clusterTime" : Timestamp(1639662318, 1),    "signature" : {      "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),      "keyId" : NumberLong(0)    }  },  "operationTime" : Timestamp(1639662318, 1)}


1.2 切换主库

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

  •   查看当前配置

  • 调整node3节点的权重

  • 重新加载配置

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

test12:PRIMARY> rs.config(){  "_id" : "test12",  "version" : 9,  "protocolVersion" : NumberLong(1),  "writeConcernMajorityJournalDefault" : true,  "members" : [    {      "_id" : 0,      "host" : "192.168.56.198:27017",      "arbiterOnly" : false,      "buildIndexes" : true,      "hidden" : false,      "priority" : 1,      "tags" : {
}, "slaveDelay" : NumberLong(0), "votes" : 1 }, {      "_id" : 1, "host" : "192.168.56.199:27018", "arbiterOnly" : true, "buildIndexes" : true, "hidden" : false, "priority" : 0, "tags" : {
}, "slaveDelay" : NumberLong(0), "votes" : 1 }, {      "_id" : 2, "host" : "192.168.56.197:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : {
}, "slaveDelay" : NumberLong(0), "votes" : 1 }, {      "_id" : 3, "host" : "192.168.56.199:27017", "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("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:PRIMARY> rs.remove("192.168.56.199:27018"){  "ok" : 1,  "$clusterTime" : {    "clusterTime" : Timestamp(1639683041, 1),    "signature" : {      "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),      "keyId" : NumberLong(0)    }  },  "operationTime" : Timestamp(1639683041, 1)}
往期精彩回顾

1.  MySQL高可用之MHA集群部署

2.  mysql8.0新增用户及加密规则修改的那些事

3.  比hive快10倍的大数据查询利器-- presto

4.  监控利器出鞘:Prometheus+Grafana监控MySQL、Redis数据库

5.  PostgreSQL主从复制--物理复制

6.  MySQL传统点位复制在线转为GTID模式复制

7.  MySQL敏感数据加密及解密

8.  MySQL数据备份及还原(一)

9.  MySQL数据备份及还原(二)

扫码关注     


浏览 38
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报