MongoDB副本集设置节点优先级priority
一、背景介绍
MongoDB副本集中,如果要指定某些节点不能被选为primary节点,或者primary节点必须在某些节点中选出,该怎么配置呢?没错可以通过priority修改这些节点的优先级实现。
官网参考链接:https://docs.mongodb.com/manual/tutorial/force-member-to-be-primary/
二、操作演示
2.1 设置节点选主优先级
(1)查看当前副本集角色
udb-h332hu4d:PRIMARY> rs.status() { "set" : "udb-h332hu4d", "date" : ISODate("2021-11-26T15:23:23.316Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "192.168.0.224:27017", "health" : 1, "state" : 2, "stateStr" : "PRIMARY", # 主节点 "uptime" : 854, "optime" : Timestamp(1637939967, 1), "optimeDate" : ISODate("2021-11-26T15:19:27Z"), "lastHeartbeat" : ISODate("2021-11-26T15:23:21.685Z"), "lastHeartbeatRecv" : ISODate("2021-11-26T15:23:22.114Z"), "pingMs" : 0, "configVersion" : 67491 }, { "_id" : 1, "name" : "192.168.0.225:27017", "health" : 1, "state" : 1, "stateStr" : "SECONDARY", # 从节点 "uptime" : 871, "optime" : Timestamp(1637939967, 1), "optimeDate" : ISODate("2021-11-26T15:19:27Z"), "electionTime" : Timestamp(1637939972, 1), "electionDate" : ISODate("2021-11-26T15:19:32Z"), "configVersion" : 67491, "self" : true }, { "_id" : 2, "name" : "192.168.0.16:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", # 选举节点 "uptime" : 834, "lastHeartbeat" : ISODate("2021-11-26T15:23:21.686Z"), "lastHeartbeatRecv" : ISODate("2021-11-26T15:23:21.675Z"), "pingMs" : 0, "configVersion" : 67491 } ], "ok" : 1 }
(2)输出当前副本集配置
udb-h332hu4d:PRIMARY> cfg = rs.conf() { "_id" : "udb-h332hu4d", "version" : 67490, "members" : [ { "_id" : 0, "host" : "192.168.0.224:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 1, "host" : "192.168.0.225:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 2, "host" : "192.168.0.16:27017", "arbiterOnly" : true, # 选举节点 "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } } }
(3)设置节点优先级
udb-h332hu4d:PRIMARY> cfg.members[0].priority = 2 # 修改当前primary节点优先级为2 2 udb-h332hu4d:PRIMARY> cfg.members[1].priority = 10 # 修改当前secondary节点优先级为10 10
(4)生效优先级配置
udb-h332hu4d:PRIMARY> rs.reconfig(cfg) { "ok" : 1 }
注:副本集中通过设置priority的值来决定优先权的大小。这个值的范围是0~100,值越大,优先权越高。默认的值是1,假设值是0,那么不能成为primay。
2.2 从节点执行命令报错
由于执行完上述调整优先级的操作后,primary节点与secondary节点发生了切换,所以之前登录的primary节点变成了secondary。那么如何在从节点执行相关查看命令呢?
(1)方法一:基于 session 的,退出来就失效了
udb-h332hu4d:SECONDARY> db.getMongo().setSlaveOk()
(2)方案二:临时生效
udb-h332hu4d:SECONDARY> rs.slaveOk()
(3)方案三:在用户家目录的隐藏文件 .mongorc.js 中加入如下行可永久生效,不必每次登陆都要设制一次
[root@blogs-v2 ~]# echo "rs.slaveOk()" > .mongorc.js [root@blogs-v2 ~]# mongo 192.168.0.224/admin -uroot -pUcloudcn udb-h332hu4d:SECONDARY> show dbs admin 0.000GB local 0.000GB
2.3 验证修改后Primary与Secondary切换情况
udb-h332hu4d:PRIMARY> rs.status() { "set" : "udb-h332hu4d", "date" : ISODate("2021-11-26T15:23:23.316Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "192.168.0.224:27017", # primary节点已经变成了secondary节点 "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 854, "optime" : Timestamp(1637939967, 1), "optimeDate" : ISODate("2021-11-26T15:19:27Z"), "lastHeartbeat" : ISODate("2021-11-26T15:23:21.685Z"), "lastHeartbeatRecv" : ISODate("2021-11-26T15:23:22.114Z"), "pingMs" : 0, "configVersion" : 67491 }, { "_id" : 1, "name" : "192.168.0.225:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 871, "optime" : Timestamp(1637939967, 1), "optimeDate" : ISODate("2021-11-26T15:19:27Z"), "electionTime" : Timestamp(1637939972, 1), "electionDate" : ISODate("2021-11-26T15:19:32Z"), "configVersion" : 67491, "self" : true }, { "_id" : 2, "name" : "192.168.0.16:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 834, "lastHeartbeat" : ISODate("2021-11-26T15:23:21.686Z"), "lastHeartbeatRecv" : ISODate("2021-11-26T15:23:21.675Z"), "pingMs" : 0, "configVersion" : 67491 } ], "ok" : 1 }
由此可见如果副本集中,如果有非primary节点优先级被提高到大于primary节点优先级后,很快就会从高优先级的节点中重新选举新主。如上述案例,secondary节点优先级调整为10,primary节点调整为2,很快优先级为10的secondary节点被选为了primary节点。另外,members[1] 这中括号里的数字是 rs.config() 命令打出来按顺序取的, members[1] 表示的顺数第二个,而不是里面的 id。
作者:UStarGao
链接:https://www.starcto.com/mongodb/263.html
来源:STARCTO
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
UCloud云平台推荐
随便看看
- 2021-10-08Redis容器化部署教程
- 2021-09-04Linux磁盘UUID冲突解决方案
- 2022-09-15MySQL自增ID相关设置
- 2022-11-10UCloud云数据库公网访问解决方案
- 2021-02-21云计算