MongoDB secondary node does not become primary when other nodes are stopped

mongodbmongodb-3.2mongodb-3.4

I want to cluster MongoDB by using a replica set. I have created a three-node replicaset (rs0,rs1,rs2), and run the DBs in parallel with no issues. But when I shut down any two instance (one primary and secondary), the remaining secondary member is not made the primary, instead it remains as a secondary.

Step1:

  • rs0: primary
  • rs1: secondary
  • rs2: secondary

Every thing is ok. When

Step2:

  • rs0: stopped
  • rs1: stopped
  • rs2: secondary (This is not promoted to primary)

Why?

Best Answer

This is by design. If you want to run with only one node, restart that node without replica set switch.

If a majority of the replica set is inaccessible or unavailable to the current primary, the primary will step down and become a secondary. The replica set cannot accept writes after this occurs, but remaining members can continue to serve read queries if such queries are configured to run on secondaries.

Read these resources for more details:

  1. Replica Set Elections
  2. mongo replica set to single server
  3. Voting in MongoDB