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

3

11

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?

Dx answered 30/8, 2014 at 18:16 Comment(1)
Have you tried mongo --eval? I am trying to solve this as well here https://mcmap.net/q/1019672/-what-is-the-proper-way-of-setting-a-mongodb-replica-set-using-docker-and-fig/429521Custos
D
5

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.

Dallapiccola answered 30/8, 2014 at 21:4 Comment(3)
What about --eval? can it be done after mongo is up, but still from shell? What I am trying to do is automate the whole process, here https://mcmap.net/q/1019672/-what-is-the-proper-way-of-setting-a-mongodb-replica-set-using-docker-and-fig/429521Custos
@FelipeSabino: You reinvent the wheel. Have a deep look at mms.mongodb.comDallapiccola
I see your point, mms is a good tip, I didn't know it could spin up the environment like that... but docker makes it very easy for replicating the environment and also makes it easy for local testing when you have several projects going on and with several versions of mongodb at the same time for each environmentCustos
M
2

No you can't do it without rs.initiate()

var x = rs.initiate(                                                                                    
    { _id:'z',                                                                                              
      members:[                                                                                             
        { _id:1, host:'52.68.124.177:27001' },                                                                   
        { _id:2, host:'52.68.124.132:27001' },                                                                   
        { _id:3, host:'52.68.124.181:27001' }                                                                    
      ]                                                                                             
    }   

The following command will add three members to the replica set.

printjson(x);    

The above command can be used to print the JSON with the members of the replica set.

Mittiemittimus answered 14/4, 2015 at 14:14 Comment(0)
N
1

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.

Nacred answered 30/8, 2014 at 21:59 Comment(2)
You can run rs.initiate() without any configuration options - a default configuration will be used if none is provided. However, since you only initiate a replica set once it doesn't make sense as a server command-line option. Normally you'd also want to see the feedback on whether the command was successful, and perhaps provide additional configuration options to create a multi-node replica set in a single command.Megargee
@Megargee whoops so I said required, dunno why.Nacred

© 2022 - 2024 — McMap. All rights reserved.