Overview
You can force a replica set member to become primary
by giving it a higher
members[n].priority value than any other
member in the set.
Optionally, you also can force a member never to become primary by
setting its members[n].priority value to
0, which means the member can never seek election as primary. For more information, see
Priority 0 Replica Set Members.
For more information on priorities, see
members[n].priority.
Consideration
A majority of the configured members of a replica set must be available for a set to reconfigure a set or elect a primary. See Replica Set Elections for more information.
Procedures
Note
When you step down a primary using rs.stepDown() or
replSetStepDown without setting the force field to
true, the stepped-down primary nominates an eligible secondary
to call an election immediately.
Force a Member to be Primary by Setting its Priority High
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 a
mongoshell connected to the primary, use the following sequence of operations to makem3.example.netthe primary:cfg = rs.conf() cfg.members[0].priority = 0.5 cfg.members[1].priority = 0.5 cfg.members[2].priority = 1 rs.reconfig(cfg) The last statement calls
rs.reconfig()with the modified configuration document to configurem3.example.netto have a highermembers[n].priorityvalue than the othermongodinstances.The following sequence of events occur:
m3.example.netandm2.example.netsync withm1.example.net(typically within 10 seconds).m1.example.netsees that it no longer has highest priority and, in most cases, steps down.m1.example.netdoes not step down ifm3.example.net's sync is far behind. In that case,m1.example.netwaits untilm3.example.netis within 10 seconds of its optime and then steps down. This minimizes the amount of time with no primary following failover.The step down forces an election in which
m3.example.netbecomes primary based on itsprioritysetting.
Optionally, if
m3.example.netis more than 10 seconds behindm1.example.net's optime, and if you don't need to have a primary designated within 10 seconds, you can forcem1.example.netto step down by running:db.adminCommand({replSetStepDown: 86400, force: 1}) This prevents
m1.example.netfrom being primary for 86,400 seconds (24 hours), even if there is no other member that can become primary. Whenm3.example.netcatches up withm1.example.netit will become primary.If you later want to make
m1.example.netprimary again while it waits form3.example.netto catch up, issue the following command to makem1.example.netseek election again:rs.freeze() The
rs.freeze()provides a wrapper around thereplSetFreezedatabase command.
Force a Member to be Primary Using Database Commands
Consider a replica set with the following members:
mdb0.example.net- the current primary.mdb1.example.net- a secondary.mdb2.example.net- a secondary .
To force a member to become primary use the following procedure:
In a
mongoshell, runrs.status()to ensure your replica set is running as expected.In a
mongoshell connected to themongodinstance running onmdb2.example.net, freezemdb2.example.netso that it does not attempt to become primary for 120 seconds.rs.freeze(120) In a
mongoshell connected themongodrunning onmdb0.example.net, step down this instance that themongodis not eligible to become primary for 120 seconds:rs.stepDown(120) mdb1.example.netbecomes primary.Note
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.