On this page
Installing MongoDB
1. Installation on Different Operating Systems
Windows
-
Download MongoDB:
- Visit the MongoDB Download Center and download the MSI installer for Windows.
-
Install MongoDB:
- Run the downloaded MSI file.
- Follow the setup wizard to install MongoDB.
- Select “Complete” setup type.
- Choose to install MongoDB as a service (recommended).
-
Add MongoDB to the PATH:
- Add the MongoDB bin directory (e.g.,
C:\Program Files\MongoDB\Server\X.Y\bin
) to your system’s PATH environment variable.
- Add the MongoDB bin directory (e.g.,
macOS
-
Install Homebrew:
- If you don’t have Homebrew installed, follow the instructions on Homebrew’s website.
-
Install MongoDB:
- Open Terminal and run:
brew tap mongodb/brew brew install mongodb-community
- Open Terminal and run:
-
Start MongoDB:
- Run:
brew services start mongodb-community
- Run:
Linux
-
Import MongoDB public GPG Key:
- Run:
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
- Run:
-
Create a List File:
- Run:
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs) mongodb-org 6.0" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
- Run:
-
Install MongoDB:
- Update package list:
sudo apt-get update
- Install MongoDB:
sudo apt-get install -y mongodb-org
- Update package list:
-
Start MongoDB:
- Run:
sudo systemctl start mongod
- Run:
-
Enable MongoDB to start on boot:
- Run:
sudo systemctl enable mongod
- Run:
2. Configuration and Basic Setup
-
Configuration File:
- The default configuration file is usually located at
/etc/mongod.conf
on Linux, orC:\Program Files\MongoDB\Server\X.Y\bin\mongod.cfg
on Windows. - Modify this file to set options like
bindIp
,port
, anddbPath
.
- The default configuration file is usually located at
-
Basic Setup:
- You can configure settings like security, storage engines, and replication in the configuration file.
- For example, to bind MongoDB to localhost, set
bindIp: 127.0.0.1
inmongod.conf
.
3. Starting and Stopping MongoDB Service
-
Windows:
- Start MongoDB:
- Use the Services application (search “Services” in Start Menu) to start the MongoDB service.
- Stop MongoDB:
- Similarly, stop the MongoDB service using the Services application.
- Start MongoDB:
-
macOS/Linux:
- Start MongoDB:
- Run:
sudo systemctl start mongod # Linux brew services start mongodb-community # macOS
- Run:
- Stop MongoDB:
- Run:
sudo systemctl stop mongod # Linux brew services stop mongodb-community # macOS
- Run:
- Start MongoDB:
4. MongoDB Compass Overview (GUI Tool)
-
Download and Install:
- Visit the MongoDB Compass Download Page and download the appropriate version for your OS.
- Follow the installation instructions.
-
Features:
- Data Visualization: View and interact with your data through a graphical interface.
- Query Builder: Build and execute queries using an intuitive GUI.
- Schema Exploration: Analyze and visualize your data schema.
- Performance Monitoring: Monitor database performance metrics.
-
Basic Usage:
- Open MongoDB Compass and connect to your MongoDB server using the connection URI (e.g.,
mongodb://localhost:27017
). - Explore databases, collections, and documents.
- Use the built-in tools to run queries, view performance statistics, and manage indexes.
- Open MongoDB Compass and connect to your MongoDB server using the connection URI (e.g.,