mongodb connection_error ai_generated true

MongoTimeoutError: Server selection timed out after 30000 ms

ID: mongodb/server-selection-timeout

Also available as: JSON · Markdown
82%Fix Rate
88%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
7 active

Root Cause

MongoDB driver cannot find a suitable server within timeout. Replica set not initialized, wrong connection string, or network issue.

generic

Workarounds

  1. 90% success Check replica set status
    mongosh --eval 'rs.status()'  # verify all members are healthy

    Sources: https://www.mongodb.com/docs/manual/

  2. 92% success Verify connection string matches replica set name
    mongodb://host1:27017,host2:27017/?replicaSet=myReplicaSet  # must match rs.initiate name
  3. 85% success Check network connectivity between app and all replica members
    All hosts in the connection string must be reachable from the application

Dead Ends

Common approaches that don't work:

  1. Increase serverSelectionTimeoutMS to very large value 72% fail

    Hides the real problem; application hangs for minutes before failing

  2. Use directConnection=true to bypass replica set 68% fail

    Bypasses read preference and failover; application breaks when the node goes down