Fix MongoDB Connection Refused Error
Common Error Messages:
• "Error: couldn't connect to server 127.0.0.1:27017"
• "MongoDB connection refused"
• "Error connecting to MongoDB: connect ECONNREFUSED"
• "Error: couldn't connect to server 127.0.0.1:27017"
• "MongoDB connection refused"
• "Error connecting to MongoDB: connect ECONNREFUSED"
What This Error Means
The "connection refused" error occurs when your MongoDB client cannot establish a connection to the MongoDB server. This typically happens when:
- The MongoDB service (mongod) is not running
- MongoDB is running on a different port than expected
- Firewall rules are blocking the connection
- The MongoDB configuration is incorrect
Immediate Fix
Step 1: Check MongoDB Service Status
First, verify if MongoDB is actually running:
# On Ubuntu/Debian:
sudo systemctl status mongod
# On CentOS/RHEL:
sudo systemctl status mongod
# On macOS with Homebrew:
brew services list | grep mongodb
Step 2: Start MongoDB Service
If MongoDB is not running, start it:
# On Ubuntu/Debian:
sudo systemctl start mongod
sudo systemctl enable mongod # Auto-start on boot
# On CentOS/RHEL:
sudo systemctl start mongod
sudo systemctl enable mongod
# On macOS with Homebrew:
brew services start mongodb-community
# Manual start (if service commands don't work):
sudo mongod --config /etc/mongod.conf
Step 3: Check Port and Configuration
Verify MongoDB is listening on the correct port:
# Check what's listening on port 27017:
sudo netstat -tlnp | grep :27017
# Or using ss command:
sudo ss -tlnp | grep :27017
# Check MongoDB configuration:
cat /etc/mongod.conf
Step 4: Test Connection
# Test connection locally:
mongo --host 127.0.0.1 --port 27017
# Or with modern MongoDB shell:
mongosh "mongodb://127.0.0.1:27017"
Root Cause
1. Service Not Running
The most common cause is that the MongoDB daemon (mongod) has stopped or failed to start. This can happen due to:
- System restart without auto-start configured
- Resource constraints (disk space, memory)
- Configuration file errors
- Permission issues on data directory
2. Configuration Issues
Check your /etc/mongod.conf
file for common issues:
# Common configuration problems:
net:
port: 27017
bindIp: 127.0.0.1 # Change to 0.0.0.0 for external access
storage:
dbPath: /var/lib/mongo # Ensure this directory exists and has correct permissions
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
3. Firewall Blocking Connections
If connecting from another machine, check firewall rules:
# Ubuntu/Debian (ufw):
sudo ufw allow 27017
# CentOS/RHEL (firewalld):
sudo firewall-cmd --permanent --add-port=27017/tcp
sudo firewall-cmd --reload
# Check if port is blocked:
telnet your-server-ip 27017
How to Prevent This
1. Enable Auto-Start
sudo systemctl enable mongod
2. Monitor Service Health
Set up monitoring to alert when MongoDB goes down:
# Simple health check script:
#!/bin/bash
if ! pgrep mongod > /dev/null; then
echo "MongoDB is down!" | mail -s "MongoDB Alert" admin@yourcompany.com
sudo systemctl start mongod
fi
3. Log Rotation
Ensure log files don't fill up disk space:
# Add to /etc/logrotate.d/mongod:
/var/log/mongodb/*.log {
daily
missingok
rotate 52
compress
notifempty
create 644 mongod mongod
postrotate
/bin/kill -SIGUSR1 `cat /var/lib/mongo/mongod.lock 2> /dev/null` 2> /dev/null || true
endscript
}
Security Note: If you change
bindIp
from 127.0.0.1 to 0.0.0.0 to allow external connections, ensure you have proper authentication and firewall rules in place to prevent unauthorized access.
Tired of Database Backup Issues?
Backuro provides automated, reliable database backups with built-in monitoring and alerting. Never worry about connection issues affecting your backups again.
Get Professional Backup Solution