2 / 2
Sep 2024

Hi, I’m new with MongoDB and I have a silly question.

I created two VM (mongodb1-test: SECONDARY & mongodb2-test: PRIMARY), in each VM i installed mongo and created a replicaset.
I disconnected from the network my VM mongodb2-test, and the two nodes became SECONDARY.

I tried to set the mongodb1-test as PRIMARY, but i get this error :MongoServerError[NotWritablePrimary]: New config is rejected :: caused by :: replSetReconfig should only be run on a writable PRIMARY. Current state SECONDARY;

I know is needed 3 nodes in a cluster for the election but im trying it with two nodes. I wonder if I can force one secondary to become primary when the connection is lost among the nodes. Or if I should use 3 nodes instead of two.

4 months later

Hi Ruben,

For the election to work you would need 3 nodes i.e. 1 primary and 2 secondary nodes and incase of primary failure one of the secondary nodes can be elected as primary.
Refer https://www.mongodb.com/docs/manual/core/replica-set-elections/ for the details of the process.

If you have some constraints and not want to add another secondary node then you can add the 3rd node as an arbiter node. The arbiter node does not have the copy of the data as in secondary node, however it can vote in the election process and help in the election process. Refer to https://www.mongodb.com/docs/manual/core/replica-set-arbiter/ for more details.

Thanks
Gurjit