How to Install MongoDB on CentOS 8 (Community Edition Tutorial)

How to Install MongoDB on CentOS 8 (Community Edition Tutorial). In this article, we will introduce MongoDB, its features, benefits, and then we will move on to the installation of its Community Edition on CentOS 8.

MongoDB server is an open source NoSQL database that has been gaining popularity in recent years. It is a cross platform document oriented database program. It can be used on different platforms and languages like Perl, Java, C++, Ruby, Python, etc.

What Is MongoDB?

MongoDB is an open source NoSQL database management program, which is an alternative to traditional relational databases. It is a tool that you can use to manage, store and retrieve document oriented information.

It is primarily preferred by organizations working with large sets of data. It is because it is capable of supporting various forms of databases.

Rosed in the mid 2000s under the NoSQL banner, it immediately grabbed the attention of the user as big data applications and other processing jobs involving data fit well in this model. Their architecture comprises collections and documents rather than tables and rows.

MongoDB Features

MongoDB is a document database with the scalability and flexibility that provides the following features:

  • Every database constitutes collections that, in turn, contain documents. These documents differ in the varying number of fields as well as their size and content.
  • In MongoDB, the rows do not require schema content beforehand. Here, you can create fields on the fly.
  • It offers scalability, which means that companies all across the world have clusters with hundred plus nodes run in the database.
  • It has a more in line document structure that helps developers construct their classes and objects in their respective programming languages.
  • Its data model allows you to represent hierarchical relationships to store arrays and other more complex structures very effortlessly.

Benefits Of MongoDB

MongoDB offers several benefits that are as follows:

Complete Cloud Based Application Data Platform

MongoDB is a lot more than a database. It is a complete cloud based application that provides MongoDB Atlas, where you can access a collection of services. They are integrated nicely with the database. Among other things, you will also have Performace Advisor, Atlas Search, MongoDB Charts, Multi cloud deployments, etc.

Highly Supportive And Provide Code Native Access

Several databases force you to use heavy wrappers, like ORMs (Object Relational Mappers) to get data into object form to use in a program. Since MongoDB stores and represent data in a document format, you can access it from any language.

Sturdy Querying And Analytics

With MongoDB you can access data with ease. It also rarely requires any joins and transactions. However, it is more than the task in terms of complex querying. With its query API, you can query deep into documents and also perform complex analytics pipelines.

Sharding

Sharding in MongoDB is a method for distributing data across multiple machines.

High Performace

The document model used by MongoDB embeds information inside a single document. This way, you don’t have to rely on expensive join operations from a traditional relational database. By doing so, the queries became much faster and returns all the necessary information in a single call to the database.

Cost Effective

MongoDB provides several flexible approaches. When you use the cloud based MongoDB Altas, it enables you to choose the size of an instance that goes well with your needs. Here, you can also adjust your cluster to automatically scale if required. It helps you to keep your cost at a minimum while still providing the flexibility to handle instant traffic bursts.

Flexible Document Schemas

The document model of MongoDB enables you to model and manipulate any data structure virtually. It supports creating explicit schemas and validating data. Its flexibility is considered an incredible asset for handling real world data and changes in requirements or environments.

User Friendly Design

If you are used to bringing down your site or application to change the structure of the data, MongoDB is the best option. MongoDB does not require any downtime to change schemas. You can start writing new data in it at any time without disrupting its operations.

Effortless Horizontal Scale Out With Sharding

MongoDB is designed to be a distributed database. You can create clusters with real time replications and shard high throughput collections in multiple clusters. This way, you will be able to sustain your performance and scale horizontally.

In this post, we will show you how to install MongoDB on CentOS 8.

Follow this article about how to Install MongoDB on CentOS 8 (Community Edition Tutorial) to install it on your machine.

Install MongoDB on CentOS 8 (Community Edition Tutorial)

Prerequisites

  • A root user or a user with sudo privileges.

Add MongoDB Repository

By default, the MongoDB package is not included in the CentOS 8 default repo. So you will need to create a repo for MongoDB. You can create it with the following command:

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

Add the following lines:

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

				
			

Save and close the file, then verify the MongoDB repo using the following command:

				
					dnf repolist
				
			

You should see the following output:

				
					repo id                                             repo name
appstream                                           Centos 8 - AppStream
baseos                                              Centos 8 - BaseOS
epel                                                Extra Packages for Enterprise Linux 8 - x86_64
epel-modular                                        Extra Packages for Enterprise Linux Modular 8 - x86_64
extras                                              Centos 8 - Extras
mongodb-org-4.4                                     MongoDB Repository

				
			

Install MongoDB Server

Now, you can run the following command to install the MongoDB server package on your server.

				
					dnf install mongodb-org 
				
			

You should see the following output:

				
					MongoDB Repository                                                                                            145 kB/s |  41 kB     00:00    
Dependencies resolved.
==============================================================================================================================================
 Package                                          Architecture           Version                        Repository                       Size
==============================================================================================================================================
Installing:
 mongodb-org                                      x86_64                 4.4.14-1.el8                   mongodb-org-4.4                  11 k
