π MongoDB Connection Guide
Complete guide to connecting to Local MongoDB and Cloud (Atlas) with connection strings and examples
Connection Overview
Local vs Cloud
- Full control over configuration
- No internet required after setup
- Great for development and testing
- You manage backups and maintenance
- Default:
localhost:27017
- Automatic backups and scaling
- High availability out-of-the-box
- Free tier available (512 MB)
- Global cloud deployment
- Built-in security and monitoring
- Local: Development, learning, testing, full control needed
- Atlas: Production apps, need scalability, want managed service
- Both: Local for dev, Atlas for production (common setup!)
Connecting to Local MongoDB
Using mongosh and drivers
Make sure MongoDB is running on your machine:
The default local connection string:
show dbs
If you've enabled authentication:
MongoDB Atlas Setup
Cloud database in minutes
- Visit MongoDB Atlas
- Sign up with email or Google/GitHub
- Verify your email address
- Complete the welcome survey (optional)
- Click "Build a Database"
- Choose "M0 Free" tier (512 MB storage)
- Select your cloud provider (AWS, Google Cloud, or Azure)
- Choose the region closest to you
- Name your cluster (e.g., "Cluster0")
- Click "Create" (takes 1-3 minutes)
- 512 MB storage
- Shared RAM
- No credit card required
- Perfect for learning and small projects
A. Create Database User:
- Go to Database Access in left menu
- Click "Add New Database User"
- Choose Password authentication
- Create username and strong password
- Set privileges to "Read and write to any database"
- Click "Add User"
B. Whitelist IP Address:
- Go to Network Access in left menu
- Click "Add IP Address"
- For development: Click "Allow Access from Anywhere" (0.0.0.0/0)
- For production: Add your specific IP
- Click "Confirm"
For production apps, only whitelist specific IPs. "Allow from Anywhere" (0.0.0.0/0) should only be used for development/testing.
- Go to your Database page
- Click "Connect" button on your cluster
- Choose "Connect with MongoDB Shell" or "Connect your application"
- Copy the connection string
- Replace
<password>with your actual password
- mongodb+srv:// instead of
mongodb://(SRV record) - Cluster domain:
cluster0.xxxxx.mongodb.net - Includes authentication and options
- Automatic SSL/TLS encryption
Connection String Anatomy
Understanding the format
| Component | Description | Example |
|---|---|---|
mongodb:// |
Protocol (standard) | mongodb:// |
mongodb+srv:// |
Protocol (DNS SRV - Atlas) | mongodb+srv:// |
username |
Database user (optional) | myuser |
password |
User password (optional) | secret123 |
host |
Server hostname or IP | localhost or cluster0.mongodb.net |
port |
Port number (optional, default: 27017) | 27017 |
database |
Default database (optional) | myapp |
options |
Query parameters (optional) | retryWrites=true&w=majority |
Connect from Code
Popular programming languages
Troubleshooting
Common connection issues
Error: MongoServerError: connect ECONNREFUSED 127.0.0.1:27017
Solutions:
- Make sure MongoDB server is running
- Check if port 27017 is in use:
lsof -i :27017 - Verify MongoDB status:
- macOS:
brew services list - Linux:
sudo systemctl status mongod - Windows: Check Services or Task Manager
- macOS:
- Check firewall settings
Error: MongoServerError: Authentication failed
Solutions:
- Verify username and password are correct
- Check you're connecting to the right authentication database (usually
admin) - For Atlas: Make sure you replaced
<password>in connection string - Special characters in password? URL encode them:
@becomes%40:becomes%3A/becomes%2F
Error: MongoServerSelectionError: connection timeout
Solutions:
- IP Whitelist: Make sure your IP is whitelisted in Atlas Network Access
- Firewall: Check your local firewall isn't blocking port 27017
- VPN: Some VPNs block MongoDB connections - try without VPN
- Connection String: Verify you're using
mongodb+srv://(notmongodb://) - Cluster Running: Ensure your Atlas cluster is active (not paused)
Error: unable to verify the first certificate
Solution:
Add SSL options to connection string:
sslValidate=false in development. In production, use proper SSL certificates.