Fix: MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
Your app or tool tried to reach MongoDB on port 27017 and nothing accepted the connection. Usually mongod is not running, crashed at startup, or listens on a different address.
Not checked by a trusted editor yet Last edited Outdated?
The Node.js driver (and tools built on it) reports this when its TCP connection to the server is refused:
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
ECONNREFUSED comes from the operating system: the host answered, but no process was listening on that address and port. The driver retries until its server selection timeout runs out, then throws this error. A variant with ::1:27017 means the client tried the IPv6 loopback address. The causes below are ordered from most to least common.
1. mongod is not running
Check before you run this
- Runs with administrator rights. Make sure you understand every line.
sudo systemctl status mongod
sudo systemctl start mongod
sudo systemctl enable mongod # start on boot
If systemd reports Unit mongod.service not found, reload the unit files with sudo systemctl daemon-reload and try again. On macOS with Homebrew the service is managed with brew services; on Windows check the MongoDB service in Services.
2. mongod starts and immediately stops
If the status shows failed or the process keeps restarting, the reason is in the log, by default /var/log/mongodb/mongod.log on Linux packages:
Check before you run this
- Runs with administrator rights. Make sure you understand every line.
sudo tail -n 50 /var/log/mongodb/mongod.log
sudo journalctl -u mongod -n 50 --no-pager
- Data directory errors such as
Data directory ... not foundor a read-only lock file: thestorage.dbPathdirectory is missing or not writable by the service account. On Debian and Ubuntu packages the default is/var/lib/mongodb, owned bymongodb; on RHEL-family packages it is/var/lib/mongo, owned bymongod. Fix ownership withchown -Ron that directory. - Socket is already in use: another process (often a second
mongodstarted by hand) holds the port. Find it withsudo ss -tulpn | grep 27017. - Configuration errors: a YAML typo in
/etc/mongod.conf, such as a tab instead of spaces, stops startup. The log names the line.
3. Wrong address or port
Check where the server really listens and compare it with your connection string:
Check before you run this
- Runs with administrator rights. Make sure you understand every line.
sudo ss -ltnp | grep mongod
grep -A3 '^net:' /etc/mongod.conf
- If
net.portis not 27017, put the right port in the URI. - If the error shows
::1:27017,localhostresolved to IPv6 while mongod listens only on127.0.0.1. Usemongodb://127.0.0.1:27017in the client, or add::1tonet.bindIp. - If the client runs on another machine, the default
bindIp: 127.0.0.1accepts only local connections. Add the server’s private interface address tonet.bindIp(comma separated), restart, and allow the port in the firewall only from the client hosts.
net:
port: 27017
bindIp: 127.0.0.1,10.0.0.15
Do not bind to 0.0.0.0 or a public address until authentication is enabled and a firewall limits who can connect.
4. Containers and VMs
- Inside a container,
127.0.0.1is the container itself, not the host or another container. Use the service name from your Compose file, for examplemongodb://mongo:27017. - If MongoDB runs in Docker and the app runs on the host, publish the port, for example
-p 127.0.0.1:27017:27017, and check it withdocker ps. - In WSL, a server started on the Windows side and one inside the Linux distribution are separate. Make sure the client talks to the one that is actually running.
Confirm the fix
mongosh "mongodb://127.0.0.1:27017" --eval 'db.runCommand({ ping: 1 })'
A reply containing ok: 1 means the server is reachable. Then run your application with exactly the same host and port. If mongosh works but the app does not, print the URI the app really uses; environment variables and config files often differ between the two.
Official documentation
Did this work for you?
No votes yet. Be the first.
If it did not work, the report form below tells the editors what went wrong.
See also
- 0231 MongoDB documents and collections compared with SQL tables MongoDB stores data as JSON-like documents grouped in collections instead of rows in tables. This explains the model, the _id field, embedding… Beginner · Explains an idea · Any system Article Basics · Choosing a database 15 Oct 2026
- 0232 mongosh cheat sheet: CRUD, indexes and explain() The mongosh commands you use every day: switching databases, inserting, querying, updating and deleting documents, creating indexes and checking a query plan… Beginner · Cheat sheet · Any system Article Basics 15 Oct 2026
- 0233 Turn on MongoDB authentication and create least-privilege users A fresh MongoDB install accepts connections without a password. Create a user administrator, enable authorization, then give each application and backup job… Intermediate · Step by step · Linux Article Administration · Security & access 15 Oct 2026
- 0234 Nightly mongodump backup with rotation A bash script for cron that writes a compressed mongodump archive each night, keeps the password out of the process list, checks… Intermediate · Step by step · Linux Script Administration · Database operations 15 Oct 2026
Repos in the wild
- MongoDB server source code (mongodb/mongo) mongodb/mongo The official repository of the MongoDB database server, including mongod, mongos and the core server tests. GitHub
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Commitpedia +3,701 chars