u/dmx2101

MongoDB Atlas connection timeout in Node.js Express despite IP whitelisting and DNS fixes

I am facing a MongoDB Atlas connection timeout issue in my Node.js + Express application.

Environment

- Node.js v24 (also tested with v22)

- Mongoose v9+ (also tested with v8.6)

- MongoDB Atlas

- VPS server (IPv4)

- Express.js backend

Problem

My application is unable to connect to MongoDB Atlas and always throws a timeout/server selection error.

Example error:

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster

What I already tried

  1. IP Whitelisting

- Added my VPS public IP in MongoDB Atlas Network Access

- Also tried:

0.0.0.0/0

(still same issue)

  1. Different Connection Strings

Tried both:

- "mongodb+srv://"

- Standard connection string from Atlas

Still getting timeout issue.

  1. DNS Changes

Added public DNS servers:

- "8.8.8.8"

- "1.1.1.1"

Also tried:

require('dns').setDefaultResultOrder('ipv4first');

No change.

  1. Version Downgrade

Downgraded:

- Node.js 24 → 22

- Mongoose 9 → 8.6

Issue still persists.

  1. Network Testing

When testing connectivity from the VPS, the MongoDB domain connection also times out.

Question

What else should I check?

Could this be:

- VPS firewall issue?

- ISP/VPS provider blocking MongoDB Atlas ports?

- DNS/SRV resolution problem?

- MongoDB Atlas networking issue?

Has anyone faced a similar issue with MongoDB Atlas on a VPS?

Any debugging steps or fixes would help.

reddit.com
u/dmx2101 — 6 days ago