In this section, we will cover the steps required to install MongoDB on different operating systems. By the end of this section, you will have MongoDB up and running on your machine.

Table of Contents

Installing MongoDB on Windows

Step-by-Step Guide

  1. Download MongoDB Installer:

    • Go to the MongoDB Download Center.
    • Select the version suitable for your Windows operating system.
    • Click on the download button to get the .msi installer.
  2. Run the Installer:

    • Locate the downloaded .msi file and double-click to run the installer.
    • Follow the installation wizard steps:
      • Accept the End-User License Agreement (EULA).
      • Choose the "Complete" setup type.
  3. Service Configuration:

    • Ensure that the "Install MongoDB as a Service" option is checked.
    • Set the service name (default is MongoDB).
    • Choose the "Run service as Network Service user" option.
  4. Finish Installation:

    • Complete the installation process by clicking the "Install" button.
    • Once the installation is complete, click "Finish."

Practical Example

# To start MongoDB service
net start MongoDB

# To stop MongoDB service
net stop MongoDB

Installing MongoDB on macOS

Step-by-Step Guide

  1. Install Homebrew:

    • If you don't have Homebrew installed, open Terminal and run:
      /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
      
  2. Install MongoDB:

    • Use Homebrew to install MongoDB by running:
      brew tap mongodb/brew
      brew install [email protected]
      
  3. Start MongoDB:

    • To start MongoDB as a service, run:
      brew services start mongodb/brew/mongodb-community
      

Practical Example

# To start MongoDB
brew services start mongodb/brew/mongodb-community

# To stop MongoDB
brew services stop mongodb/brew/mongodb-community

Installing MongoDB on Linux

Step-by-Step Guide

  1. Import the Public Key:

    • Open Terminal and run:
      wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
      
  2. Create a List File:

    • Create the /etc/apt/sources.list.d/mongodb-org-5.0.list file for Ubuntu:
      echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
      
  3. Reload Local Package Database:

    • Run the following command to reload the local package database:
      sudo apt-get update
      
  4. Install MongoDB Packages:

    • Install the MongoDB packages by running:
      sudo apt-get install -y mongodb-org
      
  5. Start MongoDB:

    • Start MongoDB by running:
      sudo systemctl start mongod
      

Practical Example

# To start MongoDB
sudo systemctl start mongod

# To stop MongoDB
sudo systemctl stop mongod

Verifying the Installation

Step-by-Step Guide

  1. Check MongoDB Version:

    • Open your terminal or command prompt and run:
      mongod --version
      
  2. Connect to MongoDB:

    • Start the MongoDB shell by running:
      mongo
      
    • You should see a prompt indicating that you are connected to a MongoDB instance.

Practical Example

# Check MongoDB version
mongod --version

# Start MongoDB shell
mongo

Conclusion

In this section, we covered the installation of MongoDB on Windows, macOS, and Linux. We also learned how to verify the installation to ensure MongoDB is running correctly. With MongoDB installed, you are now ready to move on to the basics of MongoDB in the next section.

© Copyright 2024. All rights reserved