Your Node.js app works locally but throws 'MongoNetworkError: connection timed out' when deployed to production. MongoDB connection strings work in MongoDB Compass but fail in the app.

This is usually caused by IP whitelist restrictions on MongoDB Atlas. Your production server's IP isn't whitelisted, or you're behind a dynamic IP that changed.

Step-by-Step Guide

1

Log into MongoDB Atlas dashboard and go to Network Access

2

Check if your production server IP is whitelisted (or add 0.0.0.0/0 for testing)

3

Verify your connection string includes correct username/password and cluster URL

4

Check if your connection string has +srv (for DNS seedlist)

5

Add connection options: useNewUrlParser: true, useUnifiedTopology: true

6

For Heroku/Vercel: whitelist 0.0.0.0/0 or use their IP range

7

Test connection from production server using mongo shell or Compass

Found an issue with this solution?

Related Topics

database authentication failedconnection pool exhaustedreplica set connection