MongoDB Installation

This chapter details how to install and configure MongoDB on different operating systems, including both Community and Enterprise editions.

System Requirements

Minimum Requirements

  • Operating System: Windows 10/11, macOS 10.14+, Linux (Ubuntu 18.04+, CentOS 7+)
  • Memory: At least 4GB RAM (8GB+ recommended)
  • Disk Space: At least 10GB available space
  • Processor: 64-bit processor

Windows Installation

Method 1: Using MSI Installer

  1. Download Installer

  2. Run Installer

    1. Double-click the downloaded .msi file
    2. Select "Complete" installation
    3. Check "Install MongoD as a Service"
    4. Check "Install MongoDB Compass" (optional GUI tool)
    5. Click Install to complete
  3. Configure Environment Variables

    # Add MongoDB bin directory to PATH
    C:\Program Files\MongoDB\Server\7.0\bin

Method 2: Using Chocolatey

# Run PowerShell as Administrator
choco install mongodb

macOS Installation

# Update Homebrew
brew update

# Install MongoDB
brew tap mongodb/brew
brew install mongodb-community

# Start MongoDB service
brew services start mongodb-community

# Stop service
brew services stop mongodb-community

Method 2: Manual Installation

# Download and extract
curl -O https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-7.0.tgz
tar -zxvf mongodb-macos-x86_64-7.0.tgz

# Move to /usr/local
sudo mv mongodb-macos-x86_64-7.0 /usr/local/mongodb

# Add to PATH
echo 'export PATH=/usr/local/mongodb/bin:$PATH' >> ~/.zshrc
source ~/.zshrc

Linux Installation (Ubuntu/Debian)

# 1. Import public key
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -

# 2. Create list file
echo "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

# 3. Update package list
sudo apt-get update

# 4. Install MongoDB
sudo apt-get install -y mongodb-org

# 5. Start service
sudo systemctl start mongod

# 6. Enable auto-start
sudo systemctl enable mongod

# 7. Check status
sudo systemctl status mongod

Linux Installation (CentOS/RHEL)

# 1. Create repository file
sudo vi /etc/yum.repos.d/mongodb-org-7.0.repo

Add the following content:

[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc
# 2. Install MongoDB
sudo yum install -y mongodb-org

# 3. Start service
sudo systemctl start mongod

# 4. Enable auto-start
sudo systemctl enable mongod

Docker Installation

# Pull official image
docker pull mongo:7.0

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

# Enter container
docker exec -it mongodb mongosh

Verify Installation

Check Version

mongod --version

Start MongoDB Shell

mongosh

Basic Command Test

// Show all databases
show dbs

// Switch to test database
use test

// Insert test data
db.users.insertOne({ name: "John", age: 25 })

// Query data
db.users.find()

// Exit
exit

Configuration File

Configuration File Locations

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

Common Configuration Options

# Storage configuration
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true

# Network configuration
net:
  port: 27017
  bindIp: 127.0.0.1  # Change to 0.0.0.0 for remote access

# Security configuration
security:
  authorization: enabled  # Enable authentication

# Logging configuration
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Process management
processManagement:
  fork: true
  pidFilePath: /var/run/mongodb/mongod.pid

Common Issues

1. Port Already in Use

# Find process using port 27017
sudo lsof -i :27017

# Or change MongoDB port
# Modify net.port in configuration file

2. Permission Issues (Linux)

# Fix data directory permissions
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown -R mongodb:mongodb /var/log/mongodb

3. Connection Refused

# Check firewall settings
sudo ufw allow 27017

# Check MongoDB service status
sudo systemctl status mongod

Installing MongoDB Compass (GUI Tool)

MongoDB Compass is the official GUI tool that supports visual querying, index management, data import/export, and more.

Download and Install

Connect to Database

  1. Open Compass
  2. Enter connection string: mongodb://localhost:27017
  3. Click Connect

Summary

This chapter covered multiple MongoDB installation methods:

  • Windows: MSI installer or Chocolatey
  • macOS: Homebrew or manual installation
  • Linux: Package manager installation
  • Docker: Containerized deployment

Choose the installation method suitable for your environment and complete MongoDB installation and basic configuration.

In the next chapter, we will learn about MongoDB Concepts.