Prevent Secondary from Becoming Primary
On this page
In a replica set, by default all secondary members are eligible to
become primary through the election process. You can use the
priority to affect the
outcome of these elections by making some members more likely to become
primary and other members less likely or unable to become primary.
Secondaries that cannot become primary are also unable to trigger elections. In all other respects these secondaries are identical to other secondaries.
To prevent a secondary member from ever becoming a primary
in a failover, assign the secondary a priority of
described here. For a detailed description of secondary-only members and their purposes,
see Priority 0 Replica Set Members.
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
members[n]._id field in each document in
MongoDB does not permit the current primary to have a priority
0. To prevent the current primary from again becoming a primary,
you must first step down the current primary using
This tutorial uses a sample replica set with 5 members.
rs.reconfig()shell method can force the current primary to step down, which causes an election. When the primary steps down, the
mongodcloses all client connections. While this typically takes 10-20 seconds, try to make these changes during scheduled maintenance periods.
Avoid reconfiguring replica sets that contain members of different MongoDB versions as validation rules may differ across MongoDB versions.
Retrieve the current replica set configuration.
rs.conf() method returns a replica set
configuration document that
contains the current configuration for a replica set.
mongosh, when connected to a primary, run the
rs.conf() method and assign the result to a variable:
cfg = rs.conf()
The returned document contains a
members field which contains an array
of member configuration documents, one document for each member of the
Assign priority value of
To prevent a secondary member from becoming a primary, update the
To assign a priority value to a member of the replica set, access the
member configuration document using the array index. In this
tutorial, the secondary member to change corresponds to the
configuration document found at position
2 of the
cfg.members.priority = 0
The configuration change does not take effect until you reconfigure the replica set.
Reconfigure the replica set.
rs.reconfig() method to reconfigure the replica set
with the updated replica set configuration document.
cfg variable to the