All guidesMore providers: databases, auth, hosting, email and payments
On this page

MongoDB Atlas error

VercelMongoDB

MongoServerSelectionError: Server selection timed out on Vercel with Atlas

By Ohad Krispin, founder of VibeRaven · LinkedIn

"MongoServerSelectionError: Server selection timed out after 30000 ms" (or "connection timed out") means the driver found no reachable server in your Atlas cluster before serverSelectionTimeoutMS ran out. MongoDB lists four causes: network or firewall blocks, a client IP missing from the Atlas IP access list, DNS SRV failures and TLS problems. On Vercel the IP access list is the usual one, because MongoDB says Vercel deployments use dynamic IP addresses and the access list must allow all addresses (0.0.0.0/0) for them to connect. Add that entry, or on a Pro or Enterprise plan turn on Vercel Static IPs and allow its IP pair for each region instead. Then check the cluster is Active and MONGODB_URI is right. Reproduced with mongodb 7.8.0 on 2026-10-31.

What the driver prints, and why it differs between runs

The driver builds the message in src/sdam/topology.ts as "Server selection timed out after N ms", where N is serverSelectionTimeoutMS. In a run on 2026-10-31 against a local server that accepts the TCP connection and never answers, mongodb 7.8.0 printed that message when serverSelectionTimeoutMS ended first, and "connection <monitor> to <host>:<port> timed out" when connectTimeoutMS ended first. A refused connection prints "connect ECONNREFUSED" instead. MongoDB's troubleshooting page lists "MongoServerSelectionError: connection timed out" and "getaddrinfo ENOTFOUND" as the other forms.

With Mongoose the same wait shows up first as a different error, because queries are buffered: MongooseError: Operation users.find() buffering timed out after 10000ms.

MongoServerSelectionError: Server selection timed out after 30000 ms
MongoServerSelectionError: connection <monitor> to <host>:<port> timed out
MongoServerSelectionError: getaddrinfo ENOTFOUND <host>

Do I have to allow 0.0.0.0/0 for Vercel?

Vercel says that by default it routes outbound requests from builds and functions through a dynamic range of IP addresses, so a service cannot allowlist one address. MongoDB's Vercel page says the same and that the integration needs 0.0.0.0/0 in the project's IP access list; if the entry is missing, Atlas adds it during the integration workflow. Adding any /0 entry makes Atlas email the users who hold a role on the project. MongoDB's troubleshooting page offers 0.0.0.0/0 as a temporary test, to remove once you know the cause.

The other route is Vercel Static IPs, listed for Pro and Enterprise plans. You enable it per region, each region has its own IP pair, and you put that pair in the Atlas access list. Vercel lists $100/month per project on Pro plus Private Data Transfer, and Secure Compute is an Enterprise-only option with a dedicated network. Static IPs apply to functions and, if you switch it on, to builds. They do not apply to Routing Middleware.

How do I tell the IP access list from DNS or a firewall?

MongoDB's steps: confirm the cluster is Active, then confirm the client's public IP is in Network Access. For a cluster reached with a mongodb+srv:// string, check that the SRV record resolves from the client host; if it does not, MongoDB suggests the mongodb:// string that lists every host. Atlas nodes listen on port 27017, and MongoDB advises testing TCP reachability to each node rather than ping, since ICMP may be blocked.

nslookup -type=SRV _mongodb._tcp.<cluster-name>.mongodb.net
nc -zv <node-hostname> 27017

Checklist

How to fix it, then check the fix

  1. 01In Atlas, open the Clusters page and confirm the cluster shows as Active. MongoDB lists this as the first check.
  2. 02Open Network Access in the Atlas project. Check which entries exist. Vercel's outbound addresses are not in the list unless you added 0.0.0.0/0 or your Static IPs.
  3. 03To test, add 0.0.0.0/0, save, wait for the entry to apply, then call the route again. If it connects, the access list was the cause.
  4. 04Pick the permanent setting. Either keep 0.0.0.0/0, as the Atlas integration for Vercel does, or enable Static IPs on a Pro or Enterprise project, add each region's IP pair to Atlas, confirm the function connects, then delete 0.0.0.0/0.
  5. 05
    If it still times out, compare MONGODB_URI in Vercel Production with the string that works locally, and check the SRV record resolves:
    nslookup -type=SRV _mongodb._tcp.<cluster-name>.mongodb.net
  6. 06Redeploy after changing any variable, because Vercel applies changed variables only to new deployments. The fix holds when the route answers and Atlas metrics show open connections.

Related

Sources

Official docs behind this checklist

FAQ

FAQ for builders before launch

Why does it work on my laptop but not on Vercel?

Atlas allows client connections only from entries in the project's IP access list. If it connects from your laptop, the list allows your laptop's address or all addresses. Vercel's outbound addresses are different ones.

Can I add Vercel's IP address to the access list?

Not as one address by default. Vercel documents a dynamic range of outbound addresses. Static IPs on Pro or Enterprise gives you a fixed pair per region that you can add.

Does Static IPs cover my Next.js middleware?

No. Vercel says Static IPs do not apply to Routing Middleware, which runs at the edge. They apply to Vercel Functions, and to builds if you turn that setting on.