MongoDB is a popular NoSQL database that stores data in flexible, JSON-like documents. This tutorial will walk you through the installation and setup process for MongoDB on various operating systems, including Windows, macOS, and Linux. We'll also cover some best practices to ensure your MongoDB instance is secure and optimized.
Before installing MongoDB, ensure your system meets the following requirements:
Download MongoDB:
.msi) for a standard installation or the zip file for a custom installation.Install MongoDB:
.msi file and follow the installation wizard.Configure Environment Variables:
C:\Program Files\MongoDB\Server\<version>\bin to your system's PATH environment variable. This allows you to run MongoDB commands from any command prompt.Create Data Directory:
C:\data\db.Start MongoDB Server:
mongod --dbpath "C:\data\db"
Connect to MongoDB:
mongo
Install Homebrew (if not already installed):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install MongoDB:
brew tap mongodb/brew
brew install mongodb-community@6.0
Start MongoDB Server:
brew services start mongodb-community@6.0
mongod --config /usr/local/etc/mongod.conf
Connect to MongoDB:
mongo
Import the Public Key:
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
Create a List File for MongoDB:
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
Reload the Package Database:
sudo apt-get update
Install MongoDB:
sudo apt-get install -y mongodb-org
Start MongoDB Server:
sudo systemctl start mongod
sudo systemctl enable mongod
Connect to MongoDB:
mongo
Enable Authentication:
mongod.conf file to enable authentication:
security:
authorization: "enabled"
Create Admin User:
use admin
db.createUser({
user: "admin",
pwd: "password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
Secure Bind IP:
net:
bindIp: 127.0.0.1
Adjust WiredTiger Cache Size:
mongod.conf:
storage:
wiredTiger:
engineConfig:
cacheSizeGB: 2
Enable Journaling:
storage:
journal:
enabled: true
Monitor and Tune Performance:
MongoDB provides a flexible and powerful platform for storing and managing unstructured data. By following the installation steps outlined in this tutorial, you can set up a MongoDB instance on your preferred operating system. Remember to apply best practices for security and performance optimization to ensure your MongoDB deployment is robust and efficient.
This completes the "Installation and Setup" section of our MongoDB course. Next, we will explore the basics of working with data in MongoDB.