MongoDB "connect ECONNREFUSED 127.0.0.1:27017": why it refuses, and how to fix it
Fix MongoDB "connect ECONNREFUSED 127.0.0.1:27017" and ::1:27017: start mongod, read exit code 14, check bindIp, IPv6 localhost in Node.js, and Docker service names.
by OhSudo ··
Checked on real software: MongoDB · November 2026How we check
Not sure this is your cause?Find it step by step: Can't connect to the database →
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017or, from mongosh and newer drivers:
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017ECONNREFUSED means your request reached the machine, and nothing was listening on that address and port. Your connection string may be perfectly right. Usually MongoDB isn’t running, listens somewhere else, or you’re on a different “localhost” from the one you think.
- Your appconnects to 127.0.0.1:27017connects
- That machinereached(works)refused(fails)
- Port 27017nothing listening(breaks here)
TL;DR
sudo systemctl status mongod # is it running?sudo systemctl start mongodsudo tail -n 30 /var/log/mongodb/mongod.log # why it stoppedsudo ss -tlnp | grep 27017 # where does it listen?mongosh "mongodb://127.0.0.1:27017" # test itIf the error says ::1:27017, jump to section 3. In Docker, section 4.
1. Is mongod running?
sudo systemctl status mongodIf it’s inactive, start it, and make it start at boot:
sudo systemctl start mongodsudo systemctl enable mongodOn a new install, the service doesn’t always start by itself. If Unit mongod.service could not be found, MongoDB isn’t installed as a service here: the mongodb-org packages come from MongoDB’s own repository. On macOS with Homebrew it’s brew services start mongodb-community, and on Windows net start MongoDB.
2. It won’t start: exit code 14 and friends
mongod.service: Main process exited, code=exited, status=14/n/aThe reason is in the log, in /var/log/mongodb/mongod.log by default, or:
sudo journalctl -u mongod -n 30 --no-pagerStatus 14 very often means a wrong owner, typically after someone ran mongod by hand with sudo, leaving files owned by root:
sudo chown -R mongodb:mongodb /var/lib/mongodb /var/log/mongodbsudo rm -f /tmp/mongodb-27017.socksudo systemctl start mongodOn RHEL-family systems, the user and group are mongod, and the data folder is /var/lib/mongo.
Other usual suspects in the log:
| Log says | Cause |
|---|---|
No space left on device |
A full disk: see No space left on device |
Address already in use |
Another mongod holds the port: see Address already in use |
Data directory … not found |
storage.dbPath in /etc/mongod.conf points to a missing folder |
Error parsing YAML config |
A typo in mongod.conf: it’s YAML, so indentation counts |
Status 4/ILL or Illegal instruction |
Newer MongoDB needs a CPU with AVX, which some old servers and VMs don’t expose |
MongoDB cannot start: Linux kernel versions 6.19 and newer has a known incompatibility |
MongoDB 8 and a recent kernel. Check SERVER-121912 for a fixed version. Until then, the options are a kernel up to 6.18, or MongoDB 7 |
If it stopped with no message, the kernel may have killed it for memory: see Linux process “Killed”.
3. ::1:27017: Node.js and IPv6 localhost
MongoServerSelectionError: connect ECONNREFUSED ::1:27017Note the ::1. That’s IPv6 localhost. Since version 17, Node.js doesn’t reorder addresses to put IPv4 first, so localhost can resolve to ::1, while mongod listens only on 127.0.0.1 by default. MongoDB is running, and the app knocks at the wrong door.
Use the address in your connection string:
mongodb://127.0.0.1:27017/shop4. Inside Docker
In a container, localhost is the container itself. If your app runs in one, and MongoDB in another, use the service name:
services: app: environment: MONGODB_URI: mongodb://db:27017/shop db: image: mongo:8With MongoDB on the host, use host.docker.internal, as connecting from a container to localhost explains. The Docker Compose Generator writes the service, its volume and a health check, so the app can wait until the database is ready: an app that connects before db is up gets this same error.
5. From another machine: bindIp
sudo ss -tlnp | grep 27017LISTEN 0 4096 127.0.0.1:27017 0.0.0.0:* users:(("mongod",pid=812,fd=14))127.0.0.1 means only local connections are accepted. To accept others, add the server’s private address in /etc/mongod.conf, then restart it:
net: port: 27017 bindIp: 127.0.0.1,10.0.0.5Warning
Turn on authentication before you open it up, and allow only the machines that need it in the firewall. Port 27017 should never face the internet: exposed MongoDB servers have been wiped and held for ransom by the thousands, as the Port Lookup notes.
If the connection hangs instead of being refused, a firewall is dropping it: see “Connection timed out” vs “Connection refused”.
Shell, we have a problem!
Similar, but different: MongoServerError: Authentication failed means you reached MongoDB, and the user, password or authSource is wrong: see that article. querySrv ENOTFOUND or getaddrinfo ENOTFOUND is about the name, often with a mongodb+srv:// address: see Temporary failure in name resolution. And Server selection timed out against MongoDB Atlas usually means your IP isn’t in the project’s access list. Once you’re connected, a refused write is often E11000.
Found this useful? Share it.
Found a mistake? Let us know at contact@ohsudo.com.