How to Fix MongoDB Connection Refused Error
In this tutorial, you'll learn about How to Fix MongoDB Connection Refused Error. We cover key concepts, practical examples, and best practices.
The Problem
Your application tries to connect to MongoDB and gets:
MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
Or from mongosh:
NetworkError: connect ECONNREFUSED 127.0.0.1:27017
MongoDB is not accepting connections. The server may be stopped, bound to the wrong interface, or blocked by a firewall.
Quick Fix
1. Check if MongoDB is running
# systemd systems
sudo systemctl status mongod
# If not running
sudo systemctl start mongod
sudo systemctl enable mongod
On macOS:
brew services list | grep mongodb
brew services start mongodb-community
Verify the port:
sudo ss -tlnp | grep 27017
2. Fix the bind IP configuration
MongoDB 3.6+ binds only to 127.0.0.1 by default. Edit /etc/mongod.conf:
# Wrong — localhost only
net:
port: 27017
bindIp: 127.0.0.1
# Right — allow remote (configure firewall)
net:
port: 27017
bindIp: 0.0.0.0
Or bind to specific IPs:
net:
bindIp: 127.0.0.1,10.0.0.5
Restart after changing:
sudo systemctl restart mongod
3. Check authentication settings
If MongoDB requires auth but your connection string lacks credentials:
// Wrong — no credentials
mongoose.connect('mongodb://localhost:27017/mydb')
// Right — with credentials
mongoose.connect('mongodb://user:password@localhost:27017/mydb?authSource=admin')
Reset a user password from the local shell:
mongosh
use admin
db.changeUserPassword("myUser", "newPassword")
4. Check firewall and port blocking
# UFW
sudo ufw status
sudo ufw allow from 10.0.0.0/24 to any port 27017
# firewalld
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/24" port protocol="tcp" port="27017" accept'
sudo firewall-cmd --reload
5. Check the connection string
Use the correct format for your deployment:
# Standalone
mongodb://user:password@host:27017/mydb
# Replica set
mongodb://user:password@host1:27017,host2:27017/mydb?replicaSet=rs0
# Atlas (SRV)
mongodb+srv://cluster.mongodb.net/mydb
For Docker, use the container name:
docker run --network app-network --name mongodb -d mongo
docker run --network app-network -e MONGO_URI="mongodb://mongodb:27017/mydb" my-app
Prevention
- Enable MongoDB to start on boot with
systemctl enable mongod. - Use a configuration management tool to enforce consistent
mongod.confsettings. - Test connection strings locally before deploying to production.
- Monitor disk space on the MongoDB data partition.
Common Mistakes with connection refused
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world MONGODB code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro