How to Install MongoDB 5 in CentOS 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.

Add MongoDB Repository

MongoDB is not present in the CentOS 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

Install MongoDB in CentOS 8

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@vps ~]# 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 10:33:57 EDT;>
     Docs: https://docs.mongodb.org/manual
 Main PID: 59662 (mongod)
   Memory: 150.5M
   CGroup: /system.slice/mongod.service
           └─59662 /usr/bin/mongod -f /etc/mongod.conf

Access MongoDB Shell

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

mongo

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("9df5b6b1-7306-444e-97c4-0c5e249b7125") }
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

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("b7b62506-630d-463b-8ae1-b65f72063b17"),
  "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.

MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("5d5e87fc-f40d-4e42-bc0e-96bd60ee66a1") }
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:

 {
  "_id" : "admin.mongod_admin",
  "userId" : UUID("b7b62506-630d-463b-8ae1-b65f72063b17"),
  "user" : "mongod_admin",
  "db" : "admin",
  "roles" : [
    {
      "role" : "userAdminAnyDatabase",
      "db" : "admin"
    }
  ],
  "mechanisms" : [
    "SCRAM-SHA-1",
    "SCRAM-SHA-256"
  ]
}

To exit the database engine run.

exit