How to Install MongoDB 5 in Rocky Linux 8

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. It is categorized under the NoSQL (Not only SQL) database because the storage and retrieval of data in MongoDB are not in the form of tables.

Add MongoDB Repository

MongoDB is not present in the Rocky Linux 8 default repository, we need to add it manually.

First, create the below file.

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

Paste the below configuration to a created file and save the file.

[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

At the time of writing this article, version 5.0 was the latest. You can check the latest version at the time of installation by checking their website

Installing MongoDB

yum install mongodb-org

Next, start and enable MongoDB to start on boot.

systemctl start mongod

systemctl enable mongod

To verify the status of MongoDB

systemctl status mongod

Output:

[[email protected] ~]# systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; ena>
   Active: active (running) since Mon 2021-10-25 11:42:59 EDT;>
     Docs: https://docs.mongodb.org/manual
 Main PID: 56769 (mongod)
   Memory: 148.3M
   CGroup: /system.slice/mongod.service
           └─56769 /usr/bin/mongod -f /etc/mongod.conf

Access MongoDB Shell

You can now access MongoDB’s shell by using the following command

mongo

Output:

[[email protected] ~]# mongo
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("55af8151-15f9-470e-bbf6-2f5a49b6bb85") }
MongoDB server version: 5.0.3
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
  https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
  https://community.mongodb.com

Creating MongoDB and Admin User

mongo

Next, switch to the database admin by running.

use admin

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

db.createUser(
      {
      user: "mongod_admin",
      pwd: "YOUR-PASSWORD",
      roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
      }
    )

Note: Replace YOUR-PASSWORD with the actual password

Output:

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

To list MongoDB users created, run.

show users

Output:

> show users
{
  "_id" : "admin.mongod_admin",
  "userId" : UUID("a16abf82-c8d2-4f52-8ef2-b2ce732f6516"),
  "user" : "mongod_admin",
  "db" : "admin",
  "roles" : [
    {
      "role" : "userAdminAnyDatabase",
      "db" : "admin"
    }
  ],
  "mechanisms" : [
    "SCRAM-SHA-1",
    "SCRAM-SHA-256"
  ]
}

Configuring Authentication for MongoDB

To enable authentication edit the /lib/systemd/system/mongod.service file, under the [Service] section, locate and edit the Environment parameter as shown.

vi /lib/systemd/system/mongod.service

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

After changing the file looks like.

[Service]
User=mongod
Group=mongod
Environment="OPTIONS= --auth -f /etc/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/usr/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb

For the changes to come into effect, reload the system and restart MongoDB.

systemctl daemon-reload

systemctl restart mongod

If you now try listing the users without authentication, you should get an error as shown.

[[email protected] ~]# mongo
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("80697c5f-1a1a-4166-9739-a0ff544a3ea8") }
MongoDB server version: 5.0.3
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/

To authenticate, simply pass the credentials as shown.

db.auth('mongod_admin', 'YOUR-PASSWORD')

Note: Replace YOUR-PASSWORD with the actual password

Now you can run any command after that. Let’s try listing the users once more:

show users

Output:

> db.auth('mongod_admin', 'YOUR-PASSWORD')
1
> show users
 {
  "_id" : "admin.mongod_admin",
  "userId" : UUID("a16abf82-c8d2-4f52-8ef2-b2ce732f6516"),
  "user" : "mongod_admin",
  "db" : "admin",
  "roles" : [
    {
      "role" : "userAdminAnyDatabase",
      "db" : "admin"
    }
  ],
  "mechanisms" : [
    "SCRAM-SHA-1",
    "SCRAM-SHA-256"
  ]
}

To exit the database engine run.

exit