mongodb://localhost:27017: Connecting to Local MongoDB

MongoDB's server, mongod, listens on port 27017, so mongodb://localhost:27017 is the default local connection string. Most refused connections have one of two causes: mongod is not running, or Node resolved localhost to the IPv6 address ::1 while MongoDB listens only on 127.0.0.1. Start the service, or connect to 127.0.0.1.

Starting MongoDB on each platform
PlatformStart commandCheck
macOS (Homebrew)brew services start mongodb-communitybrew services list
Linux (systemd)sudo systemctl start mongodsystemctl status mongod
Windowsnet start MongoDBservices.msc
Dockerdocker run -d -p 27017:27017 mongodocker ps

Updated

Port 27017 speaks the MongoDB wire protocol, not HTTP. Opening it in a browser prints a short notice that you are trying to access MongoDB over HTTP, which actually confirms the database is up. Connect with mongosh, Compass or a driver instead.

Connect from Node.js without the IPv6 trap

Use 127.0.0.1 in the connection string

serverSelectionTimeoutMS makes a missing server fail in seconds instead of hanging.

import { MongoClient } from "mongodb";

const client = new MongoClient("mongodb://127.0.0.1:27017/shop", {
  serverSelectionTimeoutMS: 3000,
});
await client.connect();

Exposing MongoDB safely

mongod binds to 127.0.0.1 by default for a reason. If you set bindIp to 0.0.0.0, turn on authorization in the config first, and firewall the port so only your app servers reach it.

If your goal is a simple business site rather than an app with its own data layer, We.Inc builds and hosts it with nothing to install.

How it works

  1. Start mongod with the command for your platform.
  2. Run mongosh to confirm a shell can connect.
  3. Use mongodb://127.0.0.1:27017/yourdb in your app's connection string.
  4. Add a user and authSource once anything beyond your own laptop can reach it.

Tips

  • ECONNREFUSED ::1:27017 is the IPv6 case. Node 17 and later stopped preferring IPv4 for localhost, so either use 127.0.0.1 or start mongod with --ipv6.
  • mongod exits at once if its data folder is missing or owned by another user; the log names the path.
  • In Docker Compose, your app container reaches the database at mongodb://mongo:27017, using the service name.

Frequently asked questions

What is mongodb://localhost:27017?

It is a MongoDB connection string pointing at a server on your own machine on the default port. Add a database name after the port, and credentials before the host when auth is on.

Why do I get ECONNREFUSED 127.0.0.1:27017?

Nothing is listening. The service is stopped, crashed on startup, or was never installed as a service. Start it and read its log if it stops again.

Why does MongoDB Compass connect but my Node app does not?

Compass may use 127.0.0.1 while your app uses localhost, which newer Node versions resolve to ::1. Use the same 127.0.0.1 string in the app.

How do I connect with a username and password?

Use mongodb://user:[email protected]:27017/mydb?authSource=admin when the user was created in the admin database. URL-encode special characters in the password.

Related: bolt://localhost:7687 (Neo4j), localhost refused to connect, Supabase vs Firebase, Docker Compose env files.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

Product

Who It's For

Features

Resources

Company

View Sitemap