How to Install MongoDB on Linux: A Step-by-Step Guide
MongoDB is a popular NoSQL database known for its flexibility, scalability, and high performance. It’s used by developers and organizations across various industries for its robustness and ease of use. If you are a Linux user and want to harness the power of MongoDB, this article provides a comprehensive guide to installing it on your Linux system.
Prerequisites
Before installing MongoDB, make sure you have the following:
- Linux OS: This guide will focus on Ubuntu and CentOS, but the steps can be similar for other distributions.
- Root/Sudo Privileges: You need administrative access to install software.
- A Terminal: You will be using the command line to execute most of the installation commands.
Part 1: Install MongoDB on Ubuntu
Step 1: Import the MongoDB Public Key
First, you need to import the MongoDB public key to ensure the authenticity of the packages.
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
Step 2: Create a List File for MongoDB
Create a new list file for MongoDB by creating a .list
file in the /etc/apt/sources.list.d/
directory.
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/multiverse mongodb-org/6.0/restricted main" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
Make sure to change focal
to your specific Ubuntu version (like bionic
, if you are using Ubuntu 18.04).
Step 3: Update Package Database
After adding MongoDB’s official repository, update the package database.
sudo apt update
Step 4: Install MongoDB
Now, install the MongoDB packages.
sudo apt install -y mongodb-org
Step 5: Start the MongoDB Service
After installation, you can start the MongoDB service using the following command:
sudo systemctl start mongod
Step 6: Enable MongoDB to Start on Boot
To ensure that MongoDB starts automatically when your server reboots, run:
sudo systemctl enable mongod
Step 7: Verify Installation
You can verify that MongoDB is running with the following command:
sudo systemctl status mongod
If the service is active and running, you have successfully installed MongoDB!
Part 2: Install MongoDB on CentOS
Step 1: Create a Repository File
Create a repository configuration file using your text editor.
sudo nano /etc/yum.repos.d/mongodb-org-6.0.repo
Add the following lines to the file:
[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/7/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
Step 2: Install MongoDB
Update your package database and install MongoDB.
sudo yum install -y mongodb-org
Step 3: Start MongoDB
After installation, start the MongoDB service:
sudo systemctl start mongod
Step 4: Enable MongoDB to Start on Boot
To ensure MongoDB starts automatically at system startup, execute:
sudo systemctl enable mongod
Step 5: Verify Installation
Like Ubuntu, you can check if MongoDB is running with:
sudo systemctl status mongod
Now you have a fully functional MongoDB server running on your Linux machine! Whether you’re using Ubuntu or CentOS, the installation process is straightforward and can be done in just a few steps. You can connect to MongoDB using the MongoDB shell or programmatically using various libraries provided for different programming languages.
Next Steps
- Secure Your Installation: It’s important to configure authentication and secure your MongoDB settings.
- Explore MongoDB Features: Learn about collections, documents, and indexes.
- Backup and Restore: Familiarize yourself with best practices for backing up your databases.
With MongoDB installed, you’re all set to develop resilient, scalable applications!