Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize mongodb replication set without calling rs.initiate()?

Tags:

mongodb

I am trying to initiate mongod with replSet=1 as below: $mongod --dbpath /x/y --replSet 1

But I kept getting errors like "you need to initialize the replication set by calling rs.initiate()", then I start a mongo shell to issue rs.initiate() and the problem gets resolved.

But my question is why a separate mongo shell is needed? Is there a way to do it using mongod option?

like image 759
Maohua Lu Avatar asked Sep 30 '22 23:09

Maohua Lu


2 Answers

Short answer / tl;dr

No.

Slightly longer answer

No, because it makes sense to use the shell.

Answer

When you set up a replica set, you usually have more than one member. Until the replica set is initialized, none of the members holds the necessary configuration. The node you initialize the replica set on becomes primary, storing the configuration. Now you add members either using the rs.add command or by using rs.reconfig. What happens then is that the primary contacts the added member, the config is synced and some other things. It is best practise that ordinary replica set members should be equal so that of one node fails, there is no drawback that another node becomes primary and so the new primary can stay primary until it goes down for some reason or the other itself.

So if you would start a replica set member, where should it get it's config from? Decide for itself what it has to do? That wouldn't work well in a cluster. How should it discover the other members and their config? Remember, replica set members can be in different data centers. And if there was a --IamPrimaryDoAsISay option, what would happen if there was another primary currently in the cluster? And how should a situation in which more than one member was started with that option be dealt with? A step down of the other server? May be just because you replaced a cooler? Or should the just started instance do nothing when there already was a primary? What sense would the option have then in the first place?

And all these complications just to prevent a single command from the shell?

Note: If you want a single server, just start a standalone instance (a mongod without the --replSet option).if you want to explore the possibilities of a replica set, you need more than one member.

like image 145
Markus W Mahlberg Avatar answered Nov 15 '22 07:11

Markus W Mahlberg


But my question is why a separate mongo shell is needed?

No since rs.initiate() also has the ability for configuration options and what not that cannot be specified (and also you probably don't want specified) within the comman line options.

Also rs.initiate() is only run on the primary as such it is not run by defacto on start of every node in the replica set.

Since MongoDB cannot be certain it is part of a set or where it's place is in the set rs.initiate() kind of tells the mongod process something about itself as well.

like image 31
Sammaye Avatar answered Nov 15 '22 08:11

Sammaye