MongoServerSelectionError: connection <monitor> to <MyIP> closed - mongodb

I have started learning MongoDB, I am just trying to connect to the database using MongoDB Shell.
I am using the below command.
mongosh "mongodb+srv://cluster0.12345.mongodb.net/myFirstDatabase" --username viveknuna
I have added my current IP Address to IP Access List. But getting this error.
MongoServerSelectionError: connection to closed
I have referred to this question and added 0.0.0.0/0 (Allow access from AnyWhere) in the IP Access List. and it works as expected. But this is a security risk, Is there any way without allowing all IPs.
FYI: I am inside Virtual Machine and running this command also from VM.

in my case when i got these error MongoServerSelectionError: connection <monitor> to 16.163.195.9:27017 closed
I actualy put the wrong ip address i put my private ip; thats why.
i fixed it by changing the ip address in the IP List from WAN IP to my Public IP address thats how i fixed it, just search what is my ip, in you favorite seach Engine and it will show you your public ip in the result.

For me this error was occurring because my connection string was wrong.To be very specific - I copied the sample connection string from a course I was learning and just replaced the username and password with my credentials. So, the credentials were right but not the rest of the connection string.
Just for the sake of understanding. Please see below :
mongodb+srv://myusername:mypassword#courseproject.h1mpg.mongodb.net/?retryWrites=true&w=majority"
myusername and mypassword are correct i.e belongs to my cluster in MongoDB atlas account, but the rest of the string is wrong as I copied it from somewhere instead of copying it from my own MongoDB atlas account.
So please make sure to double check if your entire connection string is correct.

This happens when your ip address on the remote mongoDb is not the same as the public ip address of your computer. Simply go to network access on atlas and add your public ip address. Also, if your ip address is the same as that on the remote mongoDb then probably the connection closed after some period(default is 6hours).Thus when adding a your ip, unsure you toggle and set the duration for the connection (max is a week)

Related

How to get the IP address of Heroku so that my Heroku hosted backend server can connect to MongoDB?

I'm trying to host my backend on Heroku, but after I tried deploying I was getting an "application error" . I checked the "heroku logs" and I think the problem is my MongoDB database only accepts connection to my specific IP address.
2023-02-07T09:34:05.807213+00:00 app[web.1]: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
How do I get the IP address of Heroku so that I can add it to this whitelist?
https://i.imgur.com/K9k0TFk.png
This is the entire Heroku log just in case maybe I'm wrong about the reason it's crashing?
https://pastebin.com/btc79fQq
EDIT:
Also is there a way to connect to the mongo atlas database with username + password instead of IP address maybe?
It seems like it's very dynamic so you need an addon like Fixie socks which sets a static IP of your Heroku app for this exact purpose.

Connect to EC2 via MongoDB Compass Community

I have an EC2 instance (Ubuntu 14). I've defined port 27017 in its security group to be accessed from anywhere.
I'm trying to connect from my PC (Windows 10) to the EC2 instance, but getting the following error:
MongoDB not running on the provided host and port
which is not true since I've made sure that MongoDB is running.
These were the settings I specified:
I've also tried to use SSH (which is also defined in the sercurity group and works well through terminal), but got the following error:
Error creating SSH Tunnel: (SSH) Channel open failure: Connection
refused
These were the settings I specified:
Alon,
Checkpoints
See if there is a need to change the bind_ip variable at the /etc/mongodb.conf file.
By default, it is locked to localhost.
Try setting the value to 0.0.0.0 or assign the IP that will be able to connect the DB to it.
Port is allowed in security group attached to EC2 (You mentioned its done)
Not using private IP to connect (I guess, you are using the right one as you connected through terminal)
Rare case, OS firewall
I guess point 1 should do the trick. Rest points for future reference
[Update 1]
Doc link for bindIp
https://docs.mongodb.com/manual/reference/configuration-options/#net-options
I am putting this as an answer although it is meant as a comment to the answer by raevilman above but I do not have the necessary reputation to post comments yet.
If you set the bind_ip variable to 0.0.0.0 as mentioned, MongoDB accepts connections from all IP addresses. This might be a security risk if you do not use access control.
On the other hand, if I am not wrong, the idea of a SSH tunnel is for that, in this case MongoDB, the entering connection comes from localhost so no change in the configuration should be necessary.
That said, I came here because I could not connect either. I got the error 'Error creating SSH Tunnel: Timed out while waiting for forwardOut'.
In my case the solution was to put 'localhost' in the hostname field at the top instead of the host IP.

adding localhost to mongo atlas whitelist

I've been using the option allow everyone connection to my atlas cluster by selecting the "Allow Access from Anywhere" button in the group IP whitelist.
Is there a way to just add my localhost ip address instead. I tried doing it but I got the following error :
/me/node_modules/mongodb/lib/replset.js:364
process.nextTick(function() { throw err; })
^
MongoError: connection 4 to cluster0blbla.net:27017 closed
Anyone know of a way to add localhost connection without opening security up to everyone ?
Yes you can always add your local IP Address by going to:
Security - > IP Whitelist and then add a new IP. There you can click "ADD CURRENT IP ADDRESS" along with any comment:

Not able to connect MongoDB Compass

I am not able to connect my server in MongoDB Compass. I am trying to connect by identity file. I am providing hostname username and file.
I got:
'Error creating SSH Tunnel: Timed out while waiting for forwardOut'
What does it mean?
Hi, krl! I was getting the exact same error from MongoDB Compass Community 1.11.1 while trying to connect through an SSH tunnel, with a ppk file protected by a passphrase.
Are you putting your remote server IP or domain name in the first field (hostname)? That was my mistake.
What solved it for me was setting the Hostname field (the first one on top) to localhost instead of remote server IP or domain name. Put your domain/IP only on the field SSH Hostname.
I hope it helps.

MongoDB remote connection

I am trying to use MongoDB for remote connection. First I tried localhost and internal ip address to build the connection. Both of them succeeded. Then I tried external ip address to connect on my own computer of MAC as an experiment first. I have created an admin user for the database in MongoDB. The bind_ip option is also commented in mongodb.conf. The firewall should also have been turned down. But the connection cannot been built still. Does anyone know the reason?
Thank you very much!
To allow connecting remote clients (which is your case) to MongoDB Server.
Make sure to do set bind_ip to either of values, in your mongodb.conf file -
Following is with any IP
bind_ip = 0.0.0.0
Following for bind to specific IP
bind_ip = 10.52.36.99 (or any specific IP)