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
| Use | URI |
|---|---|
| Default (no auth) | mongodb://localhost:27017 |
| With database | mongodb://localhost:27017/mydb |
| Via IP | mongodb://127.0.0.1:27017/mydb |
| With credentials | mongodb://user:pass@localhost:27017/mydb?authSource=admin |
| Trailing slash | mongodb://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');| Stack | Notes |
|---|---|
| NestJS | @nestjs/mongoose |
| Express | mongoose.connect(...) |
| Next.js | MONGODB_URI in .env |
Install & start
macOS: brew install mongodb-community → brew services start mongodb-community
Docker:
docker run -d --name mongo -p 27017:27017 mongo:7Troubleshooting
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.