Home › Databases

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 — how to fix

MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at _handleConnectionErrors (/app/node_modules/mongoose/lib/connection.js:...)
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

Quick fix: If the address is ::1, replace localhost with 127.0.0.1 in the URI: mongodb://127.0.0.1:27017/mydb. Otherwise start MongoDB (sudo systemctl start mongod, brew services start mongodb-community, or docker run -d -p 27017:27017 mongo). From another container, use the service name: mongodb://mongo:27017/mydb.

What it means

ECONNREFUSED means the TCP connection was actively rejected: the host was reachable, but no process was listening on that address and port. The address in the message tells you what was tried.

Cause 1: localhost resolves to IPv6 ::1

If the error says ::1:27017, this is almost certainly it. Since Node.js 17, localhost is no longer forced to IPv4 and often resolves to ::1 first. MongoDB's default config binds only to 127.0.0.1, so the IPv6 attempt is refused, even though MongoDB is running.

Fix 1

Use the IPv4 address in the connection string:

mongoose.connect('mongodb://127.0.0.1:27017/mydb');

Also update .env files (MONGO_URI=mongodb://127.0.0.1:27017/mydb). Alternatively, tell the driver to use IPv4 (mongoose.connect(uri, { family: 4 })), or make mongod also listen on IPv6 in /etc/mongod.conf:

net:
  port: 27017
  bindIp: 127.0.0.1,::1

Cause 2: MongoDB isn't running

The error shows 127.0.0.1:27017 and nothing listens there. Check:

mongosh --eval 'db.runCommand({ ping: 1 })'
sudo ss -ltnp | grep 27017

Fix 2

Start the server for your platform:

# Linux (official packages)
sudo systemctl start mongod
sudo systemctl enable mongod      # start on boot
sudo systemctl status mongod

# macOS (Homebrew)
brew services start mongodb-community

# Docker
docker run -d --name mongo -p 27017:27017 mongo

On Windows, start the "MongoDB Server" service from services.msc, or net start MongoDB in an admin prompt.

If mongod fails to start, read the reason:

sudo journalctl -u mongod -n 50
sudo tail -n 50 /var/log/mongodb/mongod.log

Frequent causes are a data directory owned by root after running sudo mongod once (sudo chown -R mongodb:mongodb /var/lib/mongodb), a stale socket file /tmp/mongodb-27017.sock owned by root, or a full disk.

Cause 3: app and database in different Docker containers

Inside a container, localhost / 127.0.0.1 is the container itself, not the host or the MongoDB container.

Fix 3

In Docker Compose, connect using the service name:

services:
  api:
    build: .
    environment:
      MONGO_URI: mongodb://mongo:27017/mydb
    depends_on:
      - mongo
  mongo:
    image: mongo
    ports:
      - "27017:27017"

To reach a MongoDB running on the host from a container on Docker Desktop, use mongodb://host.docker.internal:27017. On Linux, add extra_hosts: ["host.docker.internal:host-gateway"] and make mongod listen on an address the container can reach.

depends_on only waits for the container to start, not for MongoDB to accept connections, so the first attempt may still be refused. Add a retry in your app or a healthcheck with condition: service_healthy.

Cause 4: remote server or MongoDB Atlas

For a remote host, the server may only bind to 127.0.0.1, or a firewall blocks 27017. For Atlas, the usual error is a server selection timeout rather than a refusal; check that your current IP is in the project's Network Access list and that you use the mongodb+srv:// string from the Atlas UI.

Fix 4

On a self-hosted server, set bindIp to include the server's private address, enable authentication before exposing it, and open the port only to your app servers. Never expose MongoDB without auth to the internet.

Last updated 2026-11-30.