DEV Community

Cover image for Remember to whitelist your IP when you can't connect to Mongo DB 📡💡
Arika O
Arika O

Posted on

Remember to whitelist your IP when you can't connect to Mongo DB 📡💡

I recently started looking into backend using the MERN stack, and sometimes it happens that I can't connect to a Mongo cluster, even though it worked in the past. In 99% of the cases this is because my IP adress changed and my current IP is not whitelisted*. This is nothing that can't be fixed but I never remember what causes it and I waste time until I get to the correct solution (this is pretty much like forgetting how to center a div). When I would try to connect to a cluster I would see someting like this in the terminal (in the past I think I've also seen some straight up errors, but this is the most recent message I got):

[nodemon] 2.0.16
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
connection <monitor> to 52.58.6.203:27017 closed
[nodemon] clean exit - waiting for changes before restart
Enter fullscreen mode Exit fullscreen mode

The connection couldn't be established because my IP address changed since I initially set up the cluster (or the last time I whitelisted my IP address). So what I would need to do is go to MongoDB Atlas and on the left side of the page, chose Security and then Network Access.

Image description

I would press ADD IP ADDRESS, wait for your current IP to be whitelisted and then try to reconnect. Now everything should work again. MongoDB is pretty good at auto detecting your current IP but if the connection still can't be established, try a quick "what's my ip" on Google and compare if the IP you got back is the same as the one detected by Mongo. If it's not, you might want to add it manually again.

*MongoDB Atlas only allows connections to clusters from IP addresses that are matched by entries in our project’s IP whitelist

Top comments (1)

Collapse
 
sitonimbus profile image
Carlos Andrés Mora González

đź‘Ťđź‘Ťđź‘Ťđź‘Źđź‘Źđź‘Ź