Is there an automatic way for a secondary member to promote to primary member in a replica set?

All we need is an easy explanation of the problem, so here it is.

Here is my setup:

Node 1 = 10.1.0.1 (Primary) - priority 1
Node 2 = 10.1.0.2 (Secondary) - priority 1
Node 3 = 10.1.0.3 (Secondary) - priority 0

In the event of Node 1 failure, Node 2 will be set to Primary of the mongo replica set.

Once Node 1 is already accessible, is there a way to automatically promote it to Primary and Node 2 will be set to Secondary again once Node 1 catches up on the replica set?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Yes, easily! Set priority of node 1 to higher than 1…

Node 1 = 10.1.0.1 (Primary) - priority 2
Node 2 = 10.1.0.2 (Secondary) - priority 1
Node 3 = 10.1.0.3 (Secondary) - priority 0.5

Method 2

The main factor here is the Replica Set Elections.

Replica sets use elections to determine which set member will become primary. Replica sets can trigger an election in response to a variety of events, such as:

  • Adding a new node to the replica set,
  • Initiating a replica set,
  • Performing replica set maintenance using methods such as
    rs.stepDown() or rs.reconfig(), and
  • The secondary members losing connectivity to the primary for more
    than the configured timeout (10 seconds by default).

I think this is what you are looking for:

rs.stepDown()
instructs the primary of the replica set to become a secondary. After the primary steps down, eligible secondaries will hold an election for a new primary.

Another option would be that you can force a replica set member to become primary by giving it a higher members[n].priority value than any other member in the set. Note that a majority of the configured members of a replica set must be available for a replica set to reconfigure or elect a primary.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply