How to Install MongoDB on RHEL 7 Linux

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.

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

Prerequisites

Before starting the installation and configuration process, make sure you have RHEL 7 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:

[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.

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 7 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.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

fourteen − four =

Related Articles