一、方法一 rs.setpDown()
将 Primary 节点降级为 Secondary 节点
myapp:PRIMARY> rs.stepDown()
这个命令会让 primary 降级为 Secondary 节点,并维持 60s,如果这段时间内没有新的 primary 被选举出来,这个节点可以要求重新进行选举。
也可手动指定时间
myapp:PRIMARY> rs.stepDown(30)
在执行完该命令后,原 Secondary node3:27017 升级为 Primary。
二、方法二优先级设置
思路:
1.为了保证数据的一致性,必须先关闭应用的写服务。
2.提升要升级为 Primary 节点的 Secondary 节点的优先级。
操作如下:
点击(此处)折叠或打开
- arps:PRIMARY> config=rs.conf() //查看当前配置,存入 config 变量中。
- arps:PRIMARY> config.members[0].priority = 3 //修改 config 变量,第三组成员的优先级为 3.优先级 1-100,数字越大,优先级越高
- arps:PRIMARY> rs.reconfig(config) //配置生效
- ......
- ......
- 2017-12-22T15:19:56.596+0800 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
- 2017-12-22T15:19:56.597+0800 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) ok
- 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
- }
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于