MQ-Jr
MQ-Jr
unixcop Admin

How to Install MongoDB in CentOS 8

Introduction

MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL).

MongoDB is a popular document-based and general-purpose NoSQL database engine that stores data in JSON format.

In this tutorial, you will learn how to install MongoDB on CentOS 8.

Install MongoDB

Just follow the steps below

  • Add MongoDB Repository
vim /etc/yum.repos.d/mongodb.repo
  • Then add the following:
[mongodb-org-5]
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
  • The next step will be to install MongoDB using the command below:
dnf install mongodb-org
  • Start and enable MongoDB
systemctl start mongod
systemctl enable mongod
  • Check status of MongoDB
systemctl status mongod
  • Confirm that Mongod service is listening.
netstat -ntlpu
OR
netstat -ntlpu | grep -i mongo
  • Access MongoDB Shell with the below command
mongo

Output:

  • Create a MongoDB Admin User
  • switch to the database admin
> use admin
  • Then create a new MongoDB user by running the code below
> db.createUser(
{
user: "mongod_admin",
pwd: "unixcop@Password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)

The output  should be as shown below:

  • List MongoDB users with:
> show users
  • Configure the authentication for MongoDB, because all users can access the shell and execute any commands, which is not recommended at all for security purposes. 
  • To enable authentication open the /lib/systemd/system/mongod.service file.
vim /lib/systemd/system/mongod.service
  • Then edit the Environment parameter as shown.
Environment="OPTIONS= --auth -f /etc/mongod.conf"
  • Reload the system and restart MongoDB.
systemctl daemon-reload
systemctl restart mongod
  • Again, Access the mongoDB shell with:
mongo
  • Then list users after switching to admin user, just type:
> use admin
> show users

You should get an error as shown.

  • To authenticate, simply pass the credentials as shown.
> db.auth('mongod_admin', 'unixcop@Password')
  • List users again
> show users
  • To exit the database engine run:
> exit

Conclusion

That’s it…

In this guide, we showed you how to install MongoDB on CentOS 8.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

Join us on Facebook