思路:
1.為了保證數據的一致性,必須先關閉應用的寫服務。
2.提升要升級為Primary節點的Secondary節點的優先級。
操作如下,在主節點上執行:
arps:PRIMARY> config=rs.conf() //查看當前配置,存入config變量中。 arps:PRIMARY> config.members[2].priority = 3 //修改config變量,第三組成員的優先級為3. arps:PRIMARY> rs.reconfig(config) //配置生效 arps:SECONDARY> rs.conf() //查看當前配置 { "_id" : "arps", "version" : 4, "members" : [ { "_id" : 0, "host" : "172.17.4.37:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 1, "host" : "172.17.4.38:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 2, "host" : "172.17.4.39:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 3, //優先級變為3 "tags" : { }, "slaveDelay" : 0, "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } } } arps:SECONDARY> rs.status() { "set" : "arps", "date" : ISODate("2017-12-22T07:25:04.641Z"), "myState" : 2, "syncingTo" : "172.17.4.39:27017", "members" : [ { "_id" : 0, "name" : "172.17.4.37:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 7597536, "optime" : Timestamp(1513927481, 3), "optimeDate" : ISODate("2017-12-22T07:24:41Z"), "syncingTo" : "172.17.4.39:27017", "configVersion" : 4, "self" : true }, { "_id" : 1, "name" : "172.17.4.38:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 7597426, "optime" : Timestamp(1513927481, 3), "optimeDate" : ISODate("2017-12-22T07:24:41Z"), "lastHeartbeat" : ISODate("2017-12-22T07:25:02.961Z"), "lastHeartbeatRecv" : ISODate("2017-12-22T07:25:04.091Z"), "pingMs" : 0, "syncingTo" : "172.17.4.39:27017", "configVersion" : 4 }, { "_id" : 2, "name" : "172.17.4.39:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", //最后一個節點升級為PRIMARY "uptime" : 3202, "optime" : Timestamp(1513927481, 3), "optimeDate" : ISODate("2017-12-22T07:24:41Z"), "lastHeartbeat" : ISODate("2017-12-22T07:25:02.951Z"), "lastHeartbeatRecv" : ISODate("2017-12-22T07:25:04.344Z"), "pingMs" : 0, "electionTime" : Timestamp(1513927190, 2), "electionDate" : ISODate("2017-12-22T07:19:50Z"), "configVersion" : 4 } ], "ok" : 1 }
注:
引用來源,請盡量閱讀原來博客:來源: https://blog.csdn.net/zhangshengdongly/article/details/96427716?utm_term=mongodb%E5%89%AF%E6%9C%AC%E9%9B%86%E5%88%87%E6%8D%A2%E4%B8%BB%E4%BB%8E&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-0-96427716&spm=3001.4430
查看副本集信息: