pymongo.errors.ServerSelectionTimeoutError MongoDB Origin: Data Last updated 5 Nov 2026

PyMongo: pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s

PyMongo couldn't find a usable MongoDB server within the selection timeout.

Exact message you may see pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refusedpymongo.errors.ServerSelectionTimeoutError: No servers found yetServerSelectionTimeoutError: SSL handshake failedpymongo.errors.ConfigurationError: The "dnspython" module must be installed to use mongodb+srv:// URIspymongo ServerSelectionTimeoutError Timeout: 30s
Seen on: MongoDB Python

Meaning

Server down, wrong host/port, firewall/Atlas IP list, DNS/SRV failure, or TLS problems. The message lists each host with its error. pip install "pymongo[srv]" is needed for mongodb+srv:// on older versions (dnspython). Lower serverSelectionTimeoutMS while debugging.

Common causes

  • mongod not running or wrong host
  • Atlas IP not allowed
  • Missing dnspython for SRV URIs
  • TLS/CA problems (certifi)
  • Docker networking

⚡ Quick fix

  1. Check the host/port with nc -vz
  2. Install pymongo[srv] and certifi
  3. Whitelist your IP in Atlas

Detailed fix by platform

Python

  1. MongoClient(uri, serverSelectionTimeoutMS=5000).admin.command("ping")

How to diagnose

  1. Error — Per-host reason?
  2. SRV — dnspython present?
  3. Allowlist — IP added?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Data. Errors that are often confused with or caused by this one (click to open):

BSONObj size is invalid / exceeds maximum document size (16MB) — MongoServerError: BSONObj size: 17825792 (0x1100000) is invalid. Size must be between 0 and 16793600(16MB) (MongoDB)BSONObj size is inval…MongoDB Atlas: connection timed out — IP not whitelisted — MongoServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted (MongoDB)MongoDB Atlas: connec…MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017 — MongoDB: MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017 (MongoDB)MongoNetworkError: co…MongoParseError — MongoDB: MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://" (MongoDB)MongoParseErrorNotImplementedError: Collection objects do not implement truth value testing — PyMongo: NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not None (MongoDB)NotImplementedError: …pymongo.errors.BulkWriteError: batch op errors occurred — PyMongo: pymongo.errors.BulkWriteError: batch op errors occurred (MongoDB)pymongo.errors.BulkWr…querySrv ENOTFOUND _mongodb._tcp — MongoDB Atlas: Error: querySrv ENOTFOUND _mongodb._tcp.cluster0.abcde.mongodb.net (MongoDB)querySrv ENOTFOUND _m…Sort/Group exceeded memory limit of 100 MB (allowDiskUse) — MongoServerError: Exceeded memory limit for $group, but didn't allow external sort. Pass allowDiskUse:true to opt in (MongoDB)Sort/Group exceeded m…pymongo.e…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.