Installing MongoDB on Windows, macOS, and Linux
MongoDB is one of the most popular NoSQL databases, known for its flexibility, scalability, and ease of use. Whether you're a developer setting up a local environment or a system administrator deploying MongoDB on servers, this guide will walk you through the installation process on Windows, macOS, and Linux.
Table of Contents
-
Installing MongoDB on Windows
-
Installing MongoDB on macOS
-
Installing MongoDB on Linux
-
Verifying Your Installation
-
Basic MongoDB Commands
1. Installing MongoDB on Windows
Method 1: MongoDB Installer (Recommended)
-
Download the installer from the official MongoDB website:
https://www.mongodb.com/try/download/community -
Run the installer (.msi file) and follow the setup wizard:
-
Choose "Complete" setup type for all features
-
Uncheck "Install MongoDB Compass" if you don't need the GUI (it can be installed separately)
-
Click "Install"
-
-
Configure MongoDB as a service (optional but recommended):
-
During installation, you can configure MongoDB to run as a Windows service
-
Alternatively, you can configure it manually after installation
-
-
Add MongoDB to your PATH (optional):
-
MongoDB is typically installed in
C:\Program Files\MongoDB\Server\<version>\bin
-
Add this path to your system's PATH environment variable for easier command line access
-
Method 2: Using Chocolatey (Package Manager)
If you have Chocolatey installed, run:
choco install mongodb
2. Installing MongoDB on macOS
Method 1: Using Homebrew (Recommended)
-
Install Homebrew (if you don't have it):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
Install MongoDB:
brew tap mongodb/brew
brew install mongodb-community
-
Start MongoDB service:
brew services start mongodb-community
Method 2: Manual Installation
-
Download the macOS package from:
https://www.mongodb.com/try/download/community -
Extract the archive:
tar -zxvf mongodb-macos-x86_64-6.0.5.tgz
-
Move to standard location:
mkdir -p /usr/local/mongodb
mv mongodb-macos-x86_64-6.0.5/* /usr/local/mongodb/
-
Add to PATH:
Add the following to your shell configuration file (~/.zshrc or ~/.bashrc):
export PATH=/usr/local/mongodb/bin:$PATH
3. Installing MongoDB on Linux
For Ubuntu/Debian
-
Import the public key:
sudo apt-get install gnupg
curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg --dearmor
-
Create the list file:
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
-
Update packages and install MongoDB:
sudo apt-get update
sudo apt-get install -y mongodb-org
-
Start MongoDB service:
sudo systemctl start mongod
sudo systemctl enable mongod
For RHEL/CentOS/Fedora
-
Create repo file:
sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo <<EOF
[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://pgp.mongodb.com/server-6.0.asc
EOF
-
Install MongoDB:
sudo yum install -y mongodb-org
-
Start MongoDB service:
sudo systemctl start mongod
sudo systemctl enable mongod
4. Verifying Your Installation
After installation, verify MongoDB is running:
-
Connect to MongoDB shell:
mongosh
-
Run a test command:
db.runCommand({ping: 1})
You should see:
{ "ok" : 1 }
-
Exit the shell:
exit
5. Basic MongoDB Commands
Here are some essential commands to get started:
-
Show databases:
show dbs
-
Create or switch database:
use mydatabase
-
Create collection and insert document:
db.users.insertOne({name: "John", age: 30})
-
Find documents:
db.users.find()
-
Update document:
db.users.updateOne({name: "John"}, {$set: {age: 31}})
-
Delete document:
db.users.deleteOne({name: "John"})
Conclusion
You now have MongoDB installed on your system. The installation process varies slightly between operating systems, but the core functionality remains the same. MongoDB's flexibility makes it a great choice for various applications, from small projects to large-scale enterprise systems.
For production environments, be sure to configure proper authentication and security settings, which are not covered in this basic installation guide.
Happy coding with MongoDB!
Comments
Post a Comment