MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017 MongoDB Origin: Data OSI layer 4 · Transport Last updated 5 Nov 2026

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.

Exact message you may see 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
Seen on: MongoDB Node.js Linux

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

  1. sudo systemctl status mongod and start it
  2. Use mongodb://mongo:27017 inside compose
  3. Use 127.0.0.1 instead of localhost, check bindIp

Detailed fix by platform

Linux

  1. ss -ltnp | grep 27017 && mongosh --eval "db.runCommand({ping:1})"

How to diagnose

  1. Process — mongod running?
  2. Host — localhost vs service name?
  3. 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:

Where this error sits

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

Atlas: bad auth: Authentication failed — MongoServerError: bad auth : Authentication failed. (Atlas user / special characters in password) (MongoDB)Atlas: bad auth: Auth…bad auth : Authentication failed — MongoDB: MongoServerError: bad auth : Authentication failed (code 8000 / 18) (MongoDB)bad auth : Authentica…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…Can't connect from another host (bindIp 127.0.0.1) — MongoDB: connection refused / timed out from a remote host — mongod bindIp defaults to 127.0.0.1 (MongoDB)Can't connect from an…mongo: command not found (use mongosh) — MongoDB: bash: mongo: command not found / zsh: command not found: mongo (MongoDB)mongo: command not fo…mongod exited with code 14 (data dir permission / lock) — mongod: Process exited with status 14 — Exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory (MongoDB)mongod exited with co…MongoParseError — MongoDB: MongoParseError: Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://" (MongoDB)MongoParseErrorMongoServerSelectionError — MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017 (Node.js)MongoServerSelectionE…MongoNetw…

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.