发布于 2015-09-14 14:56:13 | 208 次阅读 | 评论: 0 | 来源: 网络整理
You can force a replica set member to become primary by giving it a higher priority value than any other member in the set.
Optionally, you also can force a member never to become primary by setting its priority value to 0, which means the member can never seek election as primary. For more information, see Secondary-Only Members.
在 2.0 版更改.
For more information on priorities, see Member Priority.
This procedure assumes your current primary is m1.example.net and that you’d like to instead make m3.example.net primary. The procedure also assumes you have a three-member replica set with the configuration below. For more information on configurations, see Replica Set Configuration Use.
This procedure assumes this configuration:
{
"_id" : "rs",
"version" : 7,
"members" : [
{
"_id" : 0,
"host" : "m1.example.net:27017"
},
{
"_id" : 1,
"host" : "m2.example.net:27017"
},
{
"_id" : 2,
"host" : "m3.example.net:27017"
}
]
}
In the mongo shell, use the following sequence of operations to make m3.example.net the primary:
cfg = rs.conf()
cfg.members[0].priority = 0.5
cfg.members[1].priority = 0.5
cfg.members[2].priority = 1
rs.reconfig(cfg)
This sets m3.example.net to have a higher members[n].priority value than the other mongod instances.
The following sequence of events occur:
Optionally, if m3.example.net is more than 10 seconds behind m1.example.net‘s optime, and if you don’t need to have a primary designated within 10 seconds, you can force m1.example.net to step down by running:
db.adminCommand({replSetStepDown:1000000, force:1})
This prevents m1.example.net from being primary for 1,000,000 seconds, even if there is no other member that can become primary. When m3.example.net catches up with m1.example.net it will become primary.
If you later want to make m1.example.net primary again while it waits for m3.example.net to catch up, issue the following command to make m1.example.net seek election again:
rs.freeze()
The rs.freeze() provides a wrapper around the replSetFreeze database command.
在 1.8 版更改.
Consider a replica set with the following members:
To force a member to become primary use the following procedure:
In a mongo shell, run rs.status() to ensure your replica set is running as expected.
In a mongo shell connected to the mongod instance running on mdb2.example.net, freeze mdb2.example.net so that it does not attempt to become primary for 120 seconds.
rs.freeze(120)
In a mongo shell connected the mongod running on mdb0.example.net, step down this instance that the mongod is not eligible to become primary for 120 seconds:
rs.stepDown(120)
mdb1.example.net becomes primary.
注解
During the transition, there is a short window where the set does not have a primary.
For more information, consider the rs.freeze() and rs.stepDown() methods that wrap the replSetFreeze and replSetStepDown commands.