Skip to content

How to Fix MongoDB Connection Refused Error

DodaTech Updated 2026-06-24 3 min read

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.conf settings.
  • Test connection strings locally before deploying to production.
  • Monitor disk space on the MongoDB data partition.

Common Mistakes with connection refused

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' 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

### What is the difference between mongodb and mongodb+srv connection strings?

<a href="/databases/mongodb/">mongodb</a>:// connects to a specific host or list of hosts. <a href="/databases/mongodb/">mongodb</a>+srv:// uses DNS SRV records to discover replica set members automatically. Use <a href="/databases/mongodb/">mongodb</a>+srv:// for Atlas and managed replica sets.

Why can my application connect via mongosh but not from code?

mongosh may use a different connection path (socket vs TCP), or the application connection string may be wrong. Verify the application uses the exact same host, port, and authentication as the shell.

Can I bind MongoDB to all interfaces safely?

Binding to 0.0.0.0 allows connections from any network. Always enable authentication and restrict access with a firewall. Never expose MongoDB directly to the internet without TLS and strong credentials.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro