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

**Code:** `pymongo.errors.ServerSelectionTimeoutError` · **Category:** MongoDB · **Seen on:** MongoDB, Python

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

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/mongodb/pymongo-serverselectiontimeouterror)
