Force a Member to Become Primary
Overview
You can force a replica set member to become primaryby giving it a highermembers[n].priority
value than any othermember in the set.
Optionally, you also can force a member never to become primary bysetting its members[n].priority
value to0
, which means the member can never seek election as primary. For more information, seePriority 0 Replica Set Members.
For more information on priorities, seemembers[n].priority
.
Consideration
A majority of the configured members of a replica set must beavailable for a set to reconfigure a set or elect a primary. SeeReplica Set Elections for more information.
Procedures
Note
Changed in version 4.0.2: If the parameter enableElectionHandoff
is true(default), when a primary steps down from rs.stepDown()
(or the replSetStepDown
command without the force:true
), the stepped-down primary nominates an eligible secondaryto call an election immediately. Otherwise, secondaries can waitup to settings.electionTimeoutMillis
before calling anelection. The stepped down primary does not wait for the effectsof the handoff. For more information, seeenableElectionHandoff
.
Force a Member to be Primary by Setting its Priority High
This procedure assumes your current primary ism1.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 theconfiguration below. For more information on configurations, see Replica SetConfiguration 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
mongo
shell connected to the primary, use thefollowing sequence of operations to makem3.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)
The last statement calls rs.reconfig()
with the modifiedconfiguration document to configure m3.example.net
to have a highermembers[n].priority
value than the othermongod
instances.
The following sequence of events occur:
m3.example.net
andm2.example.net
sync withm1.example.net
(typically within 10 seconds).m1.example.net
sees that it no longer has highest priority and,in most cases, steps down.m1.example.net
does not step downifm3.example.net
’s sync is far behind. In that case,m1.example.net
waits untilm3.example.net
is within 10seconds of its optime and then steps down. This minimizes theamount of time with no primary following failover.- The step down forces on election in which
m3.example.net
becomes primary based on itspriority
setting.- Optionally, if
m3.example.net
is more than 10 seconds behindm1.example.net
’s optime, and if you don’t need to have a primarydesignated within 10 seconds, you can forcem1.example.net
tostep down by running:
- Optionally, if
- db.adminCommand({replSetStepDown: 86400, force: 1})
This prevents m1.example.net
from being primary for 86,400seconds (24 hours), even if there is no other member that can become primary.When m3.example.net
catches up with m1.example.net
it willbecome 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 electionagain:
- rs.freeze()
The rs.freeze()
provides a wrapper around thereplSetFreeze
database 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
mongo
shell, runrs.status()
to ensure your replicaset is running as expected.In a
mongo
shell connected to themongod
instance running onmdb2.example.net
, freezemdb2.example.net
so that it does not attempt to become primaryfor 120 seconds.
- rs.freeze(120)
- In a
mongo
shell connected themongod
runningonmdb0.example.net
, step down this instance that themongod
is not eligible to become primary for 120seconds:
- rs.stepDown(120)
mdb1.example.net
becomes primary.
Note
During the transition, there is a short window wherethe set does not have a primary.
For more information, consider the rs.freeze()
andrs.stepDown()
methods that wrap thereplSetFreeze
and replSetStepDown
commands.