-2

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
Erenn
  • 625
  • 6
  • 18
  • 1
    Follow MongoDB documentation for creating a replica set. – D. SM Jul 23 '20 at 03:27
  • 1
    Do I need the db users and passwords of these 2 db's? And can it be done one command on my side? Do I need to configuration on those 2 db's? – Erenn Jul 23 '20 at 03:46
  • 1
    Here are a tutorial: [Deploy a Replica Set for Testing and Development](https://docs.mongodb.com/manual/tutorial/deploy-replica-set-for-testing/). – prasad_ Jul 23 '20 at 07:31
  • Just one question. Do I need to change something on primary db? – Erenn Jul 24 '20 at 04:23
  • 3
    There is no such thing as ""primary db" in a replica-set. A replica-set can have 1 to 50 nodes. So, your replica-set has how many nodes? Of these nodes only one can be a primary node. To setup a replica-set means (i) start all nodes, (ii) connect to one node using `mongo`, and (iii) initiate the replica-set. I suggest, stop all `mongod` processes and follow the tutorial I had provided as a link. – prasad_ Jul 25 '20 at 10:43
  • I see now. I have access to one secondary node. I will initiate replica-set and I will provide my local pc's ip address to that secondary node. Am I right? – Erenn Jul 25 '20 at 11:51
  • 2
    Also, see this about [Replication](https://docs.mongodb.com/manual/replication/index.html) (in general, and what it means). – prasad_ Jul 28 '20 at 04:48

1 Answers1

1

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>
Jin Thakur
  • 2,711
  • 18
  • 15