Saturday, October 31, 2020

How to Install MongoDB on RHEL 8 Linux

Must read

How to Setup a Firewall with UFW on Ubuntu 20.04

Nowadays, a Firewall is an essential utility and property of any system for security; by default Ubuntu Operating system having a firewall...

How to Install Ruby on Ubuntu 20.04 Linux

Nowadays, Ruby is the most popular language, especially for SaaS application development. It has a perfect and elegant syntax structure, and it...

How to Install Eclipse IDE on Ubuntu 20.04 Linux

Eclipse is the most famous and widely used Java integrated development environment (IDE). It supports many plugins to enhance the capabilities to...

The 30 most useful Linux commands

This article will introduce some practical and common Linux or Unix command , which is the Linux system administrators normally use the command . This article is not a...
SATISH KUMAR
I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.
- Advertisement -

MongoDB is an open-source and free NoSQL document based database system. Nowadays, it is prevalent as most application developers are using MongoDB, which can handle big data.

As a MongoDB is a NoSQL database, It stores data in JSON-like documents where fields can vary. You also get benefits with MongoDB like it doesn’t require any predefined database schema or data structure; it can be changed over time.

- Advertisement -

In this tutorial, we will explain the process of MongoDB installation step-by-step on RHEL 8 Linux using MongoDB’s official repositories.

Prerequisites

Before starting the installation and configuration process, make sure you have RHEL 8 installed system with sudo privileged user access to execute administrative commands.

Installing MongoDB

Here we follow a few steps to install MongoDB on the Red Hat system.

Step 1 – MongoDB official repositories enable

To enable mongodb repository on Red Hat Linux, we need to create a yum repos file for mongodb with name of mongodb-org.repo in the /etc/yum.repos.d/ directory using the following content:

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

Step 2 – Install MongoDB

After enabling the MongoDB repository, you can update the apt package list and install the mongodb-org package like any other package we are installing on Red Hat, as shown below:

$ sudo yum update
$ sudo yum install mongodb-org

The mongodb-org package will install all below package related to mongodb:

  • Mongodb-org-server – It is for MongoDB daemon (mongod) and init script along with configuration
  • Mongodb-org-mongos – It is for mongos daemon or service.
  • Mongodb-org-shell – The mongo shell provides a JavaScript interface to MongoDB, which is used to perform administrative tasks using the command-line.
  • Mongodb-org-tools provides several tools for mongodb to import and export data, check the data statistics, and have various other utilities.
- Advertisement -

Step 3 – Start MongoDB service

After completion of MongoDB installation, you can use the following command to start MongoDB service and enable the service to auto-start on system boot:

$ sudo systemctl start mongod
$ sudo systemctl enable mongod

Step 4 – Verify the Installation

To verify the MongoDB has successfully installed in the system, you should connect to the mongoDB database server using the mongo command, which will print the connection status where you can check the version of MongoDB, as shown below:

$ mongo

Once you get the mongo shell, type the following command to print the mongodb version:

db.version()
Output:

MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.10
{
  "authInfo" : {
    "authenticatedUsers" : [ ],
    "authenticatedUserRoles" : [ ]
  },
  "ok" : 1
}

In the above output message, the value of 1 for the ok field indicates success.

Configuring MongoDB

You can edit /etc/mongod.conf file to configure MongoDB instance. It is a YAML formatted configuration file.

The preconfigured or default settings is enough for most of the users. Still, to run MongoDB in production, it is recommended to uncomment the security configuration and enable settings for authorization, as shown below:

security:
  authorization: enabled

The authorization option provides Role-Based Access Control (RBAC) to provide access control for users on database resources and operations. Ig authorization is disabled, means each user having access to all database for performing any action.

After changing the configuration of the MongoDB server on the configuration file, need to restart mongod service to take new settings using the following command:

$ sudo systemctl restart mongod

You can find more option related to MongoDB configuration at Configuration file options on the documentation page.

Creating MongoDB Administrative User

If you have enabled the authentication in your MongoDB instance, you should create an administrative user to access and manage your MongoDB instance.

To create a user, first, you need to access the mongodb shell using the following command:

$ mongo

Once you get the MongoDB shell, use the following command to connect with the admin database:

use admin
Output:

switched to db admin

To create a user in MongoDB, you need to use the below command, where mongoAdmin is the username, and userAdminAnyDatabase is for User role:

db.createUser(
  {
    user: "mongoAdmin", 
    pwd: "changeMe", 
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
Output:

Successfully added user: {
	"user" : "mongoAdmin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}

You can exit the mongodb shell using the following command:

quit()

You can test new user credentials and access by using mongo shell to log in for the new user, as shown below:

$ mongo -u mongoAdmin -p --authenticationDatabase admin
use admin
Output:

switched to db admin

To get the information about users in the mongo shell, use the following command:

show users
Output:

{
	"_id" : "admin.mongoAdmin",
	"user" : "mongoAdmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

Conclusion

You have learned MongoDB installation and configuration in your RHEL 8 machine. To learn more about MongoDB, you can visit the MongoDB manual page.

If you have any problem with the installation or having any feedback, please comment below.

- Advertisement -
- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe to our newsletter

To be updated with all the latest news, offers and special announcements.

Latest article

How to Setup a Firewall with UFW on Ubuntu 20.04

Nowadays, a Firewall is an essential utility and property of any system for security; by default Ubuntu Operating system having a firewall...

How to Install Ruby on Ubuntu 20.04 Linux

Nowadays, Ruby is the most popular language, especially for SaaS application development. It has a perfect and elegant syntax structure, and it...

How to Install Eclipse IDE on Ubuntu 20.04 Linux

Eclipse is the most famous and widely used Java integrated development environment (IDE). It supports many plugins to enhance the capabilities to...

The 30 most useful Linux commands

This article will introduce some practical and common Linux or Unix command , which is the Linux system administrators normally use the command . This article is not a...

How to Reset the MySQL Root Password

Do you want to reset the MySQL root password? Sometimes we forget the root password and not able to log in on...