MongoDB: MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
The driver couldn't connect to a MongoDB server on localhost:27017 — nothing is listening there.
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017connect ECONNREFUSED ::1:27017Failed to connect to localhost:27017, reason: Connection refusedMongoNetworkError: failed to connect to server [localhost:27017] on first connect Meaning
The mongod process isn't running, it listens on another port/interface, or (in Docker) localhost is the app container. Start MongoDB (sudo systemctl start mongod, brew services start mongodb-community), check ss -ltnp | grep 27017, and use the service name in compose networks. IPv6 ::1 vs IPv4 mismatches also occur (use 127.0.0.1).
Common causes
- mongod not running or crashed
- Wrong host/port
- Docker: using localhost instead of the service name
- bindIp excludes the interface
- Node 17+ resolving localhost to ::1
⚡ Quick fix
sudo systemctl status mongodand start it- Use
mongodb://mongo:27017inside compose - Use 127.0.0.1 instead of localhost, check
bindIp
Detailed fix by platform
Linux
ss -ltnp | grep 27017 && mongosh --eval "db.runCommand({ping:1})"
How to diagnose
- Process — mongod running?
- Host — localhost vs service name?
- Port — 27017?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- Atlas: bad auth: Authentication failed MongoServerError: bad auth : Authentication failed. (Atlas user / special characters in password)
- bad auth : Authentication failed MongoDB: MongoServerError: bad auth : Authentication failed (code 8000 / 18)
- BSONObj size is invalid / exceeds maximum document size (16MB) MongoServerError: BSONObj size: 17825792 (0x1100000) is invalid. Size must be between 0 and 16793600(16MB)
Most viewed in MongoDB
- Document failed validation MongoDB: MongoServerError: Document failed validation (code 121)
- StrictPopulateError: Cannot populate path because it is not in your schema Mongoose 6+: StrictPopulateError: Cannot populate path `author` because it is not in your schema
- VersionError: No matching document found for id Mongoose: VersionError: No matching document found for id "5f..." version 3 modifiedPaths "items"
Other ways to find it
Where this error sits
Origin: Data. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 5 Nov 2026