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.
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.
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 mongodFor 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.
DivergentArrayError: For your own good, using document.save() to update an array which was selected using an $elemMatch projection will not work
How to fix "DivergentArrayError: For your own good, using document.save() to update an array which was selected using an $elemMatch projection will not work" in MongoDB
MongoServerError: bad auth : authentication failed
How to fix "MongoServerError: bad auth : authentication failed" in MongoDB
CannotCreateIndex: Cannot create index
CannotCreateIndex: Cannot create index
StaleShardVersion: shard version mismatch
How to fix "StaleShardVersion: shard version mismatch" in MongoDB
MongoOperationTimeoutError: Operation timed out
How to fix "MongoOperationTimeoutError: Operation timed out" in MongoDB