Search code examples
mongodbdatabase-replicationreplicaset

How to setup primary DB for replica Mongo DB


I am trying run a nodejs application. In order to do that I need to setup replica mongodb. I couldn't understand what should I do where. I need step by step explanation like my IQ is around room tempratures.

So this is the ENV variable for DB

MAIN_DATABASE=mongodb://127.0.0.1:27017,localhost:27018/maindb?replicaSet=rs0

I am running this command and starting a replica mongodb

mongod --port 27021 --dbpath "/data" --replSet rs0

And this is the error message that I get

-> no primary found in replicaset or invalid replica set name

I have ip's of 2 mongo db's running

62.210.**.**  mongo1
212.129.**.** mongo2

Solution

  • You just need to restart your Mongo (killall mongod and then start) with the following:

    mongod --replSet "rs0" --bind_ip localhost,hostname|62.210.**.**,hostname1|62.210.**.**
    

    Alternatively, you can also specify the replica set name and the IP addresses in a configuration file:

    replication:
       replSetName: "rs0"
    net:
       bindIp: localhost,<hostname(s)|ip address(es)>
    

    To start mongod with a configuration file, specify the configuration file’s path with the --config option:

    mongod --config <path-to-config>