Connection attempt failed when connecting to MongoDB deployment from mongo shell - node.js

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

Related

compass refuse to connect to remoto mongodb

I am trying to connect with my MongoDB data base using compass or through my NodeJS app but it refuse to connect, It was working but suddenly it now not ,I am using windows 11
I have try to connect it from the PowerShell using this and it actually connected
mongo "mongodb+srv://<username>:<password>#cluster0.oeari.mongodb.net/dbname?authSource=admin"
I tried edit the security in config file in bin folder but nothing changed , I also have configure the IP address option in MongoDB to accept connect from anywhere but still the same
security:
authorization: enabled
net:
port: 27017
bindIp: 0.0.0.0

connection refused connecting to remote mongodb server

So we've accumulated enough applications in our network that use MongoDB to justify building a dedicated server specifically for MongoDB. Unfortunately, I'm pretty new to mongodb (coming from SQL/MySQL derivatives). I have followed several guides on installing and configuring mongodb for my environment. None are perfect, but I think I'm close... I've have managed to get to a point that I can connect to the db server from the local server using the following command:
mongo -u user 127.0.0.1/admin
However, I'm NOT able to connect to the server using this from either the local OR a remote computer using it's network address, IE:
mongo -u user 192.168.24.102/admin
I've tried both with authentication enabled and disabled, and I've tried setting the bindIP to 192.168.24.102 and 0.0.0.0 with no love. Thinking it was a Firewall issue, I disabled the firewall entirely... same. no love...
so what's the secret sauce? how do I connect to a MongoDB server remotely?
Some notes to know: This server is on a local network only. There will be some NAT shenanigans at some point directing public traffic to it from remote application servers, but only specific ports (we will NOT be using 27017 when that happens) and it will sit behind a pretty robust firewall appliance, so I'm not worried about securing the server as I about securing MongoDB itself.
This answer assume a setup where a Linux server is completely remote and has MongoDB already installed.
Steps:
1. Connect to your remote server over SSH.
ssh <userName>#<server-IP-address>
2. Start Mongo shell and add users to MongoDB.
Add the admin;
use admin
db.createUser(
{
user: "AdminSammy",
pwd: "AdminSammy'sSecurePassword",
roles: [
{"userAdminAnyDatabase",
"dbAdminAnyDatabase",
"readWriteAnyDatabase"}
]
}
)
Then add general user/users. Users are added to specific databases.
use some_db
db.createUser({
user: 'userName',
pwd: 'secretPassword',
roles: [{ role: 'readWrite', db:'some_db'}]
})
3. Edit your MongoDB config file, mongod.conf, that is found in etc directory.
sudo vim /etc/mongod.conf
Scroll down to the #security: section and add the following line. Make sure to un-comment the security: line.
security:
authorization: 'enabled'
After authorization has been enabled only those authenticated with password will access the database. In this case these are the ones added in step 2 above.
Note: Visual Studio code can also be used over SSH to edit the mongo.conf file.
4. Add remote server's IP address to mongod.conf file.
Look for the net line and add the IP address of the server that is hosting this MongoDB installation, example 178.45.55.88
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1, 178.45.55.88
5. Open port 27017 on your server instance.
This allows access to your MongoDB server from anywhere in the world to anyone who knows your remote server IP address. This is one reason to have authenticated users. More robust ways of handling security are really important! Consult MongoDB manual for that.
Check firewall status using ufw.
sudo ufw status
If its not active, activate it.
sudo ufw enable
Then,
sudo ufw allow 27017
Important: You also need to allow port 22 for your SSH communication with your remote server. Otherwise you will be locked out from your remote server. Assumption here is that SSH uses port 22 for communication, the default.
sudo ufw allow 22
6. Restart Mongo daemon (mongod)
sudo systemctl restart mongod
7. Connect to remote Mongo server using Mongo shell
You can now connect to the remote MongoDB server using the following command.
mongo -u <user-name> -p <user-password> <remote-server-IP-address>:<mongo-server-port>
You can also connect to the remote MongoDB server with authentication:
mongo -u <user-name> -p <user-password> <remote-server-IP-address>:<mongo-server-port> --authenticationDatabase <auth-db-name>
You can also connect to a specific remote MongoDB database with authentication:
mongo -u <user-name> -p <user-password> <remote-server-IP-address>:<mongo-server-port>/<db-name> --authenticationDatabase <auth-db-name>
At this moment you can read and write within the some_db database from your local computer without ssh.
Important: Put into consideration the standard security measures for any database. Local security practices should guide what to do at any of the above steps.

Replication failed to connect to server [IP:27017] on first connect [MongoError: getaddrinfo ENOTFOUND

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

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

Unable to connect to PostgreSQL server: could not connect to server: Permission denied

Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Permission denied Is the server running on host "10.0.1.201" and accepting TCP/IP connections on port 5432?
This is the error i am getting when trying to connect to remote database from linux based server
Though i am able to connect to it from localhost
Can anyone help me in this
One possible scenario/solution that worked for me (for the very same problem) is here:
service httpd stop
service postgresql stop
setsebool -P httpd_can_network_connect 1
service httpd start
service postgresql start
Here we're basically allowing HTTPD to connect to PostgreSQL over network by setting SELinux bool equals to 1 (true).
Check the listen_addresses setting in postgresql.conf. If it is set to localhost, then only loopback connections will be accepted, and remote connections will get a "connection refused" error. Set listen_addresses to "*" to enable listening on all interfaces.
In PostgreSQL you have to configure client authentication in pg_hba.conf on the remote server.
Read more about pg_hba.conf # http://developer.postgresql.org/pgdocs/postgres/auth-pg-hba-conf.html , otherwise you'll never connect to that server :).
Hope it will help,
Stefan

Resources