Replication failed to connect to server [IP:27017] on first connect [MongoError: getaddrinfo ENOTFOUND - node.js

I have setup Replication and trying to connect
URI:
mongodb://[userName:password]#IP1:27017,
[userName:password]#IP2:27017/dbName?
authSource=admin&w=1&replicaSet=replicaqa
but I am getting below error:
{ MongoError: failed to connect to server [host_name_ip1:27017] on first connect
[MongoError: getaddrinfo ENOTFOUND [host_name_ArbiterIP] [host_name_ArbiterIP]:27017]
When I am individually trying to connect [IP1:27017] without replication it works.
Here is my Mongod.conf
systemLog:
destination: file
logAppend: true
path: /var/mongodb/mongod.log
# Where and how to store data.
storage:
dbPath: /data/var/lib/mongo
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
security:
authorization: "disabled"
#operationProfiling:
replication:
replSetName: "replicaqa"
Am I missing something while configuring replication?
I can see mongod instance is started with replication where I can see (Primary>, Secondary> and Arbitary> And each instance is on remote location.

Found mistake,
When we configure replication with rs.conf() we give domain name and update DNS in etc/hosts file.
In my case, I have updated it in mongo instance(etc/hosts) but not updated where my server is hosted (server is hosted on another instance).
Updated hosts file where Node service is running and now it's working for me.
when I connect without replication I was able to connect, but the issue is when I am trying to connect with Replication.
(No matter even if I have given IP address in my DB connection string URL.
like
mongodb://[userName:password]#IP1:27017,[userName:password]#IP2:27017/dbName?
authSource=admin&w=1&replicaSet=replicaqa
It will try to resolve the domain name. Because we have configured in rs.conf(), that's what happened with me )
Path of hosts file in windows
C:\Windows\System32\drivers\etc\hosts file
A path for hosts file in Linux/ CentOS
/etc/hosts
DNS
10.XX.XX.XX viavi.local
10.XX.XX.XXX domain_name_configured_while_replication

Related

MongoDB seeding and replication

I have this small project i am working on it but i got stuck with mongodb replicaset and reverse proxy. I explain myself better.
I have a Web application running one instance(just a simple web page). And this application by default connects to port 3000. I used nginx reverse proxy to use port 80 instead of 3000 (and this works)
This application is a nodejs app, and in its configuration files, it should connect to mongodb on port on
mongodb://10.0.1.100:27017/posts (value exported by code into my instance)
to display some random posts.
On my mongod.conf I bind my IP to 0.0.0.0 and set port to 27017
but when i log on my app instance and seed my mongoDB (it get seeded succesfully) but when i run
npm start
I got an error that the port 3000 is already used by another service.
So, my point is, i want to connect to my /posts from my web page, without having to type :/posts
this is my mongod.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: <%= #bind_ip %>
#processManagement:
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
and in the attribute i set:
default['mongodb']['port'] = 27017
default['mongodb']['bind_ip'] = '0.0.0.0'
Question Bonus:
mongodb://10.0.1.100:27017/posts
This value has been exported in the envroment variables, but as i need to set a replication over 2 more mongodb, i tried to insert in line separated but commas, but i get an error. is there a way how to export multiple env value under one variable name?
I hope i explained myself clearly and thank you in advance guys.

Connect and allow multiple ip's for mongodb accepting connections

I have a mongodb config file where bind ip is 127.0.0.1
Now mongodb is hosted on one instance and nodejs server is hosted on another instance which ip is (dummy ip) 184.54.125.132
On my mongodb Config file is
net:
port: 27017
bindIp: 0.0.0.0
By this my node server connect because it allows all (which is wrong)
How I add localhost ip of mongodb and my node server ip on bindIp so that mongodb server will listen only through node server and mongodb local.
I tried
bindIP: [127.0.0.1,184.54.125.132] //Not work
I tried
bindIp: 127.0.0.1,184.54.125.132 //Not work
I tried
bind_ip: [127.0.0.1,184.54.125.132] //Not work
I tried
bind_ip: 127.0.0.1,184.54.125.132 //Not work
Anyone suggest me where I am doing mistake. I also enabled authentication
It should be :
bindIp : 127.0.0.1,184.54.125.132
And dont forget to restart your mongod, after changing the config file, otherwise it wont work.
sudo service mongod restart
or
sudo systemctl mongod restart
.
Also, dont forget to open the port so that outside network can access that port(27017 or whatever port your mongodb is hosted on).
If you are using AWS EC2 to host your mongodb, you need to add that port to your inbound security rules, and that specific IP wherever you want to access it from

how to connect same mongodb (in compass using hostname: localhost and port : 27017) local in 2 different PC's [duplicate]

I follow this mongoose document enter link description here
mongoose.connect('mongodb://localhost/waterDB');
Using This, I can connect local machine waterDBmongoDB DataBase
My Personal Machine Local IP : 192.168.1.5
My Server Machine Local IP : 192.168.1.100
Both machine have waterDB DataBase . There is no username and password for both DB
I wanted to connect Server Machine waterDB Inside My Personal Machine.
According To This : mongoose.connect('mongodb://username:password#host:port/database?options...');
I try : mongoose.connect('mongodb://192.168.1.100:27017/waterDB');
But,
MongoError: failed to connect to server [192.168.1.100:27017] on first connect
at null.<anonymous> (/home/water/node_modules/mongodb-core/lib/topologies/server.js:313:35)
at emitOne (events.js:77:13)
at emit (events.js:169:7)
..........
Any solution for err ?
Thank (#_#)
It might be a problem with your MongoDB instance listening on localhost only.
You can change the bind address in MongoDB's configuration file. The config file may be located in /etc/mongodb.conf or /etc/mongod.conf. There are also 2 config file formats:
Old format (still supported):
bind_ip = 0.0.0.0
YAML (version 2.6+):
net:
bindIp: 0.0.0.0
After changing the config file you have to restart the MongoDB server.
Try without the Port no.
mongoose.connect('mongodb://192.168.1.100/waterDB');
this should work for you.
But make sure both are connected on the same network, if you are connected on other network than your server is, then it wont work
You will have to use SSH tunnel in this case. Refer to the following link which shows how you can create SSH tunnel.
Node.js SSH Tunneling to MongoDB using Mongoose
For me (using windows and mongo version 6, and remotely connecting from Mac), after changing config file to
net:
port: 27017
bindIp: "*" # OR 0.0.0.0
Then starting server with: mongod, it was still binding to 127.0.0.1 (localhost). I had to start the server with: mongod --bind_ip_all

MongoDB Configuration File not working

I am on Windows 10 environment.
By default, My mongod.cfg file has a content which is as follows i.e
# mongod.conf
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
By this when I will start MongoDB through CMD command i.e
net start mongodb
It will be running.
Now I have to use replica set and for that, I have to make some changes to configuration file i.e I have to add some piece of code for accessing the Replication i.e
# mongod.conf
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
net:
bindIp: 0.0.0.0
port: 27017
replication:
replSetName: test
after that when I start the service through command net start MongoDB it will return the error i.e
The service is not responding to the control function.
More help is available by typing NET HELPMSG 2186.
I am not sure what is this? If anyone has any Idea about that? How can I fix this? Any Help is appreciated.

Connection attempt failed when connecting to MongoDB deployment from mongo shell

first question and complete beginner so apologies in advanced for any silly mistakes.
I have created a server on Amazon Web Services and then linked that through the MongoDB Cloud Manager where I made a replica set.
I have been following the tutorial on the mongoDB cloud documentation but have become stuck on the final part - "Connect to a MongoDB Process".
It says "Cloud Manager provides a mongo shell command that you can use to connect to the MongoDB process if you are connecting from the system where the deployment runs" - Can I not do this because the deployment is running on the Amazon Server?
When I enter the mongo shell command this is what it reads:
MongoDB shell version: 3.0.4
connecting to: AM-0.amigodb.0813.mongodbdns.com:27001/AmigoMain_1
2015-08-07T18:41:56.806+0100 W NETWORK
Failed to connect to 52.18.23.14:27001 after 5000 milliseconds, giving up.
2015-08-07T18:41:56.809+0100 E QUERY
Error: couldn't connect to server AM-0.amigodb.0813.mongodbdns.com:27001 (52.18.23.14), connection attempt failed
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
I followed the instructions for the security settings on the Amazon Web Service but thinking that I may well have made a mistake.
Would greatly appreciate any help or where to go for answers.
Thanks,
Louis
Mongo by default only listens to connections on localhost. You'll need to edit mongo.conf and add your IP to the "bind ip" setting.
This is my mongod.conf file:
processManagement:
fork: true
net:
bindIp: 127.0.0.1
port: 27017
storage:
dbPath: "/data/db"
systemLog:
destination: file
path: "/var/log/mongod.log"
logAppend: true
storage:
journal:
enabled: true
It also gave me the same error message when I changed my bindIp to 0.0.0.0

Resources