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?
No.
No, because it makes sense to use the shell.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With