Cant connect mongodb with node.js due to MongoServerSelectionError

Hello @Riyad_Sm and welcome to the MongoDB community forums. :wave:

Can you use mongosh from that machine to connect to your Atlas cluster? Have you temporarily added 0.0.0.0/0 to the allow list to see if your Node app could connect? Are you behind a firewall or proxy that is not allowing your connection to go out to the MongoDB cluster?

2 Likes

I am a beginner . I never used Mongoose.

As I said I am a beginner I just did what ever was in the documentation and I am confident I did not commit any error in the code.(In picture I have given My code)
How can I find out if I am behind a proxy or firewall?
-Thanks

I forgot to tag you @Doug_Duncan

If you are running the code from your house you probably don’t have a firewall or proxy in place, especially if you’re not sure. If you’re running code from a work location however, you might be going through a proxy. You would have to ask your network team.

Note that I am able to connect to that cluster (I can’t do anything as I don’t have the credentials), so it appears that you do indeed have the server open to the world for anyone to access.

I would recommend restricting access only to the IP addresses that should be able to access this data. This is extremely important if you plan on having sensitive data stored in the cluster. I would recommend this even with authentication enabled.

As for your code, I’m not a Node developer (I’m an admin/ops guy) so I’m not sure if your code is valid or not.

@Doug_Duncan Yes. I am using from home. My code is correct as far as I know. But still I am getting the error.!!

I am not sure why you’re getting a time out error. As you can see I was able to connect to the server so the server is accessible from anywhere. I just copied your code and was able to connect to the server without issue, so the code works as well.

If you are getting a time out error from running the code then something on your end is blocking the connection from your machine to your atlas server.

What happens if you run mongosh mongodb+srv://cluster0.hd6phlm.mongodb.net? Do you get a connection or do you get a timeout?

I have no idea about mongosh. can you be more specific like what should I change in my code to connect? I would be grateful !

@Doug_Duncan I used mongoose to connect, now I am getting this error:
error : 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/
please help!

mongosh is a mongo shell
A command line tool to connect to your mongodb
Before you connect with your app/mongoose check if you can connect by shell
You don’t have add anything in your code
From command prompt
Just run mongosh “your connect string”
Are you using any VPN,anti virus or other softwares which may be blocking your connection or it could be firewall/proxy issues
Did you try from another network or mobile hotspot just to check if it works or not on a different network

@Ramachandra_Tummala I tried with mongoose with proper documentation. Nothing happened . And also I tried with my mobile network as well.still the same result.
How can I know that if is a proxy or firewall issue.? I did already try turning of my firewall though. How can I check if it is for proxy issue? Is there anyway to check that?

What is the result of above?
Even i can connect to your cluster like Doug_Duncan was able to connect
You have to contact your network admin if any firewall/proxy causing blocking

I Don’t have any network admin. I am using an local ISP here. What specific question should I ask to them about proxy? They Don’t seem to know the answer though. And Where exactly should I put that part in my code?

Do you have mongosh installed on your system?
Just run mongosh from your terminal command prompt and show the output
If shell is available all you have to do is run below
#mongosh mongodb+srv://cluster0.hd6phlm.mongodb.net

Also check this link

I checked the link. It says no proxy was used.
And also I Don’t have mongosh installed and when i searched about that I found no article Where they show how to connect with mongosh with a node.js app. All I found was mongoose Which I already tried.

@Doug_Duncan @Ramachandra_Tummala
I tried with mongosh still got the error. I am sending you the screenshot

As both I and Ramachandra can connect to your Atlas instance, there is nothing from the MongoDB Atlas side that is blocking your connection. This would mean that there is something on your side that is blocking the connections. If you have disabled the firewall and you’re still not able to connect, you might be going through a VPN that is causing issues with you making a connection.

Hi :wave: @Riyad_Sm,

Could you try the solution mentioned in the given thread :point_down:

https://www.mongodb.com/community/forums/t/error-in-connect-to-atlas/180790/7?u=kushagra_kesav

I hope it helps!

Thanks,
Kushagra

@Kushagra_Kesav @Ramachandra_Tummala @Doug_Duncan
Thanks for helping me my brothers.
I solved the problem. Basically created another mongodb account with another gmail. And it worked! Maybe something was wrong with my previous gmail.
Peace!

11 posts were split to a new topic: ECONNREFUSED - 127.0.0.1 to localhost