What is the correct way to install mongodb replica set using docker and fig?

What is the correct way to install mongodb replica set using docker and fig?

I tried to follow the official mongodb tutorials to create a fig.yml file with some replica sets, but was always blocked by the way rs.initiate() and rs.add("<hostname><:port>") called.

I found this SO answer , explaining why I cannot start everything simply with a shell without calling rs.initiate() , so how can I do this?

Oh, and I use mongo:latest (v2.6.5) as the base image without any changes.

+5
source share
2 answers

I had a similar problem, here is what I did. I use docker-compose instead of fig.

In my docker essay.

 mongors: image: mongo ports: - "27017:27017" volumes: - ./mongo:mongo entrypoint: mongo/entrypoint.sh 

In my entrypoint.sh:

 #!/bin/bash mongod --port 27018 --replSet rs0 --fork --syslog --smallfiles mongo --port 27018 --eval "rs.initiate({_id : 'rs0', members : [{_id : 0, host : 'localhost:27018'}]})" mongo --port 27018 --eval "while(true) {if (rs.status().ok) break;sleep(1000)};" 

Make sure it is executable:

 chmod +x mongo/entrypoint.sh 

This is a bit hacky, but it works :)

+1
source

Stop the mongod service first before installing the replica set

  service mongodb stop 

mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"

  mongod --port 27017 --dbpath "D:\set up\mongodb\data" --replSet rs0 --fork 

It will start the mongod instance with the name rs0, on port 27017. Now run the command prompt and connect to this mongod instance.

In the mongo client, run the rs.initiate () command to initiate a new set of replicas.

  rs.initiate() 

To verify replica set configuration, run the rs.conf () command.

rs.conf () should have the following

 { "_id" : "rs0" "version" : 1, "members" : [ { "_id" : 0, "host" "localhost:27017" }, { "_id" : 1, "host" "localhost:27018" }, { "_id" : 2, "host" "localhost:27019" } ] } 

Now you can add additional nodes to the replication set by specifying the host name that you gave them.

  rs.add("localhost:27019") { "ok" : 1 } 

Do this for each of the remaining members of the replication. Your replication set will now be up and running.

To check the status of the replication problem, run the rs.status () command.

 rs.status() 

Hope this helps.

-3
source

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


All Articles