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
-
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.
-
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.
- Locate the downloaded
-
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.
-
Finish Installation:
- Complete the installation process by clicking the "Install" button.
- Once the installation is complete, click "Finish."
Practical Example
Installing MongoDB on macOS
Step-by-Step Guide
-
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)"
- If you don't have Homebrew installed, open Terminal and run:
-
Install MongoDB:
- Use Homebrew to install MongoDB by running:
brew tap mongodb/brew brew install [email protected]
- Use Homebrew to install MongoDB by running:
-
Start MongoDB:
- To start MongoDB as a service, run:
brew services start mongodb/brew/mongodb-community
- To start MongoDB as a service, run:
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
-
Import the Public Key:
- Open Terminal and run:
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
- Open Terminal and run:
-
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
- Create the
-
Reload Local Package Database:
- Run the following command to reload the local package database:
sudo apt-get update
- Run the following command to reload the local package database:
-
Install MongoDB Packages:
- Install the MongoDB packages by running:
sudo apt-get install -y mongodb-org
- Install the MongoDB packages by running:
-
Start MongoDB:
- Start MongoDB by running:
sudo systemctl start mongod
- Start MongoDB by running:
Practical Example
Verifying the Installation
Step-by-Step Guide
-
Check MongoDB Version:
- Open your terminal or command prompt and run:
mongod --version
- Open your terminal or command prompt and run:
-
Connect to MongoDB:
- Start the MongoDB shell by running:
mongo
- You should see a prompt indicating that you are connected to a MongoDB instance.
- Start the MongoDB shell by running:
Practical Example
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.