localhost:27017 — MongoDB

If you've chosen MongoDB for your project, localhost:27017 is where your data lives during development. Unlike relational databases that store data in rows and columns (MySQL on 3306, PostgreSQL on 5432), MongoDB stores flexible JSON-like documents — making it the natural fit for JavaScript-heavy stacks. You don't need to define a schema upfront. Just start inserting data and the structure follows.

This is the default port for mongod, the MongoDB server process. It also handles the MongoDB Wire Protocol — meaning every MongoDB driver, GUI tool, and shell connects here unless you tell it otherwise.

Connecting to MongoDB

The standard connection string format is:

mongodb://localhost:27017/your_database_name

That single URI works everywhere — in mongosh, in Compass, in your application code. If you've enabled authentication (and you should, even in development), it becomes:

mongodb://username:password@localhost:27017/your_database?authSource=admin

mongosh (The Modern Shell)

mongosh replaced the old mongo shell. It's a proper Node.js REPL with syntax highlighting, auto-completion, and built-in help:

# Just connect (defaults to localhost:27017)
mongosh

# Connect to a specific database
mongosh "mongodb://localhost:27017/myapp"

# Quick health check — should return { ok: 1 }
mongosh --eval "db.runCommand({ping: 1})"

# Show all databases
mongosh --eval "show dbs"

Application Connection Strings

# Node.js (Mongoose — the most popular MongoDB ODM)
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/myapp');

# Node.js (native driver)
const { MongoClient } = require('mongodb');
const client = new MongoClient('mongodb://localhost:27017');
await client.connect();
const db = client.db('myapp');

# Python (PyMongo)
from pymongo import MongoClient
client = MongoClient('mongodb://localhost:27017/')
db = client.myapp

# PHP
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->myapp->users;

# Go
client, _ := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))

# Java (Spring Boot application.properties)
spring.data.mongodb.uri=mongodb://localhost:27017/myapp

Is MongoDB Running?

Before debugging connection issues, confirm mongod is actually up:

# Linux (systemd)
sudo systemctl status mongod

# macOS (Homebrew)
brew services list | grep mongodb

# Any OS — check if port 27017 is listening
# Windows
netstat -an | findstr "27017"
# macOS/Linux
lsof -i :27017

# Docker
docker ps | grep mongo

Starting MongoDB

How you start MongoDB depends on how it was installed:

# Linux (systemd — persists across reboots)
sudo systemctl start mongod
sudo systemctl enable mongod   # auto-start on boot

# macOS (Homebrew)
brew services start mongodb-community

# Docker (quickest way to get started from scratch)
docker run -d --name mongodb \
  -p 27017:27017 \
  -v mongodb_data:/data/db \
  mongo:7

# Manual start (any OS — useful for custom config)
mongod --dbpath /path/to/data --port 27017
Docker is the fastest path. If you just need MongoDB for development and don't want to deal with system-level installation, the Docker command above gets you a running instance in under 10 seconds. The -v flag persists data between container restarts.

MongoDB Compass

Compass is MongoDB's official GUI. It lets you browse collections visually, build aggregation pipelines with drag-and-drop, analyze schema patterns, and view query performance. Connect with:

mongodb://localhost:27017

It's free and useful even if you prefer the shell — the aggregation pipeline builder alone saves significant time when crafting complex queries.

Fixing "Connection Refused"

The number one MongoDB error. If you see MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017, it means nothing is listening on that port. In order of likelihood:

MongoDB isn't running. Start it with the commands above. On Linux, check sudo systemctl status mongod — if it says "inactive" or "failed", look at the logs: sudo journalctl -u mongod --no-pager -n 50

MongoDB crashed on startup. Common cause: the data directory doesn't exist or has wrong permissions. The default is /data/db on Linux/Mac. Create it with sudo mkdir -p /data/db && sudo chown -R $USER /data/db

Port conflict. Something else grabbed 27017 first. Check with lsof -i :27017. Kill the conflicting process or change MongoDB's port in mongod.conf.

bindIp restriction. MongoDB's config file (/etc/mongod.conf) has a bindIp setting. If it's set to a specific IP and you're connecting from a different one, the connection gets refused. For local development, bindIp: 127.0.0.1 is correct. If you need access from other machines (like a Docker container on a bridge network), change it to 0.0.0.0 — but only in development.

Authentication Errors

If you get MongoServerError: Authentication failed, the server is running but rejecting your credentials:

  • Check authSource in your connection string — MongoDB authenticates against a specific database. Admin users typically authenticate against the admin database
  • Make sure the user exists for the database you're connecting to. Run db.getUsers() in mongosh on the target database
  • If you just installed MongoDB and haven't created users yet, authentication might be enabled in the config but no users exist. Either create a user or temporarily disable auth in mongod.conf (security.authorization: disabled)

MongoDB's Other Ports

PortPurpose
27017Default mongod instance
27018Default for shard server (shardsvr)
27019Default for config server (configsvr)
28017Legacy HTTP status page (removed in 3.6+)

Related Database Ports

PortDatabaseType
3306MySQL / MariaDBRelational (SQL)
5432PostgreSQLRelational (SQL)
6379RedisKey-value / Cache
1433SQL ServerRelational (SQL)