How to Installing MongoDB on Ubuntu 22.04 with Firewall Configuration and Enhanced Security ?

MongoDB is a powerful NoSQL database that provides high performance, scalability, and flexibility. This guide will walk you through the step-by-step process of installing MongoDB 7.0 Community Edition on Ubuntu 22.04, including securing your MongoDB installation.

Prerequisites

Before you begin, ensure that you have the necessary prerequisites installed on your system:

sudo apt-get update
sudo apt-get install software-properties-common gnupg apt-transport-https ca-certificates ufw -y

These packages are necessary for handling repositories, GPG keys, and secure communication. ufw is a firewall management tool that we'll use to configure the firewall settings.

Additional Prerequisite: Install curl

If curl is not already installed on your system, you can install it using the following command:

sudo apt-get install gnupg curl -y

Import MongoDB GPG Key

Next, import the MongoDB GPG key to verify the authenticity of the software packages. Run the following command:

curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor

Add MongoDB Repository

Create a MongoDB source list file for APT in the /etc/apt/sources.list.d/ directory:

echo "deb [signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg] https://repo.mongodb.com/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

Update Package List

Update the package list to include MongoDB packages:

sudo apt-get update

Install MongoDB

Install MongoDB 7.0 Community Edition using the following command:

sudo apt-get install -y mongodb-org

Configure Firewall

Assuming you are using ufw (Uncomplicated Firewall), open the MongoDB port (27017) and enable ufw:

sudo ufw allow 27017
sudo ufw enable

Enhance MongoDB Security

Bind MongoDB to Localhost

Edit the MongoDB configuration file:

sudo nano /etc/mongod.conf

Add or modify the following line:

bindIp: 127.0.0.1

Save the changes and restart MongoDB:

sudo systemctl restart mongod

Enable Authentication

Connect to the MongoDB shell:

mongosh

Inside the shell, create an admin user:

use admin
db.createUser(
  {
    user: "admin",
    pwd: "your_admin_password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

Update the MongoDB configuration file (/etc/mongod.conf) to enable authorization:

security:
  authorization: enabled

Restart MongoDB:

sudo systemctl restart mongod

Verify Installation

Check the status of the MongoDB service to ensure it's running:

sudo systemctl status mongod

Access MongoDB Shell

Open the MongoDB Shell to interact with the database:

mongosh

Show Databases

Once in the MongoDB Shell, use the show dbs command to display a list of available databases:

show dbs

Congratulations! You have successfully installed MongoDB 7.0 Community Edition on Ubuntu 22.04, secured your installation, and configured the firewall settings. You can now start working with MongoDB and building powerful applications.

Comments

Leave a Reply