mongodb://localhost:27017 — Connect & Run MongoDB Locally

What does mongodb://localhost:27017 mean? URI examples, mongosh and Compass, Docker startup, and fixes for ECONNREFUSED.


Seeing mongodb://localhost:27017 or mongodb localhost 27017 means an app expects MongoDB on this machine (default port 27017). It is a client connection string, not a web page — opening it in Chrome will not show a site.

Connection string cheat sheet

UseURI
Default (no auth)mongodb://localhost:27017
With databasemongodb://localhost:27017/mydb
Via IPmongodb://127.0.0.1:27017/mydb
With credentialsmongodb://user:pass@localhost:27017/mydb?authSource=admin
Trailing slashmongodb://localhost:27017/

Common env keys: MONGODB_URI, MONGO_URL, DATABASE_URL.

Port guide: localhost ports §27017.

Verify with a client

mongosh

mongosh "mongodb://localhost:27017"
use mydb
db.users.insertOne({ name: "Alice" })
db.users.find()

MongoDB Compass

Paste mongodb://localhost:27017 → Connect.

In code

await mongoose.connect('mongodb://localhost:27017/myapp');
StackNotes
NestJS@nestjs/mongoose
Expressmongoose.connect(...)
Next.jsMONGODB_URI in .env

Install & start

macOS: brew install mongodb-communitybrew services start mongodb-community

Docker:

docker run -d --name mongo -p 27017:27017 mongo:7

Troubleshooting

ECONNREFUSED 127.0.0.1:27017 — MongoDB not running; check lsof -i :27017; ensure the URI includes the mongodb:// scheme.
Auth errors — add user + authSource if auth is enabled.
Docker vs host — confirm -p 27017:27017; use service names between containers.

See port-conflicts, Redis.

Summary

mongodb://localhost:27017 = local MongoDB default. Confirm with mongosh/Compass, then reuse the same URI in your app.

访客计数:------ Best viewed in Netscape Navigator · 800×600 © LocalHost Run