Database Replication Failing

Hi Team,

I am using MongoDB 5.0 Version for my project and using Ubuntu 20.04 LTS, The issue I am facing in Replication of MongoDB.

Basically I have 2 Ubuntu Servers instead of 3 servers ,with 3 mongod services. In Primary one mongo which is running on 27017, and In secondary it’s using 27017 & 27018, So totally 3 mongod services are running. All are connected in the same network, services also running fine.

I’ve initiated MongoDB replica from primary server with high priority value 3, and secodary servers mongod having priority 2, 1 respectively.

Now I’ll explain the scenario, I am testing automatic failover. When both the servers are UP, I am manually shutting DOWN primery server, after 2 to 4 secs secondary server changing his mode to primary and data also inserting into db, here no issues.

But, When both the servers are UP, I am shutting DOWN secondary server, In this test case, after few seconds primary server changing his behavior to SECONDARY, I am clueless now. When I power on secondary server, Primary changing to Primary DB. I don’t know whats happening and I am stuck with this now.

Kindly help me to solve this scenario, Please let me know for further info.

Expecting positive response, thanks in advance.

are you shutting down the ubuntu server or one of the mongod processes?

1 Like

From the observed behaviour, I suspect the same as you suspect. That is, he is shutting down the second Ubuntu machine, so he is shutting down 2 mongod processes, so he does not have majority, so he does not have a primary.

1 Like

Apologize for the confusion, here I testing with two scenarios. One is I am powering of the secondary server, another one is just unplugging the ethernet cable of secondary server.

Exactly, but the thing is I am not touching primary sever, while primary server is UP, I am powering off / unplugging the ethernet.

Read about replica set election to understand why the single mongod running on your main Ubuntu server cannot be PRIMARY once it cannot reach the 2 other mongod instances running on the second Ubuntu server what ever you shut it down, you disconnect it from the network or it crashes.

You will also understand why one of the mongod running on the second Ubuntu server becomes the PRIMARY when you unplug the network cable.

Yes confusion because you did not mentioned

in the original post.

So, Is it not possible to implement Replica using two servers? Even though MongDB official doc mentioned need three servers, but I am using two servers. Sorry for too many questions.

It is NOT what I wrote. Documentation says three and you try with two. It works but NOT in the way you think it does. It works like it is documented. If there is a majority then you have a primary. If 1 instance on one server cannot read any of the other two instances, it has not majority and does not become primary. The other 2 instances being on the same hardware have majority and one will become primary.