The app can't connect to MongoDB Atlas — "connection timed out" or "server selection timeout". What do you check?
IntermediateTroubleshootingMongoDB
Answer
Short answer
Atlas blocks everything by default. Check the IP access list (is the app's egress/NAT IP allowed?), private networking (peering/Private Endpoint routes and DNS), outbound firewall rules for port 27017, and the connection string (mongodb+srv needs working DNS SRV/TXT lookups).
Steps
- Which IP does the app leave from? From the pod/server:
curl -s ifconfig.me→ add that (or the NAT gateway's Elastic IPs) to the Atlas access list. - DNS for SRV:
dig SRV _mongodb._tcp.cluster0.abcd.mongodb.net— some corporate DNS or minimal images break SRV lookups. - Port reachability:
nc -zv cluster0-shard-00-00.abcd.mongodb.net 27017. - Private Endpoint: is the app in the right VPC/VNet, and is the private DNS zone linked? Use the private connection string.
- Credentials and authSource: an
Authentication failederror means the network is fine. - TLS: Atlas requires TLS — old drivers or missing CA certificates in slim images cause handshake errors.
"Server selection timeout" in particular
The driver couldn't find a suitable member within ~30 seconds — almost always network access, DNS or TLS, rarely MongoDB itself.
What to say in the interview
"Atlas denies by default, so I verify the app's egress IP is in the access list or that Private Endpoint routing and DNS are correct, check SRV DNS resolution and port 27017 with nc, and then credentials, authSource and TLS support in the image."