How to Prevent Secondary to become Primary in replica set

Connect with

How to Prevent Secondary to become Primary in replica set
How to prevent secondary to become primary in replica set in MongoDB is very easy by setting the value of the priority variable. Replica set config has its own priority when you create replica set and the value of this variable will be used while election to become primary.

You can set priority in the variable value in replica set config of MongoDB, this is basically used to prevent secondary to become primary. The setting of this priority value 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)
or 
rs.reconfig(cfg, force: true);

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

Reference

  1. MongoDB Doc

Happy learning for How to Prevent Secondary to become Primary in replica set?


Connect with

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

  1. Pingback: All mongoDB Tutorials | mySoftKey

Leave a Comment

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