How to fix MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017 in MongoDB
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- Check if MongoDB service is running
- Start the MongoDB service
- Verify the service is listening on port 27017
- Check MongoDB logs for startup errors
sudo systemctl status mongodMongoNetworkError: 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
- 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
- Firewall or security software is blocking port 27017
- MongoDB port is configured to a different port than 27017
- Previous MongoDB process crashed leaving a stale socket file
How to fix "MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017"
First, verify whether the MongoDB daemon is actively listening on port 27017:
On Linux/macOS:
sudo systemctl status mongodOn macOS with Homebrew:
brew services list | grep mongodbOn 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.
If MongoDB is not running, start it using the appropriate command for your operating system.
On Linux (systemd):
sudo systemctl start mongodOn Linux (service):
sudo service mongod startOn macOS with Homebrew:
brew services start mongodb-communityOn Windows:
Open Services (services.msc), find MongoDB or "MongoDB Server", right-click and select "Start".
Verify it started with:
sudo systemctl status mongodAfter starting the service, confirm MongoDB is actually listening on port 27017.
Using netcat:
nc -zv 127.0.0.1 27017Using netstat:
sudo netstat -tlnp | grep 27017Using MongoDB shell:
mongosh localhost:27017A successful connection means the service is running correctly. If it still fails, check the MongoDB logs in the next step.
If the service still won't start, examine the logs for error messages.
On Linux:
sudo tail -100 /var/log/mongodb/mongod.logOn macOS:
tail -100 /usr/local/var/log/mongodb/mongo.logOn 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.
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:
mongoose.connect("mongodb://localhost:27017/mydb")Use:
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):
net:
bindIp: 127.0.0.1
ipv6: falseThen restart MongoDB:
sudo systemctl restart mongodIf MongoDB crashed previously, a stale socket file in /tmp might prevent it from restarting.
Check for stale socket:
ls -la /tmp/mongodb-27017.sockRemove if it exists:
sudo rm /tmp/mongodb-27017.sockThen attempt to start MongoDB again:
sudo systemctl start mongodAdvanced 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.