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

I am trying to start mongod with replSet = 1 as shown 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 run the mongo shell to release rs.initiate (), and the problem is resolved.

But my question is, why do I need a separate mango shell? Is there a way to do this using the mongod option?

+2
source share
3 answers

Short answer / tl; dr

Not.

Slightly long answer

No, because it makes sense to use a shell.

Answer

When you set up a replica set, you usually have several members. As long as the replica set is not initialized, none of the members have the necessary configuration. the node that you initialize the replica set becomes the main, saving the configuration. Now you add items either with the rs.add command or with rs.reconfig . What happens then is that the main contact adds a member, configuration synchronization, and some other things. It is best that the regular members of the replica set are equal, so that one of the node fails, there is no dearth of the fact that the other node becomes the main one, and so the new primary element can remain primary until it goes for some reason or another by itself.

So, if you run a member of a replica set, where should it get the configuration from? Decide for yourself what he should do. This will not work in the cluster. How should he find other members and their configuration? Remember that replica set members can be located in different data centers. And if there was a parameter --IamPrimaryDoAsISay , what will happen if there is another main cluster? And how should a situation be solved in which more than one member with this option has been started? Switching to another server? Maybe just because you replaced the cooler? Or should the newly started instance do nothing when it was already the primary? What is the meaning of this option in the first place?

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

Note. If you need one server, just run a standalone instance (mongod without the --replSet option). If you want to explore the possibilities of a replica set, you will need several members.

+2
source

But my question is, why do I need a separate mango shell?

No, since rs.initiate() also has the ability for configuration parameters and what cannot be specified (and you probably don't want to specify as well) in the parameters of the comman line.

Also rs.initiate() runs only on the primary, so it does not run defacto when each node in the replica set is started.

Since MongoDB cannot be sure that it is part of the set or where it is located in the rs.initiate() , it tells the mongod process something about itself.

+1
source

No, you cannot do this 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 JSON with members of a replica set.

+1
source

Source: https://habr.com/ru/post/1208906/


All Articles