- Replication >
- Replica Set Tutorials >
- Member Configuration Tutorials >
- Prevent Secondary from Becoming Primary
Prevent Secondary from Becoming Primary¶
On this page
To prevent a secondary member from ever becoming a
primary in a failover, assign the secondary a priority
of 0
, as described here. You can set this “secondary-only mode” for
any member of the replica set, except the current primary. For a
detailed description of secondary-only members and their purposes, see
Priority 0 Replica Set Members.
To configure a member as secondary-only, set its
priority
value to 0
in
the members
document in its replica set
configuration. Any member with a
priority
equal to 0
will
never seek election and cannot become
primary in any situation.
MongoDB does not permit the current primary to have a priority
of 0
. To prevent the current primary from again becoming a primary,
you must first step down the current primary using
rs.stepDown()
, and then you must reconfigure the replica
set with rs.conf()
and
rs.reconfig()
.
Example¶
As an example of modifying member priorities, assume a four-member
replica set. Use the following sequence of operations to modify member
priorities in the mongo
shell connected to the primary.
Identify each member by its array index in the
members
array:
The sequence of operations reconfigures the set with the following priority settings:
- Member at
0
has a priority of2
so that it becomes primary under most circumstances. - Member at
1
has a priority of1
, which is the default value. Member1
becomes primary if no member with a higher priority is eligible. - Member at
2
has a priority of0.5
, which makes it less likely to become primary than other members but doesn’t prohibit the possibility. - Member at
3
has a priority of0
. Member at3
cannot become the primary member under any circumstances.
When updating the replica configuration object, access the replica set
members in the members
array with the
array index. The array index begins with 0
. Do not confuse
this index value with the value of the
_id
field in each document in
the members
array.
Warning
- The
rs.reconfig()
shell method can force the current primary to step down, which causes an election. When the primary steps down, themongod
closes all client connections. While this typically takes 10-20 seconds, try to make these changes during scheduled maintenance periods. - To successfully reconfigure a replica set, a majority of the members must be accessible. If your replica set has an even number of members, add an arbiter to ensure that members can quickly obtain a majority of votes in an election for primary.