localhost:27017
localhost:27017
Port 27017 is the default port for MongoDB, a popular NoSQL document database. MongoDB stores data in flexible JSON-like documents and is widely used for modern web applications requiring scalability and flexibility.
Connect to MongoDB
# Connect with mongosh (MongoDB Shell)
mongosh mongodb://localhost:27017
# Or simply
mongosh
# Connect to specific database
mongosh mongodb://localhost:27017/mydb
# With authentication
mongosh mongodb://username:password@localhost:27017
# Check connection
db.version()
Basic MongoDB Commands
# Show databases
show dbs
# Use/create database
use mydb
# Show collections
show collections
# Insert document
db.users.insertOne({ name: "John", age: 30 })
# Find documents
db.users.find()
# Find one
db.users.findOne({ name: "John" })
# Update document
db.users.updateOne(
{ name: "John" },
{ $set: { age: 31 } }
)
# Delete document
db.users.deleteOne({ name: "John" })
# Count documents
db.users.countDocuments()
Node.js MongoDB Connection
// npm install mongodb
const { MongoClient } = require('mongodb');
const uri = 'mongodb://localhost:27017';
const client = new MongoClient(uri);
async function run() {
try {
await client.connect();
console.log('Connected to MongoDB');
const database = client.db('mydb');
const collection = database.collection('users');
// Insert
await collection.insertOne({ name: 'John', age: 30 });
// Find
const users = await collection.find({}).toArray();
console.log(users);
} finally {
await client.close();
}
}
run().catch(console.error);
Fix "Connection refused" on Port 27017
# Start MongoDB service
# Windows
net start MongoDB
# Linux
sudo systemctl start mongod
# Mac
brew services start mongodb-community
# Check if MongoDB is running
# Windows
sc query MongoDB
# Linux/Mac
sudo systemctl status mongod
ps aux | grep mongod
# Check if listening on port 27017
netstat -ano | findstr :27017
lsof -i :27017
Related Ports and Resources
- localhost:3306 - MySQL database
- localhost:5432 - PostgreSQL database
- localhost:6379 - Redis cache