MongoDB Installation

Complete installation guide for all platforms

⚙️ Installing MongoDB

Learn how to install MongoDB Community Edition on Windows, macOS, and Linux. Follow step-by-step instructions to set up your local development environment quickly and correctly.


# Verify installation
mongod --version
                                    

Output:

db version v7.0.0
Build Info: {
    "version": "7.0.0"
}

Installation Options

Choose the installation method that best suits your operating system and development needs. Each platform has specific requirements and recommended approaches for optimal MongoDB setup.

🪟

Windows

MSI installer with GUI setup

Easy Setup Service
🍎

macOS

Homebrew package manager

Homebrew Quick
🐧

Linux

Package manager installation

APT/YUM Systemd
🐳

Docker

Containerized MongoDB

Portable Isolated

🔹 Windows Installation

Install MongoDB on Windows using the official MSI installer:

Step-by-Step Guide:

  1. Download MongoDB Community Server from mongodb.com/try/download/community
  2. Run the .msi installer file
  3. Choose "Complete" installation type
  4. Select "Install MongoDB as a Service"
  5. Keep default data and log directories
  6. Optionally install MongoDB Compass (GUI tool)
  7. Complete the installation

🔸 Start MongoDB Service

# Start MongoDB service
net start MongoDB

# Stop MongoDB service
net stop MongoDB

# Check service status
sc query MongoDB

🔸 Connect to MongoDB

# Open Command Prompt or PowerShell
mongosh

# Or specify connection
mongosh "mongodb://localhost:27017"

Output:

Current Mongosh Log ID: 6a7b8c9d0e1f2a3b4c5d6e7f
Connecting to: mongodb://127.0.0.1:27017
Using MongoDB: 7.0.0
test>

🔸 Add to PATH (if needed)

# Default installation path
C:\Program Files\MongoDB\Server\7.0\bin

# Add to System Environment Variables > Path
# Then restart Command Prompt

🔹 macOS Installation

Install MongoDB on macOS using Homebrew package manager:

🔸 Install Homebrew (if not installed)

# Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Verify Homebrew
brew --version

🔸 Install MongoDB

# Tap MongoDB formula
brew tap mongodb/brew

# Install MongoDB Community Edition
brew install [email protected]

# Verify installation
mongod --version

Output:

db version v7.0.0

🔸 Start MongoDB

# Start MongoDB as a service
brew services start [email protected]

# Stop MongoDB
brew services stop [email protected]

# Restart MongoDB
brew services restart [email protected]

# Check status
brew services list

🔸 Connect to MongoDB

# Connect using mongosh
mongosh

# Or run MongoDB manually (without service)
mongod --config /usr/local/etc/mongod.conf

🔹 Linux Installation (Ubuntu/Debian)

Install MongoDB on Ubuntu or Debian-based Linux distributions:

🔸 Import MongoDB GPG Key

# Import public key
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
   --dearmor

🔸 Add MongoDB Repository

# Ubuntu 22.04 (Jammy)
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

# Update package list
sudo apt-get update

🔸 Install MongoDB

# Install MongoDB packages
sudo apt-get install -y mongodb-org

# Verify installation
mongod --version

🔸 Start MongoDB Service

# Start MongoDB
sudo systemctl start mongod

# Enable MongoDB to start on boot
sudo systemctl enable mongod

# Check status
sudo systemctl status mongod

# Stop MongoDB
sudo systemctl stop mongod

# Restart MongoDB
sudo systemctl restart mongod

Output:

● mongod.service - MongoDB Database Server
     Loaded: loaded
     Active: active (running)

🔸 Connect to MongoDB

# Connect using mongosh
mongosh

🔹 Docker Installation

Run MongoDB in a Docker container for easy setup and portability:

🔸 Pull MongoDB Image

# Pull latest MongoDB image
docker pull mongo

# Pull specific version
docker pull mongo:7.0

🔸 Run MongoDB Container

# Run MongoDB container
docker run -d \
  --name mongodb \
  -p 27017:27017 \
  -v mongodb_data:/data/db \
  mongo:7.0

# With authentication
docker run -d \
  --name mongodb \
  -p 27017:27017 \
  -e MONGO_INITDB_ROOT_USERNAME=admin \
  -e MONGO_INITDB_ROOT_PASSWORD=password \
  -v mongodb_data:/data/db \
  mongo:7.0

🔸 Manage Container

# Connect to MongoDB shell
docker exec -it mongodb mongosh

# Stop container
docker stop mongodb

# Start container
docker start mongodb

# View logs
docker logs mongodb

# Remove container
docker rm mongodb

🔹 Install MongoDB Shell (mongosh)

Install the MongoDB Shell separately for connecting to databases:

🔸 Windows

# Download from mongodb.com/try/download/shell
# Run the .msi installer
# Verify installation
mongosh --version

🔸 macOS

# Install using Homebrew
brew install mongosh

# Verify installation
mongosh --version

🔸 Linux

# Download and install
wget https://downloads.mongodb.com/compass/mongosh-2.0.0-linux-x64.tgz
tar -zxvf mongosh-2.0.0-linux-x64.tgz
sudo cp mongosh-2.0.0-linux-x64/bin/mongosh /usr/local/bin/

# Verify installation
mongosh --version

🔹 Verify Installation

Test that MongoDB is installed and running correctly:

// Connect to MongoDB
mongosh

// Check version
db.version()

// Create test database
use testDB

// Insert test document
db.test.insertOne({ message: "Installation successful!" })

// Find document
db.test.find()

// Show databases
show dbs

Output:

7.0.0
switched to db testDB
{ acknowledged: true, insertedId: ObjectId("...") }
{ _id: ObjectId("..."), message: "Installation successful!" }
admin    0.000GB
config   0.000GB
local    0.000GB
testDB   0.000GB

🔹 Configuration Files

MongoDB configuration file locations by platform:

Default Config Locations:

  • Windows: C:\Program Files\MongoDB\Server\7.0\bin\mongod.cfg
  • macOS: /usr/local/etc/mongod.conf
  • Linux: /etc/mongod.conf

Default Data Directory:

  • Windows: C:\Program Files\MongoDB\Server\7.0\data
  • macOS: /usr/local/var/mongodb
  • Linux: /var/lib/mongodb

🧠 Test Your Knowledge

What is the default port for MongoDB?