Install and Configure MongoDB on RHEL9/CentOS9

Install and configure MongoDB

As a Linux professional, mastering how to install and configure MongoDB will empower you to leverage the full power of this popular NoSQL database for efficient and scalable data management.

Table of Contents

๐Ÿ”ˆIntroduction

MongoDB is a popular NoSQL document-oriented database that is known for its high performance, scalability, and flexibility. It was first released in 2009 by the MongoDB company and has since become a widely-used database among developers and enterprises. In this article, we will provide a step-by-step guide on how to install and configure MongoDB on RHEL9/CentOS9.


โœ… What is MongoDB?

MongoDB is a NoSQL database that is designed to handle large volumes of unstructured and structured data. Unlike traditional relational databases, which use a fixed schema, MongoDB uses a dynamic schema, which allows for greater flexibility when storing data. MongoDB is also highly scalable, allowing you to add more nodes to a cluster as your data grows, and it provides high availability through replication.


๐Ÿ“‹ Most Popular Features

MongoDB’s most popular features include:

  • High Performance: MongoDB can handle high volumes of data with ease and is designed to be highly performant even when dealing with complex queries.
  • Flexible Data Model: MongoDB’s dynamic schema allows for flexible data modeling, making it easy to store and retrieve data of different types and structures.
  • High Availability: MongoDB provides high availability through automatic failover and replication, ensuring that your data is always available.
  • Scalability: MongoDB is designed to be highly scalable, allowing you to add more nodes to a cluster as your data grows.
  • Rich Query Language: MongoDB provides a rich query language that allows you to perform complex queries on your data, including queries that involve geospatial data.

Installation and Configuration of MongoDB on RHEL9/CentOS9

๐ŸŸข Step1: MongoDB Repositoryย 

To install MongoDB on RHEL9/CentOS9, you need to add the MongoDB repository to your system. Completing the following steps:

โ–ถ๏ธ Export variables

There areย several versionsย of MongoDB to choose from. You can specify which version you’d like to install by using the export command:

				
					export MONGODB_VERSION=4.4
				
			
				
					export RHEL_VERSION=9
				
			
Install and configure MongoDB

Photo by admingeek from Infotechys

Import the appropriate GPG key for the version of MongoDB installed:

				
					sudo rpm --import https://pgp.mongodb.com/server-$MONGODB_VERSION.asc
				
			

In this example, we’re installing MongoDB version 4.4 on a RHEL 9 server. The command below adds the appropriate repository, using the variables we’ve previously defined.

				
					sudo dnf config-manager --add-repo=https://repo.mongodb.org/yum/redhat/${RHEL_VERSION}/mongodb-org/${MONGODB_VERSION}/x86_64
				
			
				
					Adding repo from: https://repo.mongodb.org/yum/redhat/9/mongodb-org/4.4/x86_64
				
			

๐ŸŸข Step 2: Install MongoDB

Once the repository has been successfully configured, run the command below to install MongoDB:

				
					sudo dnf install mongodb-org
				
			

๐ŸŸข Step 3: Start and Enable MongoDB

After installing MongoDB, start and enable the MongoDB service using the following command:

				
					sudo systemctl enable --now mongod
				
			

๐ŸŸข Step 4: Verify the Installation

To verify that MongoDB is installed correctly, you can run the following command to check if the service is running:

				
					$ sudo systemctl status mongod
				
			

Or, verify the version:

				
					$ mongo
				
			

you should see output similar to:

				
					MongoDB shell version v4.4.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("adcec2f0-d6c7-4cf7-ac11-0f3bbea4d1a9") }
MongoDB server version: 4.4.6
				
			

๐Ÿ“‹ Creating a Database

To create a new database, you can use the following command:

				
					$ use mydatabase
				
			

This will create a new database called “mydatabase”.


๐Ÿ“‹ Creating a Collection

To create a new collection in MongoDB, you can use the following command:

				
					$ db.createCollection("mycollection")
				
			

This will create a new collection called “mycollection” in the current database.


๐Ÿ“‹ Inserting Data

To insert data into a collection, you can use the following command:

				
					$ db.mycollection.insertOne({"name": "John", "age": 30})
				
			

This will insert a new document into the “mycollection” collection with the fields “name” and “age”.


๐Ÿ“‹ Retrieving Data

To retrieve data from a collection, you can use the following command:

				
					$ db.mycollection.find()
				
			

This will retrieve all the documents in the “mycollection” collection.


โœ… Install and configure MongoDB: Best Practices

Here are some best practices for working with MongoDB:

  • Plan your schema carefully: MongoDB is a flexible document database, but it is important to plan your schema carefully to ensure that it will scale and perform well over time.
  • Use indexing: Use indexing to improve query performance and reduce query response time. Create indexes on fields that are frequently used in queries.
  • Monitor your database: Monitor your MongoDB database regularly to identify performance issues and prevent downtime. Use monitoring tools, such as MongoDB Cloud Manager or Ops Manager, to keep track of your database’s performance.
  • Secure your database: MongoDB is a powerful database, but it is also vulnerable to security threats. Secure your database by using authentication and encryption to protect your data.
  • Backup your data: Backup your MongoDB data regularly to ensure that you can recover from data loss or corruption. Use a backup solution that is designed to work with MongoDB, such as MongoDB Cloud Backup or Ops Manager Backup.
  • Optimize your queries: Optimize your queries to reduce query response time and improve database performance. Use the MongoDB Query Profiler to identify slow queries and make improvements.
  • Keep software up-to-date: Keep your MongoDB software up-to-date to ensure that you are running the latest version and taking advantage of the latest features and bug fixes.
  • Use a sharded cluster for large datasets: For very large datasets, use a sharded cluster to distribute data across multiple MongoDB instances. This will improve scalability and performance.

By following these best practices, you can ensure that your MongoDB database is running efficiently and effectively, and that you are getting the most out of this powerful NoSQL database.


๐Ÿ Conclusion

In this article, we have provided a step-by-step guide on how to install and configure MongoDB on RHEL9/CentOS9. We also discussed some of MongoDB’s most popular features and provided some basic usage examples. By following the steps outlined in this article, you can get started with using MongoDB on RHEL9/CentOS9 and take advantage of its high performance, scalability, and flexibility.

Did you find this article helpful? Your feedback is invaluable to us! Feel free to share this post with those who may benefit, and let us know your thoughts in the comments section below.


๐Ÿ“• Related Posts

Leave a Reply

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