CentOS

How to install MongoDB on CentOS 8

How to install MongoDB on CentOS 8

MongoDB is a freely available document type database. It is open-source and comes under the family of NoSQL databases, which is much different from the traditional databases like PostgreSQL and MySQL. Data stored in MongoDB in a flexible form, like JSON documents. MongoDB doesn’t require any predefined data structure or table schema.

We will elaborate on how to install and configure the MongoDB database application on CentOS 8 in this tutorial.

Prerequisites

All commands should be executed under the root user.

Installation of MongoDB on CentOS 8

The MongoDB packages are not present in the default CentOS 8 repository. You need to enable the MongoDB official repository on your system to install related packages. While we are explaining the details of this article, the MongoDB 4.2.12 version is available as the latest version. Therefore,  you can also search for the latest MongoDB version before starting the installation. Open the ‘Terminal’ window to install the MongoDB databases. So, click on the terminal icon from the left sidebar application menu.

The following number of steps you need to perform as the root user to install and configure the MongoDB in the CentOS 8 system:

Step 1: Enable the MongoDB repository

Create a new repository file inside the /etc/yum.repos.d/ directory with the name MongoDB-org.repo and enable the MongoDB repository.

$ sudo nano /etc/yum.repos.d/mongodb-org.repo

Now, paste the following code in this file:

[mongodb-org-4.2]

name=MongoDB Repository

baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/

gpgcheck=1

enabled=1

gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc


To install any other version of MongoDB, just replace the 4.2 instances with your version.

Step 2: Install MongoDB org Package

Install the following meta package by using the below-given command:

$ sudo dnf install mongodb-org

When the above command is executed, it installs the various other packages with MongoDB such as mongodb-org-server, mongodb-org-mongos, mongodb-org-shell, and mongodb-org-tools. Press ‘y’ to continue the installation of these packages and then hit ‘Enter’. Again, press ‘y’ to successfully importing the MongoDB GPG key on your system.

Step 3: Start MongoDB services

Once the installation of MongoDB is completed, start the MongoDB services by running the below-mentioned command and then display the current service status:

$ sudo systemctl enable mongod --now
$ sudo systemctl status mongod

To know the installed version, connect to the MongoDB server by executing the following command:

$ mongo

Then, run the following command on MongoDB shell:

db.version()

The installed version displayed on the shell is shown in the following screenshot:

MongoDB configuration

Usually, the default configuration is sufficient. But, in most cases, when you are working in a production environment, you need to change the following configuration in the /etc/mongod.conf file as follows:

$ sudo nano /etc/mongod.conf

Find the security section and uncomment this section. Enable the authorization option for role-based user access.

security:
 authorization: enabled

After changing the configuration, type the following command to restart the MongoDB service:

$ sudo systemctl restart mongod

Create Admin user in MongoDB

If you have been enabled the authorization option, access the MongoDB shell prompt as follows:

$ mongo

Now, connect with the admin database by using the MongoDB shell as follows:

> use admin

Now, create an admin user with a suitable name and set the following role:

db.createUser(

>  {

    user: "mongoAdmin", 

    pwd: "mpaswrd", 

    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]

  }

)

The following output shows on the MongoDB shell:

Use the following command to exit from the shell.

> quit()

You can access the MongoDB shell as an admin user which you have created above:

$ mongo -u mongoAdmin -p --authenticationDatabase admin

Enter the password and switch to the admin database as follows:

> use admin

Now, show all user through the following command:

> show users

The following result shows on the terminal:

Conclusion

We have presented the MongoDB installation on CentOS 8 system in this article. Visit the MongoDB manual for more information about the usage. Tell us about your problems through the comments. Thanks!

 

Similar Posts