How to Install MongoDB 4 on AlmaLinux 8

MongoDB is a NoSQL database that stores data as JSON-like documents. Documents store related information together and use the MongoDB query language (MQL) for access and it is also called as GriddFS which performs load balancing and data replication features over multiple machines for storing files.

Installing MongoDB

Before installing MongoDB, we need to add a repository manually since it is not present in the default repository.

Create a repository file using the following command.

# vi /etc/yum.repos.d/mongodb.repo

And the following line and save the file.

[mongodb-org-4.4] name=MongoDB Repository

Enter the following command to install MongoDB.

dnf install -y mongodb-org

Next, Start and Enable the MongoDB.

systemctl start mongod

systemctl enable mongod

Command to Verify the MongoDB.

systemctl status mongod


[root@server ~]# systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-02-16 23:10:54 EST; 46s ago
 Main PID: 2236 (mongod)
   Memory: 102.3M
   CGroup: /system.slice/mongod.service
           └─2236 /usr/bin/mongod -f /etc/mongod.conf

MongoDB Shell

Command to access MongoDB’s shell.


Note: You will receive the following output when you enter the MongoDB shell for the first time.


[root@server ~]# mongo
MongoDB shell version v4.4.4
connecting to: mongodb://
Implicit session: session { "id" : UUID("9a6a4ff9-53ad-4e1e-b49b-ff089559b9e1") }
MongoDB server version: 4.4.4
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
Questions? Try the MongoDB Developer Community Forums
The server generated these startup warnings when booting:
        2021-02-16T23:10:54.569-05:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-02-16T23:10:54.569-05:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()

Creating MongoDB Admin User

Enter into MongoDB shell.


Command to switch to the database admin by running.

> use admin

Create an Admin user by running the following code.

Note: Replace a Enter-A-user-Here with a username and Enter-A-Password-Here with a strong and secure password.

> db.createUser(
 user: "Enter-A-user-Here",
 pwd: "Enter-A-Password-Here",
 roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]


 Successfully added user: {
        "user" : "mongod_admin",
        "roles" : [
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"

Run the following command to list MongoDB users.

> show users


> show users
        "_id" : "admin.mongod_admin",
        "userId" : UUID("5b78c7d3-b956-4330-8d77-7212a7cff7c1"),
        "user" : "mongod_admin",
        "db" : "admin",
        "roles" : [
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
        "mechanisms" : [

Authentication for MongoDB

Note: Since all users can access the shell and execute any commands this will cause security issues -- we can prevent this by creating authentication for the admin user.

To enable authentication open the /lib/systemd/system/mongod.service file and find the following line.

Environment="OPTIONS=-f /etc/mongod.conf"

and change to,

Environment="OPTIONS= --auth -f /etc/mongod.conf"

Save and exit the configuration file.

And restart MongoDB to reflect the changes.

systemctl restart mongod

Now you can try to list the user without authentication, you will receive an error


> show users
uncaught exception: Error: command usersInfo requires authentication :

To authenticate, run the following code.

Note: Replace a Enter-A-user-Here and Enter-A-Password-Here with your credentials.

> db.auth('Enter-A-user-Here', 'Enter-A-Password-Here')

Now you can run any command with no issues.

To exit MongoDB.

> exit

This Concludes how you install MongoDB on AlmaLinux 8.

If you can not configure and face any technical difficulties, kindly reach out to us via Support Ticket.