MongoDB is an open-source, cross-platform, and distributed NoSQL (non-SQL or Non-Relational) database system. Instead of storing data in tables like traditional SQL databases, MongoDB uses flexible documents to store various data forms. MongoDB uses the binary JSON format, BSON, for storing data.

MongoDB is a distributed NoSQL database with built-in high availability, automatic failover and data redundancy, and horizontal scaling via sharding across distributed clusters. It supports multi-region geographic deployment and provides a query API that supports CRUD operations (read and write), data aggregation pipeline, text search, and geospatial queries.

This guide will show you how to install MongoDB on an Alma Linux 9 server. You’ll also enable MongoDB authentication, set up an administrator user for MongoDB, and create a new database and user for your applications.

Prerequisites

To start with this guide, make sure you have the following:

  • An Alma Linux 9 server
  • A non-root user with administrator privileges

Preparing system (disable thp, setup ulimit, and sysctl.conf)

In this section, you’ll prepare and set up your ALma Linux server for the MongoDB installation. Below are three major you must do:

  • Disable Transparent Huge Pages (THP) via systemd service
  • Increasing max processes and open files for the ‘mongod’ user
  • Setting vm-max fs through the/etc/sysctl.conf’ file

So first, run the following ‘dnf’ command to install the ‘nano’ text editor to your Alma Linux system.

sudo dnf install nano -y

Disable Transparent Huge Pages (THP)

Now create a new service file ‘/etc/systemd/system/disable-thp.service‘ for disabling Transparent Huge Pages (THP). This service will also run on every system boot.

sudo nano /etc/systemd/system/disable-thp.service

Insert the following configuration into the file.

[Unit]

Description=Disable Transparent Huge Pages (THP)[Service]

Type=simple

ExecStart=/bin/sh -c "echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled && echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag"

[Install]

WantedBy=multi-user.target

When done, save and exit the file.

Next, run the ‘systemctl‘ command below to reload the systemd manager and apply your changes.

sudo systemctl daemon-reload

Lastly, start and enable the ‘disable-thp.service‘ using the command below. With this, THP will automatically disabled at startup.

sudo systemctl enable --now disable-thp.service

<img alt="disable thp" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/10/echo/screenshot_at_2024-09-22_04-14-44.png66fe772ef0972.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="249" loading="lazy" src="data:image/svg xml,” width=”750″>

Setting up ulimits

Create a new configuration ‘/etc/security/limits.d/mongodb.conf‘ with the following ‘nano‘ editor.

sudo nano /etc/security/limits.d/mongodb.conf

Insert the configuration below into the file. This allows the ‘mongod‘ user to run max processes and open max file descriptors for ‘64000‘.

mongod soft nproc 64000

mongod hard nproc 64000

mongod soft nofile 64000

mongod hard nofile 64000

Save the file and exit the editor.

Setting up sysctl.conf

Open the file ‘/etc/sysctl.conf‘ using ‘nano‘ editor.

sudo nano /etc/sysctl.conf

Add the following lines to set up fs.file-max, max map count, and enable swappiness.

fs.file-max = 2097152

vm.max_map_count = 262144

vm.swappiness = 1

Save the file and exit the editor.

Now run the ‘sysctl‘ command below to apply your changes.

sudo sysctl -p

<img alt="setup ulimit and sysctl" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/10/echo/screenshot_at_2024-09-22_04-14-57.png66fe772f15953.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="213" loading="lazy" src="data:image/svg xml,” width=”651″>

Optionally, you can also reboot the server to apply your changes.

sudo reboot

Installing MongoDB

Now after configuring your Alma Linux server, start the MongoDB server installation with the following – In this case, you’ll be installing MongoDB 7.0 on the Alma Linux 9 server.

First, create a new MongoDB repository file ‘/etc/yum.repos.d/mongodb-org-7.0.repo‘ with the following ‘nano‘ editor.

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

Insert the configuration below into the file.

[mongodb-org-7.0]

name=MongoDB Repository

baseurl=https://repo.mongodb.org/yum/redhat/9/mongodb-org/7.0/x86_64/

gpgcheck=1

enabled=1

gpgkey=https://pgp.mongodb.com/server-7.0.asc

Save the file and exit the editor.

Now run the ‘dnf‘ command below to list available repositories on your Alma Linux server. You’ll see the MongoDB repository has been added.

sudo dnf repolist

<img alt="setup repo" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/10/echo/screenshot_at_2024-09-22_04-15-07.png66fe772f3cd55.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="180" loading="lazy" src="data:image/svg xml,” width=”750″>

Next, run the ‘dnf install‘ command below to install MongoDB and the MongoDB shell to your system. Enter ‘Y‘ to confirm the installation.

sudo dnf install mongodb-org mongodb-mongosh

<img alt="install mongodb" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/10/echo/screenshot_at_2024-09-22_04-15-17.png66fe772f894a8.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="369" loading="lazy" src="data:image/svg xml,” width=”750″>

After the installation is complete, start and enable the MongoDB ‘mongod‘ service, and check the ‘mongod‘ service status to ensure it is running.

sudo systemctl enable --now mongod

sudo systemctl status mongod

You can see below that MongoDB is running on the Alma Linux server.

<img alt="start verify mongodb" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/10/echo/screenshot_at_2024-09-22_04-15-28.png66fe772fb1eaf.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="225" loading="lazy" src="data:image/svg xml,” width=”750″>

Enable MongoDB authentication

At this point, the MongoDB server is running on your Alma Linux server. Now you’ll secure your MongoDB installation by enabling password authentication and setting up a new MongoDB administrator user. Also, you’ll learn the basic ‘mongosh’ or MongoDB shell for managing the MongoDB server.

First, log in to the MongoDB server using the ‘mongosh‘ command below.

mongosh

Once logged in to the MongoDB, run the query below to disable the MongoDB Telemetry.

disableTelemetry()

Now switch to the database ‘admin‘ and run the query below to set up an admin user for MongoDB. In this example, you’ll be creating a new user ‘myAliceAdmin‘ as admin for MongoDB. Also, enter your password when prompted.

use admin

db.createUser(

{

user: "myAliceAdmin",

pwd: passwordPrompt(),

roles: [

{ role: "userAdminAnyDatabase", db: "admin" },

{ role: "readWriteAnyDatabase", db: "admin" }

]

}

)

If the new user is created, you’ll see an output ‘{ ok: 1 }‘.

Type ‘quit()‘ to exit from the MongoDB server.

<img alt="create admin" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/10/echo/screenshot_at_2024-09-22_04-15-42.png66fe773011447.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="521" loading="lazy" src="data:image/svg xml,” width=”618″>

Next, open the file ‘/etc/mongod.conf‘ using the ‘nano‘ editor.

sudo nano /etc/mongod.conf

Uncomment the ‘security‘ option and add ‘authorization: enabled‘ to enable password authentication on MongoDB.

security:

authorization: enabled

Save the file and exit the editor.

Now run the ‘systemctl‘ command below to restart the ‘mongod‘ service and apply your changes. With this, the MongoDB server should be running with authentication enabled.

sudo systemctl restart mongod

Next, run the ‘mongosh‘ command below to log in to the MongoDB.

mongosh

<img alt="connect to mongodb" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/10/echo/screenshot_at_2024-09-22_04-15-50.png66fe773040996.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="184" loading="lazy" src="data:image/svg xml,” width=”750″>

Switch to the database ‘admin‘ and authentication as a user ‘myAliceAdmin‘ with the following queries. Input your password when asked.

use admin

db.auth("myAliceAdmin", passwordPrompt())

If the authentication is successful, you’ll get an output ‘{ ok: 1 }‘.

<img alt="test auth" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/10/echo/screenshot_at_2024-09-22_04-16-13.png66fe7730694f3.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="203" loading="lazy" src="data:image/svg xml,” width=”545″>

Additionally, you can also log in to the database ‘admin‘ using the user ‘myAliceAdmin‘ via the ‘mongosh‘ command below.

mongosh --port 27017 --authenticationDatabase 

"admin" -u "myAliceAdmin" -p

Creating a user in MongoDB

In this section, you’ll be creating a new database and user in MongoDB via the ‘mongosh‘ MongoDB client.

To create a new database and user in MongoDB, run the following queries. In this example, you’ll be creating a new database ‘mydb‘, and a new user ‘myUser‘. Enter your password when prompted.

use mydb

db.createUser(

{

user: "myUser",

pwd: passwordPrompt(), // or cleartext password

roles: [ { role: "readWrite", db: "mydb" },

{ role: "read", db: "reporting" } ]

}

)

If the new database and user are created, you’ll get an output ‘{ ok: 1 }‘, and then type ‘quit()‘ to exit.

<img alt="create new user and database" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/10/echo/screenshot_at_2024-09-23_11-01-17.png66fe773094c02.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="355" loading="lazy" src="data:image/svg xml,” width=”629″>

Next, log in to the MongoDB server using the ‘mongosh‘ command below. Input the password for ‘myUser‘ when prompted.

mongosh --port 27017 --authenticationDatabase 

"mydb" -u "myUser" -p

If successful, you’ll get the prompt of MongoDB shell.

<img alt="connect to database" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/10/echo/screenshot_at_2024-09-23_11-03-13.png66fe7730b529c.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="199" loading="lazy" src="data:image/svg xml,” width=”750″>

Conclusion

Congratulations! You’ve completed the installation of MongoDB on the Alma Linux 9 server. You’ve also secured MongoDB by creating an administrator user and enabling password authentication. Lastly, you’ve also learned how to create a database and user in MongoDB, and some basic ‘mongosh’ commands for connecting to the MongoDB server through the command line.