How to Install MongoDB 4 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 the 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 created file and save the file.

[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

At the time of writing this article, version 4.4 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:

[root@server ~]# systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor pres>
   Active: active (running) since Tue 2021-05-18 09:53:46 EDT; 34s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 5638 (mongod)
   Memory: 128.5M
   CGroup: /system.slice/mongod.service
           └─5638 /usr/bin/mongod -f /etc/mongod.conf

Access MongoDB Shell

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

mongo

Output:

[root@server ~]# mongo
MongoDB shell version v4.4.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("714ed2f1-1f91-46f8-8fb8-626fa3a60acf") }
MongoDB server version: 4.4.6
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("ff01436e-1cc8-4f37-8929-3009537c3c47"),
        "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 changed 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.

[root@server ~]# mongo
MongoDB shell version v4.4.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("7b78520c-5cc2-417a-9e6e-3eeb7e8ce60b") }
MongoDB server version: 4.4.6
> db.auth('mongod_admin', 'P@ssword@2019')
Error: Authentication failed.
0
> show users
uncaught exception: Error: command usersInfo requires authentication :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1659:15
shellHelper.show@src/mongo/shell/utils.js:914:9
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1

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("ff01436e-1cc8-4f37-8929-3009537c3c47"),
        "user" : "mongod_admin",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}

To exit the database engine run.

exit

CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS