How to fix MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017 in MongoDB

MongoDBBEGINNERHIGHUpdated Dec 29, 2025ยท 234 views

This error occurs when your application cannot connect to MongoDB on the default port 27017, typically because the MongoDB service is not running or misconfigured. Fix it by starting the MongoDB service and verifying your connection settings.

Quick fix

Fixes ECONNREFUSED

sudo systemctl status mongod
  1. Check if MongoDB service is running
  2. Start the MongoDB service
  3. Verify the service is listening on port 27017
  4. Check MongoDB logs for startup errors
mongodb โ€” error
sudo systemctl status mongod
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017

What this error means

The ECONNREFUSED error means the connection was actively refused by the target server. In MongoDB context, this happens when your client tries to connect to port 27017 but finds nothing listening there. This is most commonly caused by the MongoDB daemon (mongod) not running, but can also result from incorrect configuration, firewall restrictions, or network connectivity issues between your client and the MongoDB server.

Typical symptoms

  • Application crashes or hangs on startup with MongoNetworkError
  • MongoDB Compass fails to connect with "connection refused" message
  • Node.js or other application cannot reach MongoDB
  • Error shows "127.0.0.1:27017" or "localhost:27017" in the message

Common causes

  1. MongoDB service (mongod) is not running or was not started
  2. Localhost resolves to IPv6 address ::1 instead of IPv4 127.0.0.1
  3. MongoDB is bound to a different IP address than your connection string specifies
  4. Firewall or security software is blocking port 27017
  5. MongoDB port is configured to a different port than 27017
  6. Previous MongoDB process crashed leaving a stale socket file

How to fix "MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017"

1Check if MongoDB service is running

First, verify whether the MongoDB daemon is actively listening on port 27017:

On Linux/macOS:

bash
sudo systemctl status mongod

On macOS with Homebrew:

bash
brew services list | grep mongodb

On Windows:
Open Task Manager โ†’ Services tab and look for "MongoDB" or "mongod" service. Check if its status shows "Running".

If the service is not running, proceed to the next step to start it.

2Start the MongoDB service

If MongoDB is not running, start it using the appropriate command for your operating system.

On Linux (systemd):

bash
sudo systemctl start mongod

On Linux (service):

bash
sudo service mongod start

On macOS with Homebrew:

bash
brew services start mongodb-community

On Windows:
Open Services (services.msc), find MongoDB or "MongoDB Server", right-click and select "Start".

Verify it started with:

bash
sudo systemctl status mongod
3Verify the service is listening on port 27017

After starting the service, confirm MongoDB is actually listening on port 27017.

Using netcat:

bash
nc -zv 127.0.0.1 27017

Using netstat:

bash
sudo netstat -tlnp | grep 27017

Using MongoDB shell:

bash
mongosh localhost:27017

A successful connection means the service is running correctly. If it still fails, check the MongoDB logs in the next step.

4Check MongoDB logs for startup errors

If the service still won't start, examine the logs for error messages.

On Linux:

bash
sudo tail -100 /var/log/mongodb/mongod.log

On macOS:

bash
tail -100 /usr/local/var/log/mongodb/mongo.log

On Windows:
Check C:\Program Files\MongoDB\Server\4.4\log\mongod.log (version number may differ)

Look for errors like "permission denied", "disk full", "address already in use", or configuration issues.

5Fix IPv6/IPv4 localhost resolution issue

If MongoDB is running but you still get ECONNREFUSED, the problem may be that your system resolves "localhost" to IPv6 (::1) instead of IPv4 (127.0.0.1).

Option A: Use explicit IP address in connection string
Instead of:

javascript
mongoose.connect("mongodb://localhost:27017/mydb")

Use:

javascript
mongoose.connect("mongodb://127.0.0.1:27017/mydb")

Option B: Explicitly disable IPv6 in MongoDB config
Edit your MongoDB configuration file (/etc/mongod.conf on Linux):

yaml
net:
  bindIp: 127.0.0.1
  ipv6: false

Then restart MongoDB:

bash
sudo systemctl restart mongod
6Clean up stale socket file if needed

If MongoDB crashed previously, a stale socket file in /tmp might prevent it from restarting.

Check for stale socket:

bash
ls -la /tmp/mongodb-27017.sock

Remove if it exists:

bash
sudo rm /tmp/mongodb-27017.sock

Then attempt to start MongoDB again:

bash
sudo systemctl start mongod

Advanced notes

For production deployments, ensure MongoDB is configured with proper authentication (username/password) and bind to specific IPs. If running in Docker, the connection string should point to the container name (e.g., "mongodb://mongo:27017/mydb") rather than localhost. For cloud databases (MongoDB Atlas), verify whitelist IP settings and that your connection string includes the correct cluster address. On Windows, if the service fails to start, check that the data directory and log file locations exist and have proper permissions. For development, consider using MongoDB Atlas free tier or Docker to avoid local setup issues entirely.

Frequently asked questions

What does "ECONNREFUSED" mean?
The ECONNREFUSED error means the connection was actively refused by the target server. In MongoDB context, this happens when your client tries to connect to port 27017 but finds nothing listening there. This is most commonly caused by the MongoDB daemon (mongod) not running, but can also result from incorrect configuration, firewall restrictions, or network connectivity issues between your client and the MongoDB server.
How do I fix "ECONNREFUSED"?
This error occurs when your application cannot connect to MongoDB on the default port 27017, typically because the MongoDB service is not running or misconfigured. Fix it by starting the MongoDB service and verifying your connection settings.
What causes "ECONNREFUSED"?
MongoDB service (mongod) is not running or was not started Localhost resolves to IPv6 address ::1 instead of IPv4 127.0.0.1 MongoDB is bound to a different IP address than your connection string specifies
Is the "ECONNREFUSED" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading