Skip to content
Troubleshooting⏱️ 10 min read·

MongoDB Atlas “IP Not Whitelisted” / Connection Timeout: The Exact Fix for Nepali Dynamic IPs

Fix MongoServerSelectionError and connection timeouts caused by WorldLink, Vianet, and NTC dynamic CGNAT IP address changes in Nepal when connecting to MongoDB Atlas clusters.

MN
Manoj NeupaneSoftware Developer & Programming Instructor
#MongoDB#MongoDB Atlas#Network Access#MERN Stack#Nepal ISP#Debugging
🎯 Who This Tutorial Is For

📋 Prerequisites
  • ✓A MongoDB Atlas account and cluster
  • ✓A Node.js or Python application with a MongoDB connection string

🚀 What You Will Learn & Build

  • ▸Why home internet providers in Nepal break Atlas IP whitelists overnight
  • ▸How to configure Atlas Network Access safely for development
  • ▸DNS and SRV resolution issues on Nepali ISP routers and how to fix them
1

Why Your MongoDB Atlas Connection Breaks Every Morning

Most home broadband providers in Nepal (WorldLink, Vianet, Subisu, Nepal Telecom) use **Carrier-Grade NAT (CGNAT)** with dynamic public IP pooling. Every time your fiber router reboots or your ISP refreshes leases, your public IP changes. When you created your Atlas cluster and clicked *"Add Current IP Address"*, Atlas whitelisted your IP for that day only. The next morning, your router has a new IP, and your terminal outputs: ```bash MongoServerSelectionError: connection timed out at Topology.selectServer (/node_modules/mongodb/lib/sdam/topology.js:285:38) reason: TopologyDescription { type: 'ReplicaSetNoPrimary', servers: Map(3) { ... } } ```
2

The 3 Solutions: Development vs Production

### Fix 1: For Local Development & College Projects (0.0.0.0/0) In MongoDB Atlas: 1. Go to **Security > Network Access** in the left sidebar. 2. Click **Add IP Address**. 3. Click **Allow Access from Anywhere** (`0.0.0.0/0`). 4. Set a comment like *'Development - Dynamic Nepal IP'* and click **Confirm**. *Note: This allows connections from any IP, but your database is STILL protected by your database user username and strong password. Ensure your password is at least 20 random characters.* ### Fix 2: Resolving DNS/SRV Lookup Failures on Nepali Routers Some local router DNS servers fail to resolve MongoDB's `mongodb+srv://` URI format. If you still get timeout errors after whitelisting: 1. Change your laptop's DNS servers to **Cloudflare (1.1.1.1)** or **Google (8.8.8.8)**. 2. Alternatively, in your Atlas dashboard, click **Connect > Drivers**, uncheck *"Use the latest driver format"*, and select the standard non-SRV connection string.

✨ Expected Output & Verification

Instant, uninterrupted MongoDB Mongoose connections that survive router reboots and ISP IP reassignments.

🧪 How to Test the Implementation

  1. 1Verify connection using mongosh 'mongodb+srv://user:pass@cluster.mongodb.net/dbname'
  2. 2Restart your Express server and check for 'Connected to MongoDB Atlas' in terminal

⚠️ Common Errors & How to Fix Them

❌Special characters in MongoDB password causing URI parsing errors

Root Cause: Using @, :, or / in your database password without URL-encoding.

Fix: URL-encode special characters (e.g., @ becomes %40) or use an alphanumeric password.

🛡️ Security Considerations & Best Practices

  • !Never leave 0.0.0.0/0 enabled in production environments. Production VPS IP addresses should be explicitly whitelisted.

❓ Frequently Asked Questions (FAQ)

Yes, provided you do not store sensitive production data and use a complex random password for your database user.

📌 Summary of Key Takeaways

Dynamic CGNAT in Nepal breaks single-IP whitelists. Configure 0.0.0.0/0 with a strong password for development, and switch to 1.1.1.1 DNS if SRV records fail.

Official References & Further Reading