Navigation
This version of the documentation is archived and no longer supported.

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.

{
   "_id" : <num>,
   "host" : <hostname:port>,
   "priority" : 0
}

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:

cfg = rs.conf()
cfg.members[0].priority = 2
cfg.members[1].priority = 1
cfg.members[2].priority = 0.5
cfg.members[3].priority = 0
rs.reconfig(cfg)

The sequence of operations reconfigures the set with the following priority settings:

  • Member at 0 has a priority of 2 so that it becomes primary under most circumstances.
  • Member at 1 has a priority of 1, which is the default value. Member 1 becomes primary if no member with a higher priority is eligible.
  • Member at 2 has a priority of 0.5, which makes it less likely to become primary than other members but doesn’t prohibit the possibility.
  • Member at 3 has a priority of 0. Member at 3 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, the mongod 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.