Updated:

MongoDB localhost:27017: Connect and Fix Connection Refused

Connect with mongosh or Compass, start a local Docker instance, and distinguish connection refused, authentication, IPv6, and container networking errors.


mongodb://localhost:27017 tells a MongoDB client to connect to port 27017 on the device running that client. Paste it into MongoDB Compass or use it with mongosh. A web browser does not speak the MongoDB protocol.

Start with the exact error. Connection refused means the TCP connection failed; Authentication failed means the client reached a server that rejected its credentials. They need different fixes.

Choose the right connection string

Client location or configurationConnection string
Client and server on the same computermongodb://localhost:27017/myapp
Explicit IPv4 loopbackmongodb://127.0.0.1:27017/myapp
Host port mapped as 27018:27017mongodb://127.0.0.1:27018/myapp
App and a service named mongo on the same Compose networkmongodb://mongo:27017/myapp
A user created in the admin databasemongodb://USER:PASSWORD@localhost:27017/myapp?authSource=admin

/myapp selects the application database. authSource=admin selects where the user authenticates; use the database where that user was actually created. Encode reserved characters in usernames and passwords before inserting them into a URI. See the MongoDB connection string options.

Start a local server with Docker

This example needs Docker running. It uses the MongoDB Community image from the official Docker installation guide and binds the published port to this computer’s IPv4 loopback address.

docker run -d --name localhost-mongo -p 127.0.0.1:27017:27017 --mount source=localhost-mongo-data,target=/data/db mongodb/mongodb-community-server:9.0-ubi9-slim

If a container with that name already exists, inspect it with docker ps -a and restart it with docker start localhost-mongo rather than creating another one. This example has no authentication and is intended for local development.

Check the server inside the container:

docker exec localhost-mongo mongosh --quiet --eval 'db.adminCommand({ ping: 1 })'

The response should include ok: 1. This confirms the server inside Docker responds; test the published host port separately with an installed host-side mongosh:

mongosh "mongodb://127.0.0.1:27017/myapp" --eval 'db.runCommand({ ping: 1 })'

If the container check works and the host check fails, inspect the port mapping rather than reinstalling MongoDB.

Connect with Compass or your application

In Compass, create a connection with mongodb://127.0.0.1:27017. A successful connection shows the server and databases. An empty database is not a connection error.

For an app using Mongoose, install mongoose, then use your chosen URI:

import mongoose from 'mongoose';

await mongoose.connect(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/myapp');
console.log('MongoDB connected');

Keep credentials in your environment configuration. When the application runs in another container, replace 127.0.0.1 with the MongoDB service name on their shared network. See the Docker networking guide.

Fix connection refused in order

  1. Check the process. For the example above, run docker ps --filter name=localhost-mongo, then docker logs --tail 50 localhost-mongo if it stopped.
  2. Check the published port. docker port localhost-mongo 27017 should show a mapping to 127.0.0.1:27017. Use the host-side port from that output.
  3. Check where the client runs. A container’s localhost refers to that container. A phone’s localhost refers to the phone.
  4. Compare IPv4 and IPv6. If the error names ::1:27017, try 127.0.0.1 when the server listens only on IPv4. Do not change authentication to solve an address-binding problem.
  5. Identify port conflicts. Use the port conflict checklist before stopping an existing database process.
Error or observationNext check
ECONNREFUSED 127.0.0.1:27017Server state, listener, and port mapping
Authentication failedUsername, password, and authentication database
Server selection timeoutAddress reachable from the app’s network; inspect the underlying driver error
Data appears missing after connectingDatabase name and whether you connected to a different local instance

The URL parser explains a pasted URI without attempting a database connection. Use the port reference to compare MongoDB with HTTP and Redis.

Local development guides & tools © LocalHost Run