TL;DR
ECONNREFUSED on port 27017 means your app tried to reach MongoDB, but the service isn't running โ nothing is listening on that port. Nine times out of ten, one command is all you need:
# Linux (systemd)
sudo systemctl start mongod
# macOS (Homebrew)
brew services start mongodb-community
# Windows
net start MongoDB
Root Cause
ECONNREFUSED is a TCP-level error. The OS actively rejected the connection โ the port is closed. That happens for a few reasons:
- The
mongodprocess isn't running - MongoDB is listening on a different port or bind address
- A firewall is blocking port 27017
- The data directory has wrong permissions, preventing
mongodfrom starting - MongoDB crashed on startup (the logs will say why)
Step 1 โ Check if mongod is running
# Linux / macOS
ps aux | grep mongod
# Or check the service status
sudo systemctl status mongod
A status of inactive (dead) means MongoDB stopped โ or never started. Jump to Step 2.
Step 2 โ Start MongoDB
# Linux (systemd โ Ubuntu, Debian, RHEL, CentOS)
sudo systemctl start mongod
sudo systemctl enable mongod # auto-start on boot
# macOS (Homebrew)
brew services start mongodb-community
# macOS (manual, if installed without Homebrew)
mongod --config /usr/local/etc/mongod.conf
# Windows (Command Prompt as Administrator)
net start MongoDB
Step 3 โ Verify the port is listening
Don't assume the start succeeded. Confirm MongoDB is actually accepting connections on port 27017:
# Linux / macOS
ss -tlnp | grep 27017
# or
lsof -i :27017
# Windows
netstat -ano | findstr 27017
You want to see output like this:
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1234/mongod
That line confirms MongoDB is listening. Try your app again โ the ECONNREFUSED error should be gone.
Step 4 โ If mongod fails to start, check the logs
Sometimes systemctl start mongod returns no error but the service immediately dies. The logs will tell you exactly what went wrong:
# Linux
sudo journalctl -u mongod -n 50 --no-pager
# Or check the log file directly
sudo tail -n 50 /var/log/mongodb/mongod.log
Common log errors and fixes
Permission denied on data directory
# Log output:
# exception in initAndListen: NonExistentPath: Data directory /var/lib/mongodb not found.
# Fix: create the directory and fix ownership
sudo mkdir -p /var/lib/mongodb
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chmod 755 /var/lib/mongodb
Port already in use
# Log output:
# Address already in use for socket: 0.0.0.0:27017
# Find what's using the port
sudo lsof -i :27017
# Kill the process if it's a stale mongod
sudo kill -9 <PID>
Lock file from a previous crash
# Log output:
# Unclean shutdown detected. Please visit...
# Remove the lock file
sudo rm /var/lib/mongodb/mongod.lock
sudo mongod --repair --dbpath /var/lib/mongodb
sudo systemctl start mongod
Step 5 โ Check the bind IP in mongod.conf
MongoDB running but still getting refused? The bind address is likely the problem. By default, MongoDB only listens on 127.0.0.1 โ connections from other hosts or containers get rejected before they even reach the auth layer.
# Open the config file
sudo nano /etc/mongod.conf
# Look for this section:
net:
port: 27017
bindIp: 127.0.0.1 # โ Only accepts local connections
To allow connections from other hosts โ a Docker container, a second server, etc. โ change bindIp:
net:
port: 27017
bindIp: 0.0.0.0 # Accept from any IP (use with firewall rules)
Then restart:
sudo systemctl restart mongod
Security note: Only bind to 0.0.0.0 if MongoDB sits behind a firewall. Never expose port 27017 directly to the internet without authentication and network restrictions.
Step 6 โ Firewall check
Connecting to a remote MongoDB host? A firewall could be silently dropping packets on port 27017. Open the port explicitly:
# Ubuntu (ufw)
sudo ufw allow 27017/tcp
sudo ufw status
# RHEL/CentOS (firewalld)
sudo firewall-cmd --permanent --add-port=27017/tcp
sudo firewall-cmd --reload
Step 7 โ Docker and Docker Compose
Docker adds a networking wrinkle. Inside a container, 127.0.0.1 points to the container itself โ not the host machine. So if your Node.js app runs in Docker and MongoDB is on the host (or in a different container), the connection string needs to change:
# Wrong โ resolves to the container's loopback, not the host
MONGODB_URI=mongodb://127.0.0.1:27017/mydb
# Correct โ use host.docker.internal (Docker Desktop on macOS / Windows)
MONGODB_URI=mongodb://host.docker.internal:27017/mydb
# Correct โ if MongoDB is a service in docker-compose.yml, use the service name
MONGODB_URI=mongodb://mongo:27017/mydb
Example docker-compose.yml:
services:
app:
build: .
environment:
- MONGODB_URI=mongodb://mongo:27017/mydb
depends_on:
- mongo
mongo:
image: mongo:7
ports:
- "27017:27017"
Verification
Use mongosh to confirm the connection works end-to-end:
mongosh mongodb://127.0.0.1:27017
# Expected:
# Connecting to: mongodb://127.0.0.1:27017/
# MongoServerVersion: 7.x.x
# >
Or test with a quick Node.js snippet using Mongoose:
const mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/test')
.then(() => console.log('Connected!'))
.catch(err => console.error(err));
Connected! in the output means the fix worked. No more ECONNREFUSED.