Upgrading:
 cyrus-sasl-gssapi                                x86_64                 2.1.27-6.el8_5                 baseos                           49 k
 cyrus-sasl-lib                                   x86_64                 2.1.27-6.el8_5                 baseos                          122 k
 cyrus-sasl-plain                                 x86_64                 2.1.27-6.el8_5                 baseos                           46 k
Installing dependencies:
 cyrus-sasl                                       x86_64                 2.1.27-6.el8_5                 baseos                           95 k
 mongodb-database-tools                           x86_64                 100.5.3-1                      mongodb-org-4.4                  50 M
 mongodb-org-database-tools-extra                 x86_64                 4.4.14-1.el8                   mongodb-org-4.4                  23 k
 mongodb-org-mongos                               x86_64                 4.4.14-1.el8                   mongodb-org-4.4                  17 M
 mongodb-org-server                               x86_64                 4.4.14-1.el8                   mongodb-org-4.4                  22 M
 mongodb-org-shell                                x86_64                 4.4.14-1.el8                   mongodb-org-4.4                  14 M
 mongodb-org-tools                                x86_64                 4.4.14-1.el8                   mongodb-org-4.4                  11 k

Transaction Summary
==============================================================================================================================================

				
			

Once the MongoDB is installed, you can verify the MongoDB version using the following command:

				
					mongod --version
				
			

You will get the MongoDB version in the following output:

				
					db version v4.4.14
Build Info: {
    "version": "4.4.14",
    "gitVersion": "0b0843af97c3ec9d2c0995152d96d2aad725aab7",
    "openSSLVersion": "OpenSSL 1.1.1k  FIPS 25 Mar 2021",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel80",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

				
			

In this part of our article how to Install MongoDB on CentOS 8 (Community Edition Tutorial) is to start MongoDB service.

Start and Enable MongoDB Service

At this point, the MongoDB server is installed on your server. Now, you can start the MongoDB service using the following command:

				
					systemctl start mongod
				
			

You can check the status of the MongoDB service using the following command:

				
					systemctl status mongod
				
			

You should see the following output:

				
					● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2022-06-18 04:09:00 UTC; 4s ago
     Docs: https://docs.mongodb.org/manual
  Process: 13245 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=0/SUCCESS)
  Process: 13243 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 13241 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
  Process: 13239 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
 Main PID: 13247 (mongod)
   Memory: 64.7M
   CGroup: /system.slice/mongod.service
           └─13247 /usr/bin/mongod -f /etc/mongod.conf

Jun 18 04:08:59 centos8 systemd[1]: Starting MongoDB Database Server...
Jun 18 04:08:59 centos8 mongod[13245]: about to fork child process, waiting until server is ready for connections.
Jun 18 04:08:59 centos8 mongod[13245]: forked process: 13247
Jun 18 04:09:00 centos8 mongod[13245]: child process started successfully, parent exiting
Jun 18 04:09:00 centos8 systemd[1]: Started MongoDB Database Server.

				
			

Next, enable the MongoDB service so that it starts after the system reboot:

				
					systemctl enable mongod
				
			

Create MongoDB Admin User

By default, user authentication is not configured in MongoDB. For security reasons, it is essential to enable the authentication and set an admin password for MongoDB.

First, connect to the MongoDB console using the following command:

				
					mongo
				
			

Once you are connected to MongoDB shell, you should see the following shell:

				
					MongoDB shell version v4.4.14
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f4667c8e-e11a-4575-9ecc-1f252d0515a0") }
MongoDB server version: 4.4.14
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
>

				
			

Next, switch the database to admin using the following command:

				
					use admin
				
			

Next, create an admin user and set a password with the following command:

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

You should see the following output:

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

				
			

Now, verify the created user with the following command:

				
					show users
				
			

You should see all users in the following output:

				
					{
	"_id" : "admin.admin",
	"userId" : UUID("932728e0-2141-4eb9-9ab0-c6c816ae77b5"),
	"user" : "admin",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

				
			

After setting up an admin user and password, you will need to edit the MongoDB configuration file and enable the authentication.

				
					nano /etc/mongod.conf
				
			

Change the following lines:

				
					security:
 authorization: enabled
				
			

Save and close the file, then restart the MongoDB service to apply the changes.

				
					systemctl restart mongod
				
			

Verify MongoDB Authentication

At this point, MongoDB is secured with username and password. You can now connect to the MongoDB shell using the following command:

				
					mongo --port 27017 --authenticationDatabase "admin" -u "admin" -p
				
			

You will be asked to provide an admin password to connect to the MongoDB:

				
					MongoDB shell version v4.4.14
Enter password: 
				
			

Provide your admin password and press the Enter key. Once you are connected to the MongoDB, you should see the MongoDB shell in the following output:

				
					connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("19580f9b-bebf-4ec9-a9eb-e46c2923ece3") }
MongoDB server version: 4.4.14
> 
				
			

How to Install MongoDB on CentOS 8 (Community Edition Tutorial) Conclusion

In this guide, we explained how to install MongoDB Community Edition on CentOS 8. Due to its simplicity and flexibility, robust features and native read/write interface for JavaScript, MongoDB is gaining more and more popularity among web developers. MongoDB has grown largely from the level of being a simple JSON data store to now the most popular NoSQL database solution.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x