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.
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
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
- 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:
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 🙂