How to Install MongoDB 4 in CentOS 8

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 created file and save the file.

[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/development/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

Install MongoDB in CentOS 8

yum install mongodb-org

Next, start and enable MongoDB to start on boot.

systemctl start mongod

sudo systemct   enable mongod

To verify the status of MongoDB

systemctl status mongod

Output:

[root@my ~]# systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor pres>
     Active: active (running) since Wed 2020-03-18 10:56:01 EDT; 1min 1s ago
         Docs: https://docs.mongodb.org/manual
 Main PID: 28352 (mongod)
     Memory: 73.5M
     CGroup: /system.slice/mongod.service
                     └─28352 /usr/bin/mongod -f /etc/mongod.conf

Access MongoDB Shell

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

mongo

Output:

[root@my ~]# mongo
MongoDB shell version v4.2.4-rc0-5-g24b6ee2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d9b4464f-46c1-411d-9e67-497f6df637bb") }
MongoDB server version: 4.2.4-rc0-5-g24b6ee2
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
                http://docs.mongodb.org/
Questions? Try the support group
                http://groups.google.com/group/mongodb-user
Server has startup warnings:
2020-03-18T10:59:27.738-0400 I  STORAGE  [initandlisten]
2020-03-18T10:59:27.738-0400 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-03-18T10:59:27.738-0400 I  STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2020-03-18T10:59:29.934-0400 I  CONTROL  [initandlisten]
2020-03-18T10:59:29.934-0400 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-03-18T10:59:29.934-0400 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-03-18T10:59:29.934-0400 I  CONTROL  [initandlisten]
2020-03-18T10:59:29.934-0400 I  CONTROL  [initandlisten]
2020-03-18T10:59:29.934-0400 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-03-18T10:59:29.934-0400 I  CONTROL  [initandlisten] **        We suggest setting it to 'never'
2020-03-18T10:59:29.934-0400 I  CONTROL  [initandlisten]
---
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.

Creating MongoDBan 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: "P@ssword@2019",
...  roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
...  }
...  )

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("d1c68355-e7b5-4c09-a4fd-4c97537e251b"),
                "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@my ~]# mongo
MongoDB shell version v4.2.4-rc0-5-g24b6ee2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName                                                                                        =mongodb
Implicit session: session { "id" : UUID("9a5f9b58-83a4-4375-ba96-53f5c7841b38")                                                                                         }
MongoDB server version: 4.2.4-rc0-5-g24b6ee2
> show users
2020-03-18T11:33:53.314-0400 E  QUERY    [js] uncaught exception: Error: command                                                                                         usersInfo requires authentication :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1638:15
shellHelper.show@src/mongo/shell/utils.js:883:9
shellHelper@src/mongo/shell/utils.js:790:15
@(shellhelp2):1:1
>

To authenticate, simply pass the credentials as shown.

db.auth('mongod_admin', 'P@ssword@2019')

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

show users

Output:

> db.auth('mongod_admin', 'P@ssword@2019')
1
> show users
{
                "_id" : "admin.mongod_admin",
                "userId" : UUID("d1c68355-e7b5-4c09-a4fd-4c97537e251b"),
                "user" : "mongod_admin",
                "db" : "admin",
                "roles" : [
                                {
                                                "role" : "userAdminAnyDatabase",
                                                "db" : "admin"
                                }
                ],
                "mechanisms" : [
                                "SCRAM-SHA-1",
                                "SCRAM-SHA-256"
                ]
}
>

To exit the database engine run.

exit