Installation and Basic Configuration
Introduction
MongoDB is a popular NoSQL database known for its high performance, scalability, and flexibility. This guide will walk you through the process of installing and configuring MongoDB on your system. Whether you are using Windows, macOS, or Linux, this step-by-step tutorial will help you get started with MongoDB.
Prerequisites
Before we begin, ensure you have the following:
Administrator access to your system
Basic knowledge of command-line interface
Installation
Windows
Step 1: Download MongoDB
Go to the MongoDB Download Center and select the MongoDB Community Server.
Choose the version compatible with your Windows OS and download the installer.
Step 2: Install MongoDB
Run the downloaded
.msifile.Follow the setup wizard, selecting the Complete setup type.
Choose to install MongoDB as a service.
Configure the Data and Log directories (default paths are usually sufficient).
Step 3: Verify Installation
Open Command Prompt.
Run the command:
mongod --versionThis should display the installed MongoDB version.
macOS
Step 1: Install Homebrew
If you haven't installed Homebrew, run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Step 2: Install MongoDB
Tap the MongoDB formula:
brew tap mongodb/brewInstall MongoDB:
brew install mongodb-community
Step 3: Start MongoDB
Start MongoDB as a service:
brew services start mongodb/brew/mongodb-communityVerify installation by checking the version:
mongod --version
Linux (Ubuntu)
Step 1: Import the Public Key
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -Step 2: Create a List File for MongoDB
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.listStep 3: Reload the Local Package Database
sudo apt-get updateStep 4: Install MongoDB Packages
sudo apt-get install -y mongodb-orgStep 5: Start MongoDB
sudo systemctl start mongodStep 6: Verify Installation
mongod --versionBasic Configuration
Configuration File
MongoDB’s configuration file is located at /etc/mongod.conf by default. Key sections include:
Storage: Configures the storage engine and database file paths.
Net: Configures network settings including the port MongoDB listens on.
Security: Configures security options like authorization.
Example Configuration (/etc/mongod.conf)
/etc/mongod.conf)# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# Where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Network interfaces
net:
port: 27017
bindIp: 127.0.0.1
# Security
security:
authorization: enabledStarting and Stopping MongoDB
Windows
Use the net command:
net start MongoDB
net stop MongoDBmacOS
Using Homebrew services:
brew services start mongodb/brew/mongodb-community
brew services stop mongodb/brew/mongodb-communityLinux
Using systemctl:
sudo systemctl start mongod
sudo systemctl stop mongodEnabling Authentication
Create an administrative user.
use admin db.createUser( { user: "admin", pwd: "password", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )Restart MongoDB with authentication enabled.
Authenticate using the created user:
mongo -u "admin" -p "password" --authenticationDatabase "admin"
Conclusion
You have now installed and configured MongoDB on your system. This guide covered the essential steps to get you started with MongoDB, including installation, basic configuration, and enabling authentication. For more detailed configuration and advanced features, refer to the MongoDB documentation.
Last updated