ECONNREFUSED or 61

MongoDB Connection Refused on Port 27017 – Quick Fix

Database Errors Beginner 👁 8 views 📅 Jun 20, 2026

MongoDB throws "connection refused" when the server isn't running or bound to localhost. Restarting mongod usually fixes it. Here's the exact command and why it works.

You get this when trying to connect to MongoDB and it just says no.

First thing: check if mongod is even alive. Most people forget it's a daemon—it doesn't start by itself.

sudo systemctl status mongod

If you see inactive (dead) or failed, that's your problem. Start it:

sudo systemctl start mongod

On macOS (homebrew), it's:

brew services start mongodb-community

Now try connecting again. If that worked, skip to the prevention section. If not, keep reading.

Why this happens

MongoDB's default config (/etc/mongod.conf) binds to 127.0.0.1 by default. That means it only listens on localhost. If you're trying to connect from another machine (or a Docker container), you'll get connection refused even if mongod is running.

What's actually happening here is that the server is up, but it's not accepting connections on the IP your client is hitting. The error message is the same either way.

Quick test: see if it's listening

sudo lsof -i :27017

If nothing shows, mongod isn't running. If you see mongod and 127.0.0.1:27017 (LISTEN), then it's bound to localhost only.

Less common variations of the same issue

Binding to 0.0.0.0 (all interfaces)

Edit the config file:

sudo nano /etc/mongod.conf

Find the net section. Change:

net:
  port: 27017
  bindIp: 0.0.0.0

Then restart mongod. This lets connections from any IP. Be careful—don't do this on a public server without firewall rules.

Docker container can't connect to host MongoDB

If MongoDB runs on your host but a Docker container says connection refused, you can't connect to localhost from inside a container—localhost means the container itself, not your host. Use host.docker.internal on macOS/Windows, or your host's actual IP on Linux.

mongodb://host.docker.internal:27017

MongoDB service crashed but process is still running

Sometimes the daemon hangs but the process lingers. You'll see mongod in ps aux but connections still fail. Kill it hard:

sudo killall -9 mongod
sudo systemctl start mongod

IPv6 binding issue on Linux

On some distros, if your system prefers IPv6 and MongoDB is bound to 127.0.0.1 (IPv4 only), the connection might time out instead of being refused. Check with ss -tlnp for both IPv4 and IPv6 listeners. If you see only tcp6 for port 27017, add bindIp: 0.0.0.0 to force IPv4.

Prevention

Set mongod to start on boot:

sudo systemctl enable mongod

On macOS:

brew services start mongodb-community --now

Also add a cron job that checks every 5 minutes and restarts if dead. Ugly but works:

*/5 * * * * /usr/bin/systemctl is-active --quiet mongod || /usr/bin/systemctl start mongod

One more thing: if you're on a shared server and someone else changed the port in mongod.conf, lsof will show the real port. Don't assume it's always 27017.

That should cover the common paths. If you still get connection refused, check your firewall—sudo ufw status on Ubuntu, or the Windows Firewall rules if you're on Windows. MongoDB uses port 27017 by default, but you already knew that.

Was this solution helpful?