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
.msi
file.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 --version
This 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/brew
Install MongoDB:
brew install mongodb-community
Step 3: Start MongoDB
Start MongoDB as a service:
brew services start mongodb/brew/mongodb-community
Verify 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.list
Step 3: Reload the Local Package Database
sudo apt-get update
Step 4: Install MongoDB Packages
sudo apt-get install -y mongodb-org
Step 5: Start MongoDB
sudo systemctl start mongod
Step 6: Verify Installation
mongod --version
Basic 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: enabled
Starting and Stopping MongoDB
Windows
Use the net
command:
net start MongoDB
net stop MongoDB
macOS
Using Homebrew services:
brew services start mongodb/brew/mongodb-community
brew services stop mongodb/brew/mongodb-community
Linux
Using systemctl
:
sudo systemctl start mongod
sudo systemctl stop mongod
Enabling 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.