Introduction
Setting up a database is a fundamental step in many software development projects, and MongoDB is a popular choice for its flexibility and scalability. In this comprehensive guide, we’ll walk you through the entire process of how to install MongoDB 6.0 on Rocky Linux 9. Whether you’re an experienced database administrator or a beginner, we’ll provide you with clear instructions and explanations to help you get your MongoDB database up and running smoothly on your Rocky Linux 9.
Table of Contents
- Introduction
- Why Choose MongoDB 6.0?
- Prerequisites
- Install MongoDB 6.0 on Rocky Linux 9
- Step 1: System Update
- Step 2: Configure MongoDB Repository
- Step 3: Install MongoDB 6.0
- Step 4: Enable and Start MongoDB
- Step 5: Access MongoDB
- Step 6: Secure Your MongoDB Installation on Rocky Linux 9
- Step 7: Access MongoDB with Authentication.
- Step 8: Change MongoDB on Rocky Linux 9 default Path (optional)
- Conclusion
Why Choose MongoDB 6.0?
Before we dive into the installation process, let’s understand why MongoDB is a preferred choice for many developers and organizations, MongoDB 6.0 brings revolutionary improvements that elevate data management to a whole new level. With features like native time series collections, enhanced full-text search, and improved security options, MongoDB 6.0 empowers businesses to handle diverse data types with ease, while ensuring robust performance and scalability.
MongoDB employs a flexible data model, facilitating support for an extensive array of data types, such as strings, numbers, booleans, arrays, and embedded documents. Consequently, this adaptability renders it ideally suited for managing unstructured and semi-structured data.
Some key features of MongoDB include:
- Scalability: MongoDB is great at managing large amounts of data and can easily grow horizontally by spreading data across multiple servers or clusters.
- High Availability: MongoDB ensures data accessibility even when faced with hardware or network failures by providing replication and automated failover mechanisms.
- Flexible Querying: MongoDB supports robust querying capabilities, enabling advanced document searches, indexing, and a powerful aggregation framework for intricate data retrieval and analysis.
- Ad hoc Updates: MongoDB allows for on-the-fly updates and adjustments to documents without the need for a predefined schema, offering flexibility in data management.
- Integration and Ecosystem: MongoDB has a vast ecosystem with support for multiple programming languages, frameworks, and tools. It also integrates well with other technologies such as Apache Spark, Hadoop, and various cloud platforms.
Prerequisites
Before we dive into the installation process, make sure you have the following prerequisites in place:
- To get started, you’ll need a Rocky Linux 9 system with root or sudo access. If you haven’t set this up yet, you can follow our guide titled “Initial Setup Rocky Linux 9 Server: Secure and Efficient.” This setup can be done on either a physical machine or in a virtual environment like VMware or VirtualBox.
- Additionally, having some basic knowledge of command-line operations will be helpful for this process.
Install MongoDB 6.0 on Rocky Linux 9
Here’s a simple guide on how to install and configure MongoDB 6.0 on Rocky Linux 9. By following these steps, you can tap into the advantages it offers:
Step 1: System Update
Before installing MongoDB on Rocky Linux 9, it’s essential to ensure that your system is up to date. Open a terminal and run the following commands:
[samm@mongodb ~]$ sudo dnf update
[samm@mongodb ~]$ sudo dnf upgrade
When you run these commands, they will go out and get the most up-to-date package information and then update your system’s software packages to their latest versions. It’s crucial to keep your system updated to ensure both security and stability.
Additionally, MongoDB on Rocky Linux 9 requires some prerequisites, including the presence of a supported version of the OpenSSL library. You can install it using the following command:
[samm@mongodb ~]$ sudo dnf install openssl-devel
Step 2: Configure MongoDB Repository
MongoDB provides official repositories for various Linux distributions, including Rocky Linux 9. To add the MongoDB repository to your system, run the following command:
[samm@mongodb ~]$ sudo dnf install -y https://repo.mongodb.org/yum/redhat/mongodb-org/6.0/x86_64/RPMS/mongodb-org-6.0.0-1.el8.x86_64.rpm
When you use this command, it adds the MongoDB repository to the list of repositories that your system can access.
Step 3: Install MongoDB 6.0
Now that you’ve got the repository all set up, move on by refreshing your package list and then running the following command to install MongoDB 6.0:
[samm@mongodb ~]$ sudo dnf update
[samm@mongodb ~]$ sudo dnf install mongodb-org
Running this command will take care of installing the MongoDB package as well as all the dependencies it needs, all in one go. Once the installation is complete, you’re ready to configure, enable and start MongoDB on your Rocky Linux 9.
Step 4: Enable and Start MongoDB
Once MongoDB is installed, you can start the MongoDB service and configure it to launch automatically on system boot on your Rocky Linux 9. Use the following commands:
[samm@mongodb ~]$ sudo systemctl enable mongod
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.
[samm@mongodb ~]$ sudo systemctl start mongod
Running these commands will start MongoDB right away and configure it to launch automatically whenever your system starts up.
To ensure that MongoDB on your Rocky Linux 9 is running without issues, you can use the following command to check its status:
[samm@mongodb ~]$ sudo systemctl status mongod
● mongod.service - MongoDB 6.0.5 Document Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-03-20 23:55:47 WIB; 7s ago
Docs: https://docs.mongodb.org/manual
Main PID: 1200 (mongod)
Memory: 62.6M
CPU: 582ms
CGroup: /system.slice/mongod.service
└─1200 /usr/bin/mongod --config /etc/mongod.conf
Mar 20 23:55:47 mongodb systemd[1]: Started MongoDB 6.0.5 Document Database Server.
If MongoDB is running smoothly on yoyr Rocky Linux 9, the status message will confirm that it’s active and operational. After that, you can proceed to check the installed version of MongoDB.
[samm@mongodb ~]$ mongod --version
db version v6.0.5
Build Info: {
"version": "6.0.5",
"gitVersion": "c9a99c120371d4d4c52cbb15dac34a36ce8d3b1d",
"openSSLVersion": "OpenSSL 1.1.1n 15 Mar 2022",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "debian11",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
Step 5: Access MongoDB
To work with MongoDB on Rocky Linux 9 and carry out database tasks, you can easily access the MongoDB shell by executing:
$ mongosh 127.0.0.1:27017
When you run this command, it should start the MongoDB shell, which confirms that your installation has been successful.
Step 6: Secure Your MongoDB Installation on Rocky Linux 9
By default, MongoDB doesn’t require authentication. However, for better security, it’s advisable to enable authentication. Start by creating an admin user with the required privileges:
Create the Admin User
To create an admin user in MongoDB on Rocky Linux 9, you can follow these steps:
In the MongoDB shell, switch to the built-in admin
database, which is typically used for administrative tasks, Then, you can create the admin user
. Replace adminUser
and adminPassword
with your desired username and password for the admin user:
test> use admin
switched to db admin
admin> db.createUser({ user: "mongo-admin", pwd: "m0ngO4dm1n2023", roles: [{role: "root", db: "admin"}]})
{ ok: 1 }
admin> db.system.users.find()
[
{
_id: 'admin.mongo-admin',
userId: new UUID("af967ad4-9d24-4617-9615-80e793b6b5c1"),
user: 'mongo-admin',
db: 'admin',
credentials: {
'SCRAM-SHA-1': {
iterationCount: 10000,
salt: 'I0esBbduvM8pmHQ2fp2leg==',
storedKey: 'KQb8b3TSF6py6O75FAwSO3mFZ7w=',
serverKey: 'kPqwkvgHRsqIjgZ+c2Hl4FhpBis='
},
'SCRAM-SHA-256': {
iterationCount: 15000,
salt: '8rEzL0MdSZ2WABlcUTDchV3kt2x48epC+IHtKQ==',
storedKey: 'FrryrJLaIABb7PEebk2zYSfUQa/P9ATkyEWP+TgPZQo=',
serverKey: 'JLFgZVMZdLUt4zKXdjswysoLkSKlqEAFWc1SvqNJvyQ='
}
},
roles: [ { role: 'root', db: 'admin' } ]
}
]
Afterward, create a user named "mongo-admin
” – feel free to modify this name as per your preference. Once you’ve added the provided script, it’s crucial to set up a password for this user.
Add Database and User MongoDB
To add database and user MongoDB, execute the following commands:
Create Database
admin> use sammlinux_db
switched to db sammlinux_db
Add User Read Wirite (R/W)
sammlinux_db> db.createUser({ user: "samm-rw", pwd: "s4mm&2023", roles: [{role: "readWrite", db: "sammlinux_db"}]})
Add User Read Only (R/O)
sammlinux_db> db.createUser({ user: "samm-ro", pwd: "s4mm#2023", roles: [{role: "read", db: "sammlinux_db"}]})
Enabling Authentication
MongoDB 6.0 configuration files are stored in the /etc/mongod.conf
directory. Here, you can customize settings such as enabling Authentication and specifying the listening IP address and port to suit your needs. To enable authentication, you can use your preferred text editor to edit the MongoDB configuration file. Let’s proceed with making these changes:
[samm@mongodb ~]$ sudo vi /etc/mongod.conf
Add the following lines:
net:
port: 27017
bindIp: 127.0.0.1,172.32.1.80
security:
authorization: enabled
Then restart your mongoDB service, using this command:
[samm@mongodb ~]$ sudo systemctl restart mongod
Step 7: Access MongoDB with Authentication.
At this point, it’s necessary for clients to provide authentication to gain access to the database. To confirm that MongoDB is correctly installed and operational, you can connect to the MongoDB shell. Here’s the syntax for authentication:
[samm@mongodb ~]$ mongosh 127.0.0.1:27017 -u "mongo-admin" -p --authenticationDatabase "admin"
Enter password: m0ngO4dm1n2023
When you want to access MongoDB from a client, you can do so by using the server’s IP address followed by a colon:
[samm@mongodb ~]$ mongosh 172.32.1.80:27017 -u "mongo-admin" -p --authenticationDatabase "admin"
Enter password: m0ngO4dm1n2023
Enter the password when prompted. You should now be connected to the MongoDB shell.
Current Mongosh Log ID: 63955de17d5b6ddaa159e24e
Connecting to: mongodb://<credentials>@172.32.1.80:27017/?directConnection=true&authSource=sammlinux_db&appName=mongosh+1.6.2
Using MongoDB: 6.0.5
Using Mongosh: 1.6.2
For more information and documentation about mongosh, you can visit: https://docs.mongodb.com/mongodb-shell/
In MongoDB 6, you can use the show dbs
command to display a list of available databases. Open the MongoDB shell and run the following command:
test> show dbs
sammlinux_db 40.00 KiB
test>
This will list all the databases present on your MongoDB server, along with their sizes. Keep in mind that databases with no data may not appear in this list until they have some data stored in them.
Step 8: Change MongoDB on Rocky Linux 9 default Path (optional)
By default, MongoDB on your Rocky Linux 9 stores its data in the directory located at /var/lib/mongo. However, this directory can be altered as below.
Stop the MongoDB service.
To stop MongoDB on Rocky Linux, you can use the systemctl
Here’s how you can do it:
[samm@mongodb ~]$ sudo systemctl stop mongod
Create a new directory for MongoDB data.
Now, you can create it, for example : /database/mongodb
directory. and set the appropriate permissions for the directory to allow MongoDB to write data to it:
[samm@mongodb ~]$ sudo mkdir -p /database/mongodb
[samm@mongodb ~]$ sudo mkdir -p /database/mongodb/data
[samm@mongodb ~]$ sudo mkdir -p /database/mongodb/log
[samm@mongodb ~]$ sudo chown mongodb.mongodb -R /database/mongodb
Copy contents to the new directory.
If you’ve stored data in the default data location and wish to retain it, it’s crucial to move it to the new spot. For instance, if your data was previously in /var/lib/mongo
, and you want to shift it to the new location, /database/mongodb
, you can smoothly do this using the “rsync -av
” command.
[samm@mongodb ~]$ sudo rsync -av /var/lib/mongodb /database/mongodb/data
Rename the old directory for backup.
[samm@mongodb ~]$ sudo mv /var/lib/mongodb /var/lib/mongodb.bak
After making these changes, proceed to restart the MongoDB service. This step will ensure that the modifications take effect. As a result, MongoDB will begin utilizing the new directory you’ve created to store its data.
[samm@mongodb ~]$ sudo systemctl restart mongod
Conclusion
Congratulations, After successfully completing the install of MongoDB 6.0 on your Rocky Linux 9 server, configuring it, and ensuring its security, you are now all set to begin using MongoDB for your database requirements. MongoDB’s flexibility and scalability make it a fantastic choice for a wide variety of applications.Whether you’re building a small project or a large-scale application, MongoDB can handle your data storage needs.Whether you’re dealing with time series data, full-text search, or security considerations, MongoDB 6.0 has you covered.
In this tutorial, we covered each step clearly, from preparing your Rocky Linux 9 system to configuring MongoDB and enabling authentication. If you encounter any issues or have further questions, consult the MongoDB documentation or seek assistance from the MongoDB community.
Now, you have the power of MongoDB 6.0 at your fingertips, so you’re ready to store and manage your data efficiently on Rocky Linux 9. Feel free to enjoy exploring the world of NoSQL databases with MongoDB!
Also Read Our Other Guides :
- How To Install MongoDB 6.0 on Debian 10 & 11
- How To Install MongoDB 6.0 on Ubuntu Server 22.04
- How To Install Node.js on Rocky Linux 9 With 3 Different Ways
- How To Install MySQL 8.0 on Rocky Linux 9
Finally, now you have learned how to install MongoDB 6.0 on your Rocky Linux 9 system.