How to Install MongoDB on CentOS 8

Choose a different version or distribution

Introduction

Before we begin talking about how to install MongoDB on CentOS 8, let’s briefly understand - What is MongoDB?

It is a popular NoSQL database that offers scalability and flexibility. Installing MongoDB on CentOS 8 is a straightforward process, allowing you to utilize its powerful features for your applications' data storage and retrieval needs.

In the MongoDB database, one can store data in a flexible manner, where fields could differ from one document to another. A predefined scheme is not mandatory, and the structure of data could be changed over time.

In this tutorial, you will install MongoDB on CentOS 8. Also, we will answer some FAQs regarding the MongoDB installation.

Step 1 - Installing MongoDB

For CentOS 8 core repositories, MongoDB is not available. So, first, the MongoDB repository shall be enabled, and the packages would be installed.

Carefully follow the steps given below as root or user with sudo privileges to install MongoDB on your CentOS 8 system.

1) First, enable the MongoDB repository by creating a new repository file named, mongodb-org.repo in the /etc/yum.repos.d/ directory using the following command:

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

In case, you want to install MongoDB of one of the previous versions, replace 6.0 with your preferred version.

2) Install the MongoDB-org meta-package using the following command:

sudo dnf install mongodb-org

While the installation is going on, you will be alerted to import the MongoDB GPG key. Just type y and press Enter.

These packages should be installed in the system by now, as a part of the mongodb-org package:

  • mongodb-org-server - The mongod daemon.
  • mongodb-org-mongos - The mongos daemon.
  • mongodb-org-shell - The Mongo shell which is an interactive Javascript UI to MongoDB.
  • mongodb-org-tools - Contains several MongoDB tools for transferring data, statistics, and other information.

3) After the completion of installation, activate and initiate the services of MongoDB using the following command:

sudo systemctl enable mongod --now

4) For verification of the installation, link to the MongoDB server. Then, print the server version by typing:

mongosh

5) After that, to get information regarding the MongoDB version, use this:

db.version()

Then, the version will appear in this manner:

Output

6.0.6

Step 2 - Configuring MongoDB

The configuration file of MongoDB is titled mongod.conf and inside the /etc directory. The format of this file is YAML.

sudo vi /etc/mongod.conf

1) Generally, default configuration settings are adequate but for production environments, it is safer to refrain from commenting on the security section and activating it as shown below:

security:
  authorization: enabled

The option for authorization allows Role-Based Access Control (RBAC) which monitors a user's database-related activities. In case, this option is not enabled, every user would be able to gain access to any database and execute any action.

2) Restart the service after effecting changes in the configuration file.

sudo systemctl restart mongod

Step 3 - Creating Administrative MongoDB User

On activation of MongoDB authentication, an administrative user is to be created to access and oversee the MongoDB instance.

1) The very first step is to access the MongoDB shell with the following command:

mongosh

2) Put this command for connecting to the admin database:

use admin
Output

switched to db admin

3) Generate a new name for the user named mongoAdmin with the userAdminAnyDatabase role:

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

{ ok: 1 }
💡
You may choose any name for the administrative MongoDB user.

4) Finally, exit from the Mongo shell with the following command:

quit()

5) To examine the changes implemented, you have to access the Mongo shell through the admin user created earlier:

mongosh -u mongoAdmin -p --authenticationDatabase admin
Output

Enter password:
use admin
Output

switched to db admin

6) At this point, you need to print the users with 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"
	]
}

Advantages to Install MongoDB on CentOS 8

1. Flexible Data Model: MongoDB's document-based data model allows for dynamic and flexible schemas, making it easy to handle evolving data structures.

2. Horizontal Scalability: MongoDB supports horizontal scaling through sharding, allowing you to distribute data across multiple nodes for improved performance and increased storage capacity.

3. High Performance: MongoDB's efficient indexing and query optimization capabilities provide fast data retrieval, making it suitable for applications that require real-time data access and low latency.

4. Robust Replication: MongoDB offers built-in replication, ensuring high availability and data redundancy by automatically synchronizing data across multiple replica sets.

5. Rich Functionality: MongoDB provides a comprehensive set of features, including text search, geospatial indexing, and analytics capabilities, making it suitable for a wide range of use cases.

FAQs to Install MongoDB on CentOS 8

Can I integrate MongoDB with programming languages like Python on CentOS 8?

 Yes, MongoDB provides language-specific drivers to interact and work with MongoDB databases using various programming languages, including Python, on CentOS 8.

Can I install MongoDB using the package manager?

Yes, you can install MongoDB using the dnf package manager on CentOS 8, which simplifies the installation process.

Which version of MongoDB should I install on CentOS 8? 

It is recommended to install the latest stable version of MongoDB on CentOS 8 to benefit from the latest features, bug fixes, and security patches.

How do I start and stop the MongoDB service on CentOS 8?

Once MongoDB is installed, you can use the systemctl command to start, stop, restart, or check the status of the MongoDB service.

What are the default ports used by MongoDB? 

By default, MongoDB listens on port 27017 for client connections, while port 27018 is for internode communications in a replica set.

Can I upgrade MongoDB to a newer version on CentOS 8?

Yes, you can upgrade MongoDB to a newer version by following the official MongoDB documentation, which provides step-by-step instructions for the upgrade process.

How can I restore a MongoDB backup on CentOS 8?

To restore a MongoDB backup, you can use tools like mongo-restore to import the backed-up data into a MongoDB instance.

Conclusion

We hope this detailed tutorial helped you understand how to install MongoDB on CentOS 8. To learn more about MongoDB installation, check out the official MongoDB documentation.

By leveraging MongoDB's capabilities, CentOS 8 users can enjoy a reliable and efficient database solution that empowers their applications with flexibility, scalability, and high performance.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.