How to fix MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster in MongoDB
Your application cannot reach the MongoDB Atlas cluster. This is usually caused by IP whitelisting restrictions, authentication issues, DNS problems, or network connectivity issues. Check your IP access list and connection string credentials first.
Quick fix
Fixes MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster
- Check your MongoDB Atlas IP Access List
- Verify your connection string and credentials
- Test DNS resolution
- Check cluster status and provisioning
- Verify port and firewall access (Cloud Deployments)
# MongooseServerSelectionError: Could not connect to any servers in your MongoDB AMongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster
What this error means
The MongooseServerSelectionError occurs when your Node.js/Mongoose application fails to establish a connection to any node in your MongoDB Atlas cluster. This error indicates a complete connection failure—the driver cannot discover or connect to any replica set members. The most common cause is that your current IP address is not whitelisted in MongoDB Atlas's IP Access List, which restricts which external addresses can connect to your cluster. Other causes include incorrect authentication credentials, DNS resolution failures, firewall blocks, or network routing issues in cloud environments.
Typical symptoms
- Application crashes or hangs when trying to query the database
- Connection timeout errors in logs
- Mongoose model operations fail silently or throw ServerSelectionError
- Application works locally but fails in production or cloud deployment
- Error appears immediately when trying to connect, or after a timeout period
Common causes
- Your current IP address is not in the MongoDB Atlas IP Access List
- Connection string contains incorrect username or password
- DNS resolution fails for the MongoDB Atlas cluster hostname
- Firewall or network security group blocks outbound connections on port 27017
- MongoDB Atlas cluster is paused or in a provisioning state
- Application is in a cloud environment (AWS, Heroku, etc.) with dynamic or undeclared IP addresses
- Incorrect authSource database specified in connection string
How to fix "MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster"
Log in to MongoDB Atlas and navigate to Security > Network Access. You should see your current IP address in the whitelist. If not, click "Add IP Address" and select "Add Current IP Address" to automatically detect and add your IP. If you are deploying to a cloud service with dynamic IPs, add 0.0.0.0/0 to allow connections from anywhere (less secure but necessary for development).
In MongoDB Atlas, go to Clusters > Connect > Drivers. Copy the connection string provided. Ensure your .env file has the correct CONNECTION_STRING with your database user's username and password URL-encoded. Common issue: special characters in passwords must be URL-encoded (e.g., @ becomes %40).
Example correct format:
mongodb+srv://username:password@cluster.mongodb.net/database?retryWrites=true&w=majority
Open a terminal and run:
nslookup your-cluster.mongodb.net
If this fails or returns no results, your DNS cannot resolve MongoDB's servers. Try using Google's DNS (8.8.8.8) or Cloudflare's DNS (1.1.1.1) instead of your ISP's default DNS. On Linux/Mac, edit /etc/resolv.conf; on Windows, use Control Panel > Network > DNS Settings.
Go to MongoDB Atlas > Clusters and verify your cluster shows a green status and is not paused. If it's paused, click "Resume". If the "Connect" button is disabled, the cluster is still provisioning—wait 5-10 minutes and refresh.
If deploying to AWS ECS, Heroku, Railway, or similar, ensure your outbound firewall/security group allows traffic on port 27017. In AWS, check that your security group has an outbound rule allowing all traffic (or specifically port 27017) to 0.0.0.0/0. For Heroku/Railway, these typically allow outbound by default.
In your Mongoose connection options, add these parameters to handle transient connection issues:
const mongoUri = process.env.MONGODB_CONNECTION_STRING;
await mongoose.connect(mongoUri, {
maxPoolSize: 10,
minPoolSize: 2,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
});
These settings give the driver more time and resources to establish connections.
Ensure you are using a recent version of Mongoose and the underlying MongoDB Node driver. Run:
npm update mongoose
Older versions may have connection stability issues or missing features.
Advanced notes
For applications behind a NAT Gateway (AWS ECS/Fargate in private subnets), the NAT Gateway must be in a public subnet with a route to the Internet Gateway. Verify the route tables for private subnets include a route to the NAT Gateway (0.0.0.0/0). Consider using MongoDB Atlas VPC peering or AWS PrivateLink for production deployments to avoid traversing the public internet.
If you are using MongoDB Atlas with a free tier (M0 cluster), connection limits are lower (500 max connections). If you exceed this, you may see ServerSelectionError. Upgrade to M10 or higher, or implement connection pooling on the application side.
For advanced debugging, enable Mongoose debug logging:
mongoose.set("debug", true);
This will log all MongoDB driver operations and reveal exact timeout points.
Frequently asked questions
- What does "MongooseServerSelectionError: Could not connect to any servers in your" mean?
- js/Mongoose application fails to establish a connection to any node in your MongoDB Atlas cluster. This error indicates a complete connection failure—the driver cannot discover or connect to any replica set members. The most common cause is that your current IP address is not whitelisted in MongoDB Atlas's IP Access List, which restricts which external addresses can connect to your cluster.
- How do I fix "MongooseServerSelectionError: Could not connect to any servers in your"?
- Your application cannot reach the MongoDB Atlas cluster. This is usually caused by IP whitelisting restrictions, authentication issues, DNS problems, or network connectivity issues. Check your IP access list and connection string credentials first.
- What causes "MongooseServerSelectionError: Could not connect to any servers in your"?
- Your current IP address is not in the MongoDB Atlas IP Access List Connection string contains incorrect username or password DNS resolution fails for the MongoDB Atlas cluster hostname
- Is the "MongooseServerSelectionError: Could not connect to any servers in your" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- 📚MongoDB Official Documentation
- 📖MongoDB Atlas - Troubleshoot Connection Issues(Official Docs)
- 📖MongoDB Atlas - Configure IP Access List(Official Docs)
- 🐙Mongoose GitHub - Issue #13988(GitHub)