MongoDB – Installation
MongoDB Installation Guide
MongoDB can be installed on various operating systems such as Windows, macOS, and Linux. Below is a step-by-step guide for installing MongoDB on Windows, macOS, and Linux.
1. MongoDB Installation on Windows
Step 1: Download MongoDB
- Go to the MongoDB Download Center.
- Select the Windows operating system.
- Choose the version (e.g., Current Stable version).
- Select the package type as MSI (Windows Installer).
- Click Download.
Step 2: Install MongoDB
- Once the MSI file is downloaded, double-click to run the installer.
- Follow the on-screen instructions.
- In the Setup Type screen, select Complete for a full installation.
- Ensure the Install MongoDB as a Service option is checked (this will install MongoDB as a service that starts automatically).
Step 3: Add MongoDB to the PATH Environment Variable
- After installation, add MongoDB’s bin folder to the system’s PATH variable:
- Open the Environment Variables settings (Right-click on This PC -> Properties -> Advanced System Settings -> Environment Variables).
- Under System variables, find Path, click Edit, and then New. Add the following path:
C:\Program Files\MongoDB\Server\\bin
Step 4: Create the Data Directory
- MongoDB stores data in a folder called data. By default, it looks for data in C:\data\db. You can create this folder manually:
- Open Command Prompt and run the following command: mkdir C:\data\db
Step 5: Start MongoDB
- Open Command Prompt and run: mongod
- This starts the MongoDB server.
Step 6: Verify the Installation
- In another Command Prompt window, type: mongo
- This opens the MongoDB shell, and if everything is set up correctly, you will be connected to your MongoDB instance.
2. MongoDB Installation on macOS
Step 1: Install Homebrew (if not already installed)
- If you don’t have Homebrew installed on your macOS, open the Terminal and run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 2: Install MongoDB
- Use Homebrew to install MongoDB:
brew tap mongodb/brew brew install mongodb-community
Step 3: Start MongoDB
- To start MongoDB as a background service, run:
brew services start mongodb/brew/mongodb-community
Step 4: Verify the Installation
- To verify that MongoDB is running, open another Terminal window and run : mongo
- This will open the MongoDB shell, and you should be connected to your MongoDB instance.
3. MongoDB Installation on Linux (Ubuntu)
Step 1: Import MongoDB Public Key
- Open the Terminal and run the following command to import the MongoDB public GPG key:
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
Step 2: Add MongoDB Repository
- Add the MongoDB repository to your system’s package list:
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
Step 3: Update the Package List
- Update the package list to include the MongoDB packages :
sudo apt-get update
Step 4: Install MongoDB
- Install the MongoDB server package:
sudo apt-get install -y mongodb-org
Step 5: Start MongoDB
- Start the MongoDB service:
sudo systemctl start mongod
- To enable MongoDB to start on boot:
sudo systemctl enable mongod
Step 6: Verify the Installation
- Check the status of MongoDB to ensure it is running:
sudo systemctl status mongod
- To connect to MongoDB, type:
mongo
- This opens the MongoDB shell, and you should see the MongoDB prompt if it is running properly.
Summary of Installation Steps:
- Windows: Use the MSI installer to install MongoDB, configure the environment path, and start the service.
- macOS: Use Homebrew to install MongoDB and manage it as a background service.
- Linux (Ubuntu): Add MongoDB’s repository, install via apt, and start the service using systemctl.
Once MongoDB is installed and running, you can start working with collections, documents, and querying the database through the MongoDB shell or connect through a driver for your application.
Recent Comments