How to Install MongoDB on AlmaLinux 8

MongoDB is a relational database that is free and open-source. When data is stored in dynamic schemas (JSON-like documents), it makes the integration of data into certain kinds of applications simpler and quicker. It’s often compared to other NoSQL databases like Cassandra or CouchDB. The reasons for its popularity are high performance, flexibility, ease of use, rich feature set, and the fact that it is free and open-source.

MongoDB has gone from unknown to being one of the highest-profile startups in New York City within a couple of years. It now has more than 2500 employees with funding totaling around $300 million. Top companies such as Adobe, Craigslist, Forbes, and Shutterfly use MongoDB as their primary data storage. Even NASA, with all its super-computers, uses MongoDB for its satellite data collection. There are hundreds of thousands of users, including over 100,000 organizations, who have downloaded the software. The basic version is available for free under an open-source license.

In this tutorial, we will walk you through the steps required to install MongoDB on an AlmaLinux 8 server.

Prerequisites

  • A server with AlmaLinux 8 installed.
  • Root access to the server.
  • It is important to know that MongoDB can run on any hardware and OS. However, it is usually recommended to have a separate disk for the database files since they are almost never accessed by other processes in normal conditions. Besides that, if you work with high volume data or high write operations, it is better to use a separate disk for the journal files as well.

Step 1. Updating your System

It is always good to update your system first. Run the following command to update all packages on your system.

sudo dnf update -y
sudo dnf install epel-release -y

Step 2. Adding MongoDB Repository

The standard AlmaLinux 8 repository do not contain the MongoDB package, so we need to add the official MongoDB repository with the newest stable version.

In this tutorial, we will use the DNF package manager to install MongoDB. With that said, we need to add the official MongoDB repository to the /etc/yum.repos.d/ directory so that the DNF will be able to download the MongoDB package from its repository.

Run the following command below to create a new repo file called mongodb.repo, and put it into the /etc/yum.repos.d/ directory.

nano /etc/yum.repos.d/mongodb.repo

Once you open the file, copy and paste the following lines into it.

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

Once you are done, press CTRL+X followed by Y, Enter to save the changes.

To check if the repo file has been added successfully, run the following command.

sudo dnf repolist

You should see the mongodb-org-4.4 repo in the repo list.

To verify if the repo is enabled or not, run the following command.

sudo dnf repolist mongodb-org-4.4

You should get the following output.

If it’s not enabled, you need to run the following command.

dnf config-manager --set-enabled mongodb-org-4.4

Step 3. Installing MongoDB

You have added the official MongoDB repo. Now let’s install MongoDB using the DNF package manager. Run the following command to install MongoDB on your AlmaLinux 8 server.

sudo dnf install mongodb-org

During the installation, you will be asked to confirm the installation. Just type Y and press Enter to continue.

You might also be asked to validate Mongo's signing key as part of your DNF request. Just type Y and press Enter again to confirm. The DNF may take some time to download and install MongoDB, so be patient.

Once the installation is done, you can proceed with starting the MongoDB service by running the following command.

sudo systemctl start mongod

If you get systemctl start unit not found error, reload all unit files and start the mongod.service again.

sudo systemctl daemon-reload
sudo systemctl start mongod

To enable the mongodb service to run automatically upon reboot, run the following command.

sudo systemctl enable mongod

You can use the following commands to stop and restart the mongodb service.

sudo systemctl stop mongod
sudo systemctl restart mongod

Finally, check if the mongodb service is running correctly by typing this command below.

sudo systemctl status mongod

Step 4. Testing MongoDB

To test our MongoDB installation, we will log in to the MongoDB shell and do some basic queries.

To log in to the MongoDB shell, run the following command below.

mongo

Once you are successfully logged into the MongoDB shell, your prompt should change to >. This is where you will be entering your command/queries.

Now, let’s switch to the admin database, using the use command.

use admin

Next, let’s create a new username, “vitux”, with the password is “password,” and give it the “userAdminAnyDatabase” role.

db.createUser(
{
user: "vitux",
pwd: "password",
roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ]
}
)

Press Enter to execute the command. You should get a Successfully added user.

Now, to verify if our user was created correctly, run the following query below.

show users

You should see this output if everything is working correctly.

To create a new database, you can use the command below.

use <database_name>

Where: <database_name> is the name of the database you want to create.

This command will switch to the <database_name> database if it exists. If it does not exist, this command will create a new one for you and switch to it.

For example, let’s create a new database called “vitux_db” on our MongoDB server.

use vitux_db

To show the database that you are currently using, run the following command below.

db

You have created a database. Let’s add some data to it. We will create a collection called “linux_version,” and we will store the distro names and version numbers in it.

To add data to our new collection, we will use the insert method and pass an object that has distro and version info. Copy and paste the following line to the MongoDB prompt and hit Enter.

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

To print a list of all collections in your database, run the query below.

show collections

To print a list of all data in a collection , you can run the query below.

db.<collection_name>.find()

or

db.<collection_name>.find().pretty()

For example, to print all data in the linux_version collection, run this command below. In this example, we will use the pretty() method because it beautifies the output, which makes it easier to read/human-readable.

db.linux_version.find().pretty()

To exit your MongoDB shell, type or paste the command below and hit Enter.

quit();

The MongoDB installation is now complete.

For more on using the MongoDB shell, including how to work with data, visit its official documentation page.

Conclusion

In this tutorial, you have learned how to install MongoDB on your AlmaLinux 8 system. You also learned the basic commands required for working with MongoDB.