0

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?

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
6
  • I'm a bit confused as to why Node 1 will be set to priority = 2. Isnt it that the lower the priority, the more it is being prioritized in election of primary? – JRA May 25 at 2:06
  • I think my understanding is the opposite. My bad :D – JRA May 25 at 10:03
  • Think it like this.. With priority 2, node1 gets two times more votes than node2. :-) – JJussi May 25 at 15:50
  • In essence, the mongo replica set do not need something to trigger the election of primary is that right? As long as there are members that have higher priority, it will always switch back to them? – JRA May 26 at 3:19
  • 1
    That something (what triggers election this time) is even when node1 changes its status from RECOVERY2 to SECONDARY. That point Node1 get all votes and node2 will change it status to SECONDARY. Then node1 says, well boys and girls... It looks like that I'm voted as the PRIMARY... ;-) So every time than one of the nodes change it status, election is done. BTW, don't forget to "mark" right answer as a "right answer" – JJussi May 26 at 11:53
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.