Installing MongoDB on different operating systems
To install MongoDB on different operating systems, follow the steps for your specific OS. MongoDB provides official packages and instructions for popular platforms.
1. Installing MongoDB on Ubuntu (Linux)
Step 1: Import the MongoDB GPG Key
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
Step 2: Create MongoDB Source List
echo "deb [ arch=amd64,arm64 ] 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
Step 3: Update Package Database
sudo apt update
Step 4: Install MongoDB
sudo apt install -y mongodb-org
Step 5: Start and Enable MongoDB Service
sudo systemctl start mongod
sudo systemctl enable mongod
Step 6: Verify Installation
mongod --version
Step 7: Connect to MongoDB
mongo
2. Installing MongoDB on CentOS / RHEL (Linux)
Step 1: Create a MongoDB Repo File
bashcat <<EOF | sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo
[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
EOF
Step 2: Install MongoDB
sudo yum install -y mongodb-org
Step 3: Start and Enable MongoDB Service
sudo systemctl start mongod
sudo systemctl enable mongod
Step 4: Verify Installation
mongod --version
Step 5: Connect to MongoDB
mongo
3. Installing MongoDB on Windows
Step 1: Download MongoDB Installer
- Go to the MongoDB Download Center.
- Select Windows, choose the correct version (usually the latest), and click Download.
Step 2: Run the Installer
- Double-click the downloaded
.msi
file to start the installation. - Select Complete setup to install MongoDB Server, Database Tools, and MongoDB Compass (GUI tool).
Step 3: Configure MongoDB
- Choose the option to Install MongoDB as a Service.
- Select Run MongoDB as a Network Service User or Local User based on your preference.
Step 4: Complete Installation
- After installation, MongoDB will be running as a service. You can manage it through Windows Services or use the command prompt.
Step 5: Verify Installation
- Open Command Prompt and run:
mongod --version
Step 6: Connect to MongoDB
- Open Command Prompt and type:
mongo
4. Installing MongoDB on macOS
Step 1: Install Homebrew (if not installed)
- Open the terminal and run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 2: Tap MongoDB Formula
brew tap mongodb/brew
Step 3: Install MongoDB
brew install mongodb-community@6.0
Step 4: Start MongoDB Service
brew services start mongodb/brew/mongodb-community
Step 5: Verify Installation
mongod --version
Step 6: Connect to MongoDB
mongo
5. Installing MongoDB on Docker (Cross-Platform)
Step 1: Install Docker
- Install Docker on your system by downloading it from Docker’s website.
Step 2: Pull MongoDB Docker Image
docker pull mongo
Step 3: Run MongoDB Container
docker run -d -p 27017:27017 --name mongodb mongo
Step 4: Verify Container is Running
docker ps
Step 5: Connect to MongoDB
docker exec -it mongodb mongo
Post-Installation Tips
- MongoDB Compass: For a GUI, MongoDB Compass can be installed (especially useful for Windows and macOS users).
- MongoDB Configuration File: The configuration file is typically located at
/etc/mongod.conf
(Linux) orC:\Program Files\MongoDB\Server\<version>\bin\mongod.cfg
(Windows) where you can set additional parameters like logging, network interfaces, etc. - Security: For production environments, ensure that authentication and role-based access control (RBAC) are configured for MongoDB.