Installing MongoDB on Windows, macOS, and Linux

MongoDB is one of the most popular NoSQL databases, known for its flexibility, scalability, and ease of use. Whether you're a developer setting up a local environment or a system administrator deploying MongoDB on servers, this guide will walk you through the installation process on Windows, macOS, and Linux.

Table of Contents

  1. Installing MongoDB on Windows

  2. Installing MongoDB on macOS

  3. Installing MongoDB on Linux

  4. Verifying Your Installation

  5. Basic MongoDB Commands

1. Installing MongoDB on Windows

Method 1: MongoDB Installer (Recommended)

  1. Download the installer from the official MongoDB website:
    https://www.mongodb.com/try/download/community

  2. Run the installer (.msi file) and follow the setup wizard:

    • Choose "Complete" setup type for all features

    • Uncheck "Install MongoDB Compass" if you don't need the GUI (it can be installed separately)

    • Click "Install"

  3. Configure MongoDB as a service (optional but recommended):

    • During installation, you can configure MongoDB to run as a Windows service

    • Alternatively, you can configure it manually after installation

  4. Add MongoDB to your PATH (optional):

    • MongoDB is typically installed in
      C:\Program Files\MongoDB\Server\<version>\bin

    • Add this path to your system's PATH environment variable for easier command line access

Method 2: Using Chocolatey (Package Manager)

If you have Chocolatey installed, run:

choco install mongodb

2. Installing MongoDB on macOS

Method 1: Using Homebrew (Recommended)

  1. Install Homebrew (if you don't have it):

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

brew tap mongodb/brew
brew install mongodb-community
  1. Start MongoDB service:

brew services start mongodb-community

Method 2: Manual Installation

  1. Download the macOS package from:
    https://www.mongodb.com/try/download/community

  2. Extract the archive:

tar -zxvf mongodb-macos-x86_64-6.0.5.tgz
  1. Move to standard location:

mkdir -p /usr/local/mongodb
mv mongodb-macos-x86_64-6.0.5/* /usr/local/mongodb/
  1. Add to PATH:
    Add the following to your shell configuration file (~/.zshrc or ~/.bashrc):

export PATH=/usr/local/mongodb/bin:$PATH

3. Installing MongoDB on Linux

For Ubuntu/Debian

  1. Import the public key:

sudo apt-get install gnupg
curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg --dearmor
  1. Create the list file:

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
  1. Update packages and install MongoDB:

sudo apt-get update
sudo apt-get install -y mongodb-org
  1. Start MongoDB service:

sudo systemctl start mongod
sudo systemctl enable mongod

For RHEL/CentOS/Fedora

  1. Create repo file:

sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo <<EOF
[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-6.0.asc
EOF
  1. Install MongoDB:

sudo yum install -y mongodb-org
  1. Start MongoDB service:

sudo systemctl start mongod
sudo systemctl enable mongod

4. Verifying Your Installation

After installation, verify MongoDB is running:

  1. Connect to MongoDB shell:

mongosh
  1. Run a test command:

db.runCommand({ping: 1})

You should see:

{ "ok" : 1 }
  1. Exit the shell:

exit

5. Basic MongoDB Commands

Here are some essential commands to get started:

  • Show databases:

show dbs
  • Create or switch database:

use mydatabase
  • Create collection and insert document:

db.users.insertOne({name: "John", age: 30})
  • Find documents:

db.users.find()
  • Update document:

db.users.updateOne({name: "John"}, {$set: {age: 31}})
  • Delete document:

db.users.deleteOne({name: "John"})

Conclusion

You now have MongoDB installed on your system. The installation process varies slightly between operating systems, but the core functionality remains the same. MongoDB's flexibility makes it a great choice for various applications, from small projects to large-scale enterprise systems.

For production environments, be sure to configure proper authentication and security settings, which are not covered in this basic installation guide.

Happy coding with MongoDB!

Comments

Popular posts from this blog

What is MongoDB? A Beginner's Guide to the Leading NoSQL Database