Installing MongoDB 5 on AlmaLinux 8

Hello,
In this week’s feature highlight, we look at How to Install MongoDB 5 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 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-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

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

Output:

[root@vps ~]# systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enab>
   Active: active (running) since Mon 2021-10-25 13:36:18 EDT; >
     Docs: https://docs.mongodb.org/manual
  Process: 57055 ExecStart=/usr/bin/mongod $OPTIONS (code=exite>
  Process: 57053 ExecStartPre=/usr/bin/chmod 0755 /var/run/mong>
  Process: 57051 ExecStartPre=/usr/bin/chown mongod:mongod /var>
  Process: 57050 ExecStartPre=/usr/bin/mkdir -p /var/run/mongod>
 Main PID: 57058 (mongod)
   Memory: 156.6M
   CGroup: /system.slice/mongod.service
           └─57058 /usr/bin/mongod -f /etc/mongod.conf

MongoDB Shell

Command to access MongoDB’s shell.

mongo

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

Output:

[root@vps ~]# mongo
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("283a2949-15c6-413e-a0f2-6aa95c1bae3a") }
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/
================

Creating MongoDB Admin User

Enter into MongoDB shell.

mongo

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" } ]
 }
 )

Output:

 Successfully added user: {
    "user" : "Enter-A-user-Here",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}
>

Run the following command to list MongoDB users.

> show users

Output:

> show users
{
    "_id" : "admin.Enter-A-user-Here",
    "userId" : UUID("8e2d9f07-e5a0-439c-9893-9fbe1bb33c54"),
    "user" : "Enter-A-user-Here",
    "db" : "admin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ],
    "mechanisms" : [
        "SCRAM-SHA-1",
        "SCRAM-SHA-256"
    ]
}
> 

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

output:

> 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:1635:15
shellHelper.show@src/mongo/shell/utils.js:933:9
shellHelper@src/mongo/shell/utils.js:838: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:

{
    "_id" : "admin.Enter-A-user-Here",
    "userId" : UUID("8e2d9f07-e5a0-439c-9893-9fbe1bb33c54"),
    "user" : "Enter-A-user-Here",
    "db" : "admin",
    "roles" : [
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ],
    "mechanisms" : [
        "SCRAM-SHA-1",
        "SCRAM-SHA-256"
    ]
}

To exit the database engine run.

exit    
(Visited 110 times, 1 visits today)