How to Install MongoDB on Rocky Linux 8

Install MongoDB on Rocky Linux 8

In this article, we will have explained the necessary steps to install MongoDB on Rocky Linux 8. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

MongoDB is an open-source document-oriented database that is designed to store a large scale of data and also allows you to work with that data very efficiently. Unlike in SQL databases where data is stored in rows and columns inside tables, in MongoDB, data is structured in JSON-like format inside records which are referred to as documents. MongoDB is implemented in C++ language and is available for Windows, Mac OS, and Linux Operating systems supported by both 32 and 64-bit systems.

Install MongoDB on Rocky Linux 8

Step 1. First, before you start installing any package on your Rocky Linux server, we always recommend making sure that all system packages are updated.

sudo dnf install epel-release
sudo dnf update
sudo dnf upgrade

Step 2. Install MongoDB on the Rocky Linux system.

Now add the  package repository:

sudo nano /etc/yum.repos.d/mongodb-org-5.0.repo

Add the following code:

[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc

After that, install MongoDB on Rocky Linux 8 using the following command below:

sudo dnf install mongodb-org

Next, start and enable MongoDB:

sudo systemctl enable mongod --now

Once the installation is complete, confirm your installed version:

mongod --version

Step 3. Configure firewall.

If you have firewalld running on your server and would like MongoDB service to be accessible over the network, open it on the firewall:

sudo firewall-cmd --add-port=27017/tcp --permanent
sudo firewall-cmd --reload

Step 4. Access MongoDB Shell.

To access the MongoDB shell, run the following command from any terminal session on the server:

mongo

From the shell interface, you can run the command below to show all databases:

db

In MongoDB, databases are created by simply switching to a nonexisting database and specifying the name of the database to create as below (the new database is test-db):

use test-db

Next, add the following command creates a collection of documents (data structure in MongoDB):

db.linux.insertOne(
  { "ubuntu" : "20.04",
    "rocky linux" : "8",
    "debian" : "11"
  }
)

To show documents in your database:

show collections

Now create a new MongoDB user by running the code below:

db.createUser(
  {
   user: 'nagdimin ',
   pwd: 'your-strong-passwd',
   roles: [ { role: 'readWrite', db: 'test-db' } ]
  }
 );

That’s all you need to do to install MongoDB on Rocky Linux 8. I hope you find this quick tip helpful. For further reading on MongoDB, please refer to their official knowledge base. If you have questions or suggestions, feel free to leave a comment below.