How to Prevent Secondary to become Primary in replica set

Connect with

How to prevent secondary node of mongodb to become primary node is very easy by setting the priority of it , priority variable in mongodb has own priority while election to become primary. You can set priority in replication of mongoDB, this is basically used to prevent secondary to become primary. Setting of this is mongoDB replication environment variable is very easy process in mongoDB, following are the steps;

Step 1. Copy the replica set configuration to a variable

In the mongo shell, use rs.conf() to retrieve the replica set configuration and assign it to a variable. For example:

cfg = rs.conf()

Step 2. Change each member’s priority value.

Change each member’s members[n].priority value, as configured in the members array. first of all check position of node which you want to change the priority.

cfg.members[0].priority = 0.5
cfg.members[1].priority = 3
cfg.members[2].priority = 2

Step 3. Assign the replica set the new configuration.

Use rs.reconfig() to apply the new configuration.

rs.reconfig(cfg, force: true);

This operation updates the configuration of the replica set using the configuration defined by the value of cfg.


  1. MongoDB Doc

Connect with

1 thought on “How to Prevent Secondary to become Primary in replica set

Leave a Reply

Your email address will not be published. Required fields are marked *