how to install MongoDB replica set

Install MongoDB in Ubuntu 16 / 18 / 20/ 22

Last updated on March 11th, 2023 at 03:41 pm

Introduction

MongoDB is a popular NoSQL database that is used in many modern web applications. It is known for its flexibility, scalability, and ease of use. In this article, we will show you how to install MongoDB on Ubuntu 16.04, 18.04, 20.04, and 22.04.2.

Prerequisites

Before you begin, you need to have a Ubuntu system with sudo privileges. You can log in as a root user or a user with sudo access.

Step 1: Import the MongoDB GPG key

The first step is to import the MongoDB GPG key. We need this to verify the authenticity of the MongoDB packages during the installation process.

To import the GPG key, run the following command:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

Step 2: Add the MongoDB repository

Next, add the MongoDB repository to your Ubuntu system. You can do this by creating a new file in the /etc/apt/sources.list.d directory.

To add MongoDB repository in Ubuntu 16.04:

echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list

For 18.04 Ubuntu:

echo "deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

For Ubuntu 20.04:

echo "deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

Or, For Ubuntu 22.04:

echo "deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu hirsute/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

Step 3: Update the package repository

After adding the MongoDB repository, you need to update the package repository to get the latest package information.

To update the package repository, run the following command:

sudo apt-get update

Step 4: Install MongoDB

Now, you can install the MongoDB package using the apt package manager.

To install MongoDB, run the following command:

sudo apt-get install -y mongodb-org

We use -y option to answer yes to any prompts during the installation process.

Step 5: Start MongoDB

After the installation is complete, you need to start the MongoDB service.

To start MongoDB, run the following command:

sudo systemctl start mongod

Step 6: Verify that MongoDB is running

Finally, you can verify that MongoDB is running by checking its status.

To check the status of MongoDB, run the following command:

sudo systemctl status mongod

If MongoDB is running properly, you should see a message that says “active (running)”.

Conclusion

In this article, we have shown you how to install MongoDB on Ubuntu 16, 18, and 20. With MongoDB installed, you can now start using it for your applications. MongoDB is a powerful NoSQL database that is well-suited for modern web applications, so it is definitely worth learning if you are working in this field.

Next: Setup Authorization in your MongoDB Installation

Leave a Reply

%d bloggers like this: