MongoDB Atlas Connection Troubleshooting Guide: Every Error and Fix for Node.js
Complete guide to fixing MongoDB Atlas connection errors in Node.js. Covers querySRV ECONNREFUSED, ETIMEOUT, authentication failed, IP whitelist issues, DNS resolution, connection string formats, Mongoose vs native driver, serverless and Docker fixes. Updated 2026.
On this page
MongoDB Atlas connection errors are some of the most frustrating backend issues because the error messages are often vague and the real cause could be your network, DNS, IP whitelist, connection string, driver version, or Atlas configuration. I have deployed multiple production Node.js apps to Atlas and hit every one of these errors at different stages: local development, Docker containers, serverless functions, and cloud VMs. This guide covers every Atlas connection error I have encountered, organized by error message so you can find your exact issue and fix it.
Quick Fix: If You Just Want It to Work
Before diving into specific errors, try these three steps that fix 80 percent of Atlas connection issues.
Error 1: querySRV ECONNREFUSED
This is the most common Atlas error. The `mongodb+srv://` connection string uses DNS SRV records to discover your cluster nodes. When your DNS cannot resolve the SRV record, you get this error. It does not mean Atlas is down. It means your machine or environment cannot perform the DNS lookup.
Causes and Fixes for querySRV ECONNREFUSED
- Your ISP or corporate network blocks DNS SRV record lookups
- You are behind a firewall or proxy that intercepts DNS queries
- Your Docker container or serverless function uses a DNS resolver that does not support SRV records
- Your Node.js version has a DNS resolution bug
- The Atlas cluster hostname is wrong or the cluster was deleted
Fix 1a: Override DNS Servers in Node.js
Force Node.js to use Google or Cloudflare DNS instead of your system DNS resolver. This bypasses ISP DNS issues, corporate firewalls, and Docker DNS problems.
Fix 1b: Use Standard Connection String Instead of SRV
If DNS SRV resolution is completely blocked in your environment, switch from `mongodb+srv://` to the standard `mongodb://` connection string. This bypasses SRV lookups entirely by connecting directly to the cluster nodes.
To find your standard connection string: go to Atlas Dashboard, click Connect on your cluster, select Connect your application, then click the dropdown and switch from SRV to Standard connection string format.
Fix 1c: Verify DNS Resolution Manually
Error 2: ETIMEOUT and Connection Timeout
Timeout errors mean DNS resolved correctly but the TCP connection to the Atlas cluster could not be established. The most common cause is IP whitelist: Atlas blocks connections from IP addresses that are not in your Network Access list.
Fix 2a: Add Your IP to Atlas Network Access
- Go to Atlas Dashboard then Network Access then IP Access List
- Click Add IP Address
- Click Add Current IP Address to whitelist your machine
- For development: you can add 0.0.0.0/0 to allow all IPs temporarily
- For production: add your server or cloud provider static IP
- Wait 1 to 2 minutes for the change to propagate before retrying
Important: if your IP changes (home internet, mobile hotspot, VPN), you need to update the whitelist every time. For dynamic IPs during development, 0.0.0.0/0 is the simplest fix but should not be used in production.
Fix 2b: Increase Connection Timeout
Fix 2c: Check Firewall and VPN
Error 3: Authentication Failed
Authentication errors mean the TCP connection to Atlas succeeded but your username or password was rejected. This is almost always a credentials issue, not a network issue.
Causes and Fixes for Authentication Failed
- Wrong username: Atlas database users are separate from Atlas account users. Go to Database Access in Atlas to check your database username.
- Wrong password: Reset the password in Atlas Dashboard then Database Access then Edit then Update Password.
- Special characters in password: If your password contains @, #, %, /, or ? these must be URL encoded in the connection string. Use encodeURIComponent() in JavaScript.
- Wrong authSource: The default authSource is admin. If you changed it, add ?authSource=yourdb to the connection string.
- User has no permissions: Check that the database user has readWriteAnyDatabase or the appropriate role in Database Access.
Error 4: Connection String Format Issues
Correct Connection String Formats
Error 5: MongooseServerSelectionError in Docker
Docker containers use their own DNS resolver which often does not support SRV record lookups. This causes the same querySRV ECONNREFUSED error. Docker also has its own network stack, so IP whitelist issues can appear even if your host machine connects fine.
Error 6: Connection Issues in Serverless (Vercel, AWS Lambda, Netlify)
Serverless functions have two unique problems: they have dynamic IPs that cannot be whitelisted individually, and they have short execution timeouts. Every cold start creates a new MongoDB connection which takes 2 to 5 seconds, often exceeding the function timeout.
This pattern caches the connection in a global variable so subsequent invocations on the same container reuse the connection instead of creating a new one on every request.
Serverless IP Whitelist Fix
- For development and small projects: add 0.0.0.0/0 to allow all IPs
- For production: use Atlas Network Peering with your cloud provider VPC
- Vercel: use Vercel Secure Compute with a static IP (paid feature)
- AWS Lambda: deploy in a VPC with a NAT Gateway that has a static IP, whitelist that IP
- For maximum security: use Atlas Private Endpoints with AWS PrivateLink or Azure Private Link
Error 7: Mongoose Deprecated Options Warning
If you upgraded from Mongoose 5 to Mongoose 6 or above, several options were removed. These options were necessary in Mongoose 5 but are now the default behavior and passing them causes errors.
Error 8: SSL and TLS Errors
Atlas requires TLS/SSL for all connections. These errors usually appear in environments with outdated CA certificates, corporate proxy SSL inspection, or misconfigured Node.js TLS settings.
Error 9: Connection Works Locally But Fails in Production
When your Atlas connection works on your local machine but fails on your deployed server, the issue is almost always one of these four things.
- IP whitelist: Your server IP is not in the Atlas Network Access list. Add the server static IP or use 0.0.0.0/0 to test.
- Environment variables: The MONGODB_URI is not set or has different formatting in your production environment. Check for trailing spaces, newlines, or missing quotes.
- DNS: Your server DNS resolver cannot resolve the SRV record. Use dns.setServers() or switch to standard connection string.
- Firewall: Your cloud provider or server firewall blocks outbound connections on port 27017. Check security group rules.
Mongoose vs Native Driver Connection
Both Mongoose and the native MongoDB Node.js driver use the same underlying connection logic, but the setup code is different. Here is the correct pattern for each.
Quick Diagnosis Checklist
- querySRV ECONNREFUSED: DNS cannot resolve SRV record. Use dns.setServers() or switch to standard connection string.
- ETIMEOUT: IP not whitelisted or port 27017 blocked. Check Atlas Network Access and firewall rules.
- Authentication failed: Wrong username, password, or special characters not URL encoded. Check Database Access in Atlas.
- Invalid connection string: Check format, remove deprecated options, check for trailing spaces in .env file.
- Docker: Set dns to 8.8.8.8 in docker-compose.yml or use standard connection string.
- Serverless: Cache the connection in a global variable. Whitelist 0.0.0.0/0 or use VPC peering.
- SSL errors: Update CA certificates or set NODE_EXTRA_CA_CERTS in production.
- Works locally fails remotely: Check IP whitelist, environment variables, DNS resolution, and firewall rules.
- Deprecated options: Remove useNewUrlParser, useUnifiedTopology, useCreateIndex, useFindAndModify.
- General debug: Try connecting with mongosh from the same machine to isolate driver vs network issues.
Testing Connection With mongosh
Official-Style Error Explanation
MongoDB Atlas connection errors in Node.js fall into four categories: DNS resolution failures (querySRV ECONNREFUSED, ENOTFOUND) where the SRV record cannot be resolved, network connectivity failures (ETIMEOUT, connection timed out) where the IP is not whitelisted or port 27017 is blocked, authentication failures (bad auth, authentication failed) where credentials are wrong or passwords have unencoded special characters, and configuration failures (invalid connection string, deprecated options) where the connection string format or driver options are incorrect. The diagnostic approach is: first test DNS resolution with nslookup or dig, then test TCP connectivity with nc or telnet on port 27017, then test credentials by connecting with mongosh, then check your Node.js code and driver options. For Docker and serverless environments, DNS and IP whitelisting are the most common failure points because containers use internal DNS resolvers and serverless functions have dynamic IPs.
FAQ
What does querySRV ECONNREFUSED mean?
It means your DNS resolver cannot look up the SRV record for your Atlas cluster. The mongodb+srv connection string uses DNS SRV records to discover cluster nodes. When the DNS lookup fails, the driver cannot find the cluster IP addresses. The fix is to override DNS servers in Node.js with dns.setServers(['8.8.8.8', '8.8.4.4']) or switch to the standard mongodb:// connection string that bypasses SRV lookups.
Why does my Atlas connection time out?
Connection timeouts mean DNS resolved but the TCP connection could not be established. The most common cause is that your IP address is not in the Atlas Network Access whitelist. Go to Atlas Dashboard, Network Access, and add your IP. For development with changing IPs, add 0.0.0.0/0 to allow all IPs. Also check that your firewall allows outbound TCP connections on port 27017.
How do I connect to Atlas from Docker?
Docker containers use their own DNS resolver that often does not support SRV record lookups. Add dns settings to your docker-compose.yml with 8.8.8.8 and 8.8.4.4, or use dns.setServers in your Node.js code, or switch from mongodb+srv:// to the standard mongodb:// connection string. Also make sure the Docker host IP is whitelisted in Atlas Network Access.
How do I connect to Atlas from Vercel or AWS Lambda?
Serverless functions have dynamic IPs, so you either need to whitelist 0.0.0.0/0 in Atlas Network Access (acceptable for small projects), use VPC peering with a static IP NAT Gateway, or use Atlas Private Endpoints. For performance, cache the MongoDB connection in a global variable so cold starts do not create a new connection every time.
Should I use Mongoose or the native MongoDB driver?
Mongoose adds schemas, validation, middleware, and an easier query API on top of the native driver. Use Mongoose for most applications, especially if you want schema validation and model-based architecture. Use the native driver when you need maximum performance, want to work with raw MongoDB queries, or are building a lightweight microservice that does not need schema enforcement.
Why does Atlas authentication fail even though my password is correct?
The most common cause is special characters in the password that are not URL encoded. Characters like @, #, %, /, and ? have special meaning in URIs and must be encoded with encodeURIComponent() in JavaScript. Also verify you are using the database user credentials from Atlas Database Access, not your Atlas account login. Reset the password in Atlas if unsure.
Do I still need useNewUrlParser and useUnifiedTopology?
No. These options were required in Mongoose 5 and the MongoDB driver 3.x but are removed in Mongoose 6 and above and driver 4 and above. Passing them now causes errors or warnings. Just call mongoose.connect(uri) with no options object or only options like maxPoolSize that are still supported.
Shahmeer Rizwan
Full-Stack Developer