How to install and configure MongoDB On CentOS 8.1

To Install MongoDB on CentOS 8

Introduction:

MongoDB is a free and open-source document database that pertains to the databases family called NoSQL, which is different from the traditional table-based SQL databases like MySQL and PostgreSQL. It is saved in flexible, JSON-like documents where fields can vary from document to document and need a predefined schema, and data structure can be modified over time. This video will cover the method of installation MongoDB on CentOS 8.1

Installation Procedure:

Enable the MongoDB repository by creating a new repository file named mongodb-org.repo inside the /etc/yum.repos.d/ directory:

[root@linuxhelp ~]# Vim /etc/yum.repos.d/mongodb-org.repo
 [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

Install the mongodb-org meta-package:

[root@linuxhelp ~]# sudo dnf install mongodb-org
CentOS Linux 8 - AppStream                                                 294 kB/s | 6.3 MB     00:21    
CentOS Linux 8 - BaseOS                                                    551 kB/s | 2.3 MB     00:04    
CentOS Linux 8 - Extras                                                    2.4 kB/s | 1.5 kB     00:00    
CentOS Linux 8 - Extras                                                    8.7 kB/s | 8.6 kB     00:00    
Extra Packages for Enterprise Linux Modular 8 - x86_64                     8.8 kB/s | 9.3 kB     00:01    
Extra Packages for Enterprise Linux 8 - x86_64                             4.5 kB/s | 4.1 kB     00:00    
Extra Packages for Enterprise Linux 8 - x86_64                             529 kB/s | 8.6 MB     00:16    
[root@linuxhelp ~]# systemctl start mongod

Now enable the mongod service so that after boot up it start automatically

[root@linuxhelp ~]# systemctl enable mongod

To check status of service enter following command

[root@linuxhelp ~]# systemctl status mongod
● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-12-16 11:09:47 IST; 27s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 94188 (mongod)
   Memory: 94.0M
   CGroup: /system.slice/mongod.service
           └─94188 /usr/bin/mongod -f /etc/mongod.conf

Now enter to mongo shell provide the following command

[root@linuxhelp ~]# mongo
MongoDB shell version v4.2.11
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("04e9bf3f-3317-411c-bdf2-abf0c8fa4821") }
MongoDB server version: 4.2.11
Welcome to the MongoDB shell.
Server has startup warnings: 
2020-12-16T11:09:46.450+0530 I  STORAGE  [initandlisten] 
2020-12-16T11:09:46.450+0530 I  STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-12-16T11:09:47.232+0530 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-12-16T11:09:47.232+0530 I  CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-12-16T11:09:47.232+0530 I  CONTROL  [initandlisten] 
To enable free monitoring, run the following command: db.enableFreeMonitoring()
> use admin
switched to db admin
> db.createUser(
... {
... user:"mdadmin",
... pwd: "password",
... roles: [{role:"userAdminAnyDatabase", db: "admin"}]
... }
... )
Successfully added user: {
	"user" : "mdadmin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}
> 
> 
> 
> show users
{
	"_id" : "admin.mdadmin",
	"userId" : UUID("98b57c9b-cf80-4dca-9b05-c39085bf764c"),
	"user" : "mdadmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}
> quit()

To turn on authentication move into the following file

[root@linuxhelp ~]# vim /lib/systemd/system/mongod.service
Find the following line:
Environment="OPTIONS=--f /etc/mongod.conf"

Add the --auth option as follows:
Environment="OPTIONS= --auth -f /etc/mongod.conf"

Now reload the mongo.d service

[root@linuxhelp ~]# systemctl --system daemon-reload
 [root@linuxhelp ~]# systemctl restart mongod

Test mongo user authentication

[root@linuxhelp ~]# mongo
MongoDB shell version v4.2.11
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
MongoDB server version: 4.2.11
switched to db admin
> use admin
Now show users
> show users
An error message should display:
2020-12-16T11:16:42.222+0530 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:1661:15
shellHelper.show@src/mongo/shell/utils.js:883:9
shellHelper@src/mongo/shell/utils.js:790:15
@(shellhelp2):1:1

Next, use the following command to authenticate with the credentials created before

> db.auth('mdadmin','password')
1
Now show users to confirm
> show users
{
	"_id" : "admin.mdadmin",
	"userId" : UUID("98b57c9b-cf80-4dca-9b05-c39085bf764c"),
	"user" : "mdadmin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

With this, the installation of MongoDB on CentOS 8.1 comes to an end.

Tag : Mongodb CentOS
Comment
shiraziismailsoy
May 25 2022
Thank you (y)
Add a comment
FAQ
Q
Does MongoDB support SQL?
A
Not directly, no. However, MongoDB does support a rich query language of its own. For examples on using MongoDB’s query language, see MongoDB CRUD Operations
Q
Is mongodb .repo already exists?
A
No, we need to create and add the configuration
Q
What kind of NoSQL database MongoDB is?
A
MongoDB is a document-oriented database. It stores data in the form of BSON structure-based documents. These documents are stored in a collection.
Q
What is “Namespace” in MongoDB?
A
MongoDB stores BSON (Binary Interchange and Structure Object Notation) objects in the collection. The concatenation of the collection name and database name is called a namespace.
Q
What is MongoDB?
A
Mongo-DB is a document database that provides high performance, high availability, and easy scalability.