MongoDB replica set


I am trying to set a MongoDB replica set.

The idea is, having 4 instances in aws: 1 – Nodejs app, a simple webpage, configured to connect to a DB_HOST on port 27017 2 – MongoDB Primary 3 – MongoDB replica 4 – MongoDB replica

My NodeJs app works without any problem if i access its IP in my browser, i can see the homepage, but if i try to put :27017/posts, it keeps loading and than gives the error.

This is how i configured the project with terraform and bash scripts, as i need npm to run the app.

mongodb conf:

# mongod.conf  # for documentation of all options, see: #   http://docs.mongodb.org/manual/reference/configuration-options/  # Where and how to store data. storage:   dbPath: /var/lib/mongodb   journal:     enabled: true #  engine: #  mmapv1: #  wiredTiger:  # where to write logging data. systemLog:   destination: file   logAppend: true   path: /var/log/mongodb/mongod.log  # network interfaces net:   port: <%= @port %>   bindIp: <%= @bindIp %>   #processManagement:  #security:  #operationProfiling:  #replication: replication:   replSetName: rs0  #sharding:  ## Enterprise-Only Options:  #auditLog:  #snmp: 

def variables:

default['mongodb']['port'] = 27017 default['mongodb']['bindIp'] = '0.0.0.0 

this is the script to configure IP for mongodb primary and secondaries:

#!/bin/bash  sudo echo '10.0.1.100 rs0' >> /etc/hosts  sudo systemctl enable mongod sudo systemctl start mongod  mongo mongodb://10.0.1.100 --eval "rs.initiate( { _id : 'rs0', members: [{ _id: 0, host: '10.0.1.100:27017' }]})" mongo mongodb://10.0.1.100 --eval "rs.add( '10.0.2.100:27017' )" mongo mongodb://10.0.1.100 --eval "rs.add( '10.0.3.100:27017' )" mongo mongodb://10.0.1.100 --eval "db.isMaster().primary" mongo mongodb://10.0.1.100 --eval "rs.slaveOk()"   sleep 60; sudo systemctl restart metricbeat sudo systemctl restart filebeat  sleep 180; sudo filebeat setup -e \   -E output.logstash.enabled=false \   -E output.elasticsearch.hosts=['10.0.105.100:9200'] \   -E setup.kibana.host=10.0.105.101:5601 && sudo metricbeat setup 

As the nodejs app need a DB_HOST variable to start seeding the mongodb, i had to set this script.

#!/bin/bash sleep 120 cd /home/ubuntu/AppFolder/app  export DB_HOST=mongodb://10.0.1.100:27017,10.0.2.100:27017,10.0.3.100:27017?replicaSet=rs0   node /home/ubuntu/AppFolder/app/seeds/seed.js  sudo npm install sudo npm start &  sudo filebeat modules enable nginx 

Once i spin up my instances with terraform, and log inside the Database primary or any of them, and try a simple command as rs.status() nothing happens, the console waits for me to imput something else. and in the mongodb.log i got this REPL MESSAGE

2020-05-10T01:56:27.135+0000 I REPL     [initandlisten] Did not find local voted for document at startup. 2020-05-10T01:56:27.135+0000 I REPL     [initandlisten] Did not find local replica set configuration document at startup;  NoMatchingDocument: Did not find replica set configuration document in local.system.replset 

i can see that my configuration has been reconized and it does show in the log rs0 but right after i get the above message. And this happens on all 3 mongodb instance. (Mongodb service is up and running).

On the other hand, if i ssh inside my APP instance, in the logs i get this

 DeprecationWarning: `openSet()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/4.x/docs/connections.html#use-mongo-client  /home/ubuntu/AppFolder/app/node_modules/mongodb/lib/replset.js:365           process.nextTick(function() { throw err; })                                         ^ MongoError: no primary found in replicaset or invalid replica set name     at /home/ubuntu/AppFolder/app/node_modules/mongodb-core/lib/topologies/replset.js:560:28     at Server.<anonymous> (/home/ubuntu/AppFolder/app/node_modules/mongodb-core/lib/topologies/replset.js:312:24)     at Object.onceWrapper (events.js:315:30)     at emitOne (events.js:116:13)     at Server.emit (events.js:211:7)     at /home/ubuntu/AppFolder/app/node_modules/mongodb-core/lib/topologies/server.js:300:14     at /home/ubuntu/AppFolder/app/node_modules/mongodb-core/lib/connection/pool.js:469:18     at _combinedTickCallback (internal/process/next_tick.js:132:7)     at process._tickCallback (internal/process/next_tick.js:181:9) sudo: unable to resolve host ip-10-0-103-40 npm WARN lifecycle solution-code@1.0.0~postinstall: cannot run in wd solution-code@1.0.0 node seeds/seed.js (wd=/home/ubuntu/AppFolder/app) npm WARN solution-code@1.0.0 No repository field. 

Sorry for the long post i just wanted to be sure to share all the required info and thank you in advance for your help.