MongoDB Installation & Setup Guide
Complete step-by-step guide to install and configure MongoDB on your computer. Choose between local installation or cloud setup with MongoDB Atlas.
You have two options to get MongoDB running:
Option 1: Local Installation
Install MongoDB directly on your computer
Pros:
- Works offline
- Faster for local development
- More control over configuration
Cons:
- Takes up disk space
- Requires installation steps
- Need to manage updates
Option 2: MongoDB Atlas (Cloud)
Use MongoDB in the cloud (free tier available)
Pros:
- No installation needed
- Access from anywhere
- Automatic backups
- Production-ready
Cons:
- Requires internet connection
- Free tier has storage limits (512MB)
Recommendation for Beginners
Start with MongoDB Atlas (Cloud) - it's easier and gets you up and running in 5 minutes. You can always install locally later if needed!
Follow the instructions for your operating system:
Step 1: Download MongoDB Community Server
- Visit mongodb.com/try/download/community
- Select the latest stable version (7.0 or higher)
- Choose your operating system
- Download the installer
Step 2: Install MongoDB
Windows Installation:
- Double-click the downloaded MSI file
- Click "Next" on the welcome screen
- Accept the license agreement
- Choose "Complete" installation
- Service Configuration Screen:
- ✓ Keep "Install MongoDB as a Service" checked
- ✓ Select "Run service as Network Service user"
- Service Name:
MongoDB - Data Directory: Keep default
- Log Directory: Keep default
- Click "Next"
- MongoDB Compass (optional):
- Keep checked if you want a visual interface
- Or uncheck to skip
- Click "Install" and wait
- Click "Finish"
Adding MongoDB to PATH (Windows)
- Search for "Environment Variables" in Windows
- Click "Edit the system environment variables"
- Click "Environment Variables" button
- Under "System variables", find and select Path
- Click "Edit"
- Click "New" and add:textC:\Program Files\MongoDB\Server\7.0\bin
- Click "OK" on all windows
- Restart your terminal
macOS Installation:
Using Homebrew (easiest method):
# Install Homebrew if you don't have it/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"# Add MongoDB tapbrew tap mongodb/brew# Install MongoDBbrew install mongodb-community@7.0# Start MongoDB servicebrew services start mongodb-community@7.0
Linux (Ubuntu/Debian):
# Import MongoDB public keywget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -# Add MongoDB repositoryecho "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list# Update package listsudo apt-get update# Install MongoDBsudo apt-get install -y mongodb-org# Start MongoDB servicesudo systemctl start mongod# Enable MongoDB to start on bootsudo systemctl enable mongod
Follow these steps to set up a free cloud database in minutes:
Step 1: Create an Account
- Go to cloud.mongodb.com
- Click "Sign Up"
- Sign up with email or use Google/GitHub
- Verify your email if required
Step 2: Create a Free Cluster
- After logging in, click "Build a Database"
- Select M0 FREE tier (512MB storage)
- Choose a cloud provider (AWS, Google Cloud, or Azure)
- Select a region close to you
- Name your cluster (or keep default "Cluster0")
- Click "Create Cluster"
Step 3: Security Setup
You'll see a Security Quickstart screen:
- Create Database User:
- Username:
myuser(or your choice) - Password: Click "Autogenerate Secure Password"
- SAVE THIS PASSWORD! Copy it somewhere safe
- Click "Create User"
- Username:
- Network Access:
- Choose "My Local Environment"
- Click "Add My Current IP Address"
- Or click "Allow Access from Anywhere" (0.0.0.0/0) for learning
- Click "Finish and Close"
What if I see "Service Config" screen?
Sometimes after clicking "Finish and Close", you might see additional options:
- Deployment Type: Leave as "Cluster"
- Cloud Provider & Region: Already set
- Cluster Tier: Should show "M0 Sandbox (Free)"
- Additional Settings: Leave everything as default
- Just click "Create Cluster" or "Confirm"
Wait 3-5 minutes for your cluster to deploy.
Step 4: Get Your Connection String
- Once cluster is ready, click "Connect" button
- Select "Connect your application"
- Driver: Choose "Node.js"
- Version: Select latest
- Copy the connection string
- Replace
<password>with your actual password - Replace
myFirstDatabasewith your database name (e.g.,schoolDB)
Example connection string:
mongodb+srv://myuser:mypassword@cluster0.xxxxx.mongodb.net/schoolDB?retryWrites=true&w=majority
Success!
Your MongoDB Atlas cluster is ready! You can now connect to it from your applications using the connection string.
Let's make sure everything is working correctly:
For Local Installation:
Open your terminal and run:
# Check MongoDB Shell versionmongosh --version# Connect to local MongoDBmongosh# You should see a connection message and a prompt like:# test>
If you see the MongoDB shell prompt, congratulations! MongoDB is installed correctly.
For MongoDB Atlas:
Test your connection using mongosh:
# Connect to your Atlas cluster (replace with your connection string)mongosh "mongodb+srv://myuser:mypassword@cluster0.xxxxx.mongodb.net/schoolDB"# You should see a connection success message
Quick Test:
Once connected, try these commands:
// Show all databasesshow dbs// Create a test databaseuse testDB// Insert a test documentdb.test.insertOne({ message: "Hello MongoDB!" })// Find the documentdb.test.find()// You should see your inserted document!
All Set!
Now proceed to the MongoDB Basics lesson to start learning how to use MongoDB!