How to Install MongoDB and MongoDB Compass on Windows, macOS, and Linux: A Complete Guide
Learn how to install MongoDB and MongoDB Compass on Windows, macOS, and Linux with our easy-to-follow guide. Perfect for Node.js developers looking to enhance their database skills.
Mon, Jul 8, 2024
4 min read
How to Install MongoDB and MongoDB Compass on Windows, macOS, and Linux
Introduction
MongoDB is a widely-used NoSQL database appreciated for its flexibility and scalability. MongoDB Compass is a GUI tool for MongoDB that provides a user-friendly interface for database management and visualization. In this guide, we’ll walk you through installing MongoDB and MongoDB Compass on Windows using a GUI method and on macOS and Linux using the command line. This guide is especially created to help you with your ongoing Node.js training. 🚀
Table of Contents
- Installing MongoDB on Windows (GUI)
- Installing MongoDB on macOS (CLI)
- Installing MongoDB on Linux (CLI)
- Installing MongoDB Compass
- Basic MongoDB Commands for Linux and macOS
Installing MongoDB on Windows (GUI)
Download the Installer:
- Visit the MongoDB Download Center.
- Select your Windows version and click "Download".
Run the Installer:
- Double-click the downloaded
.msi
file to launch the setup wizard. - Choose "Complete" setup type to install MongoDB with default options.
- Double-click the downloaded
Configure the Service:
- During installation, ensure "Install MongoDB as a Service" is selected.
- You can specify the data directory and log directory or leave them as default.
Complete Installation:
- Click "Next" and then "Install" to complete the installation process.
- Once finished, click "Finish" to exit the setup wizard.
Verify Installation:
- Open Command Prompt and run:
mongod --version
- You should see the version of MongoDB installed.
- Open Command Prompt and run:
Create Data Directory:
- MongoDB expects the data directory to be at
C:\data\db
. Create this directory:mkdir C:\data\db
- MongoDB expects the data directory to be at
Start MongoDB:
- MongoDB should start automatically as a service. You can also manually start it using:
net start MongoDB
- MongoDB should start automatically as a service. You can also manually start it using:
Installing MongoDB on macOS (CLI)
Install Homebrew:
- If Homebrew is not installed, you can install it by running:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- If Homebrew is not installed, you can install it by running:
Install MongoDB:
- Use Homebrew to install MongoDB by adding the MongoDB tap and installing:
brew tap mongodb/brew brew install mongodb-community@6.0
- Use Homebrew to install MongoDB by adding the MongoDB tap and installing:
Start MongoDB:
- Start the MongoDB service with:
brew services start mongodb/brew/mongodb-community
- Start the MongoDB service with:
Verify Installation:
- Check the installed version with:
mongod --version
- Check the installed version with:
Installing MongoDB on Linux (CLI)
Ubuntu/Debian
Import the Public Key:
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
Create a List File:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
Update Package List:
sudo apt-get update
Install MongoDB:
sudo apt-get install -y mongodb-org
Start MongoDB:
sudo systemctl start mongod
Enable MongoDB to Start on Boot:
sudo systemctl enable mongod
RHEL/CentOS
Create a Repository File:
echo "[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://www.mongodb.org/static/pgp/server-6.0.asc" | sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo
Install MongoDB:
sudo yum install -y mongodb-org
Start MongoDB:
sudo systemctl start mongod
Enable MongoDB to Start on Boot:
sudo systemctl enable mongod
Installing MongoDB Compass
Download MongoDB Compass:
- Go to the MongoDB Compass Download Page.
Install on Windows:
- Download the
.exe
file and run the installer. - Follow the setup wizard to install MongoDB Compass.
- Download the
Install on macOS:
- Download the
.dmg
file. - Open the
.dmg
file and drag MongoDB Compass to your Applications folder.
- Download the
Install on Linux:
- Download the
.deb
file for Debian-based distributions or the.rpm
file for RHEL-based distributions. - Use the appropriate package manager to install it:
- For
.deb
:sudo dpkg -i <path_to_deb_file>
- For
.rpm
:sudo rpm -ivh <path_to_rpm_file>
- For
- Download the
Basic MongoDB Commands for Linux and macOS
Start MongoDB Server
mongod
Connect to MongoDB
mongo
Create a Database
use myDatabase
Create a Collection
db.createCollection("myCollection")
Insert a Document
db.myCollection.insertOne({ name: "Alice", age: 25 })
Find Documents
db.myCollection.find()
Update a Document
db.myCollection.updateOne({ name: "Alice" }, { $set: { age: 26 } })
Delete a Document
db.myCollection.deleteOne({ name: "Alice" })
List Databases
show dbs
List Collections
show collections
Drop a Collection
db.myCollection.drop()
Drop a Database
db.dropDatabase()
Conclusion
By following this guide, you should now have MongoDB and MongoDB Compass installed on your system using the GUI on Windows and CLI on macOS and Linux. You can start exploring the powerful features of MongoDB for your development projects. If you run into any issues or have any questions, feel free to contact us. Happy coding! 😄