Today we will walk through how to install MongoDB 6.0 on Rocky Linux 9|AlmaLinux 9. MongoDB is a free and open-source NoSQL database management system. NoSQL (Not merely SQL) is a database technology that is used as an alternative to traditional relational databases. NoSQL databases are extremely handy when dealing with massive amounts of scattered data. MongoDB is a tool for managing document-oriented data, as well as storing and retrieving data.
High-volume data storage with MongoDB enables businesses to store enormous amounts of data while maintaining speedy performance. Ad hoc queries, indexing, load balancing, aggregation, server-side JavaScript execution, and other aspects of MongoDB are being used by businesses.
In contrast to relational databases, which utilize tables and rows, NoSQL databases like MongoDB employ collections and documents. Key-value pairs, the fundamental unit of data in MongoDB, are the building blocks of documents. Collections, which are the Java equivalent of SQL tables, house document sets. Programming languages including C, C++, C#, Go, Java, Python, Ruby, and Swift are all compatible with MongoDB.
Where is MongoDB used?
MongoDB may be used by a company for the following purposes:
- Storage. MongoDB is both vertically and horizontally scalable and can store enormous amounts of both structured and unstructured data. Indexes are used to improve search performance. Searches can also be performed using field, range, and expression queries.
- Integration of data. This combines data for applications, such as hybrid and multi-cloud apps.
- Descriptions of complex data structures. Document databases allow documents to be embedded to define nested structures (a structure within a structure) and may accept data variances.
- Load distribution. MongoDB may be distributed across several servers.
Aspects of MongoDB
MongoDB has the following features:
- Replication. For high availability, a replica set of two or more MongoDB instances is employed. Replica sets are made up of primary and secondary servers. The primary MongoDB server handles all read and write activities, while the secondary replica stores a copy of the data. If a primary replica fails, the backup replica is employed.
- Scalability. MongoDB allows both vertical and horizontal scalability. Vertical scaling works by adding additional power to a current machine, and horizontal scaling works by adding new machines to a user’s resources.
- Load balancing. By leveraging vertical or horizontal scalability, MongoDB manages load balancing without the need for a second, separate load balancer.
- Schema-less. MongoDB is a schema-less database, which implies it can manage data without requiring a schema.
- Document. When compared to SQL databases, data in MongoDB is kept in documents with key-value pairs rather than rows and columns, making the data more flexible.
Install MongoDB 6.0 on Rocky Linux 9|AlmaLinux 9
The following steps will guide you on how to install MongoDB 6.0 on Rocky Linux 9|AlmaLinux 9 successfully.
1. Update the system
Update your system first, then install Epel.
sudo dnf update
sudo dnf install epel-release -y
2. Include a MongoDB 6.0 repository on Rocky Linux 9|AlmaLinux 9
The standard Rocky Linux 9 |AlmaLinux 9 distributions do not come with a MongoDB repository. As a result, we must include the official MongoDB 6.0 repository with the most recent stable version. We must use a text editor to add the repository to the file /etc/yum.repos.d in order to accomplish this.
Establish a configuration file.
sudo vi /etc/yum.repos.d/mongodb-org-6.0.repo
Include the content below:
[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
Verify that the repository has added the repo file.
$ sudo dnf repolist
repo id repo name
appstream Rocky Linux 9 - AppStream
baseos Rocky Linux 9 - BaseOS
crb Rocky Linux 9 - CRB
epel Extra Packages for Enterprise Linux 9 - x86_64
extras Rocky Linux 9 - Extras
hashicorp Hashicorp Stable - x86_64
mongodb-org-6.0 MongoDB Repository
Examine whether the repo file is enabled by this command.
$ sudo dnf repolist mongodb-org-6.0
repo id repo name status
mongodb-org-6.0 MongoDB Repository enabled
3. Install MongoDB 6.0 on Rocky Linux 9 |AlmaLinux 9
The repository has now been established. Using the DNF package manager, we will now install the MongoDB package on the system.
Execute the following command.
sudo dnf install mongodb-org
Dependency tree:
MongoDB Repository 83 B/s | 1.3 kB 00:16
Dependencies resolved.
=======================================================================================================================================
Package Architecture Version Repository Size
=======================================================================================================================================
Installing:
mongodb-org x86_64 6.0.5-1.el9 mongodb-org-6.0 10 k
Installing dependencies:
cyrus-sasl x86_64 2.1.27-20.el9 baseos 71 k
mongodb-database-tools x86_64 100.7.0-1 mongodb-org-6.0 27 M
mongodb-mongosh x86_64 1.8.0-1.el8 mongodb-org-6.0 43 M
mongodb-org-database x86_64 6.0.5-1.el9 mongodb-org-6.0 10 k
mongodb-org-database-tools-extra x86_64 6.0.5-1.el9 mongodb-org-6.0 15 k
mongodb-org-mongos x86_64 6.0.5-1.el9 mongodb-org-6.0 21 M
mongodb-org-server x86_64 6.0.5-1.el9 mongodb-org-6.0 30 M
mongodb-org-tools x86_64 6.0.5-1.el9 mongodb-org-6.0 10 k
Transaction Summary
=======================================================================================================================================
Install 9 Packages
Total size: 121 M
Total download size: 73 M
Installed size: 494 M
Alternatively, you can install it by specifying the release, naming each component specifically, and adding the version to the package name with the following command:
sudo yum install -y mongodb-org-<VERSION> mongodb-org-database-<VERSION> mongodb-org-server-<VERSION> mongodb-mongosh-<VERSION> mongodb-org-mongos-<VERSION>mongodb-org-tools-<VERSION>
Start the MongoDB service when the MongoDB has been successfully installed on your system.
sudo systemctl start mongod
Note: If this error occurs: Starting mongo.service failed: Start the MongoDB service after reloading the daemon if the unit mongo.service cannot be found.
sudo systemctl daemon-reload
sudo systemctl start mongod
The MongoDB service must be enabled in order for it to start up.
sudo systemctl enable mongod
Now verify its status:
$ systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2023-04-07 18:41:58 EAT; 6min ago
Docs: https://docs.mongodb.org/manual
Main PID: 948 (mongod)
Memory: 242.0M
CPU: 3.879s
CGroup: /system.slice/mongod.service
└─948 /usr/bin/mongod -f /etc/mongod.conf
Apr 07 18:41:58 localhost.localdomain systemd[1]: Started MongoDB Database Server.
Check the version of MongoDB installed:
$ mongod --version
db version v6.0.5
Build Info: {
"version": "6.0.5",
"gitVersion": "c9a99c120371d4d4c52cbb15dac34a36ce8d3b1d",
"openSSLVersion": "OpenSSL 3.0.1 14 Dec 2021",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "rhel90",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
4. Configure MongoDB 6.0 on Rocky Linux 9|AlmaLinux 9
We still need to make a few configurations after MongoDB is fully installed in order to use the database.
i) Create a MongoDB admin user
Start by entering the following command to access the MongoDB shell.
$ mongosh
Current Mongosh Log ID: 64303baf8b065c6b1d241913
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.0
Using MongoDB: 6.0.5
Using Mongosh: 1.8.0
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
------
The server generated these startup warnings when booting
2023-04-07T18:42:45.409+03:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2023-04-07T18:42:45.410+03:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2023-04-07T18:42:45.410+03:00: vm.max_map_count is too low
------
------
test>
Make a role and a user. We’ll create a user for this demonstration and give them the admin role, as follows:
use admin
db.createUser(
{
user: "monguser1",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)
In order to exit the shell, you must first supply the user with a password:
admin> exit
bye
We’ll attempt to log in with the newly created user to see if everything is working properly:
$ mongosh -u monguser1
Enter password: ******
Current Mongosh Log ID: 64304980fe0055a73d77e29d
Connecting to: mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.0
Using MongoDB: 6.0.5
Using Mongosh: 1.8.0
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
test>
Issue the following command to determine which database is present:
> db
Output:
test
[test] is the default database.
ii) Create a new database
The test database cannot be used. Use the command followed by the name of an empty database on the command prompt to create a new database.
In my case, I will use tutornix-db.
> use tutornix-db
switched to db tutornix-db
tutornix-db>
Finally, tutornix-db has been created.
iii) Establish a collection
In documents, MongoDB stores data in JSON-like forms. For demonstration reasons, I’ll make a straightforward collection called workers and add some test data to it.
tutornix-db> db.createCollection('workers')
Output:
{ OK : 1 }
To display all the databases’ collections:
tutornix-db> show collections
Output:
workers
Run the command below to insert a document into a collection.
db.workers.insertOne({first_name:"John", last_name:"Jayde"});
Output:
{
acknowledged: true,
insertedId: ObjectId("64303cf7ac9443cfeb5e44a9")
}
To view the documents contained in a collection. You will notice that an object in the collection will automatically assign a unique value.
tutornix-db> db.workers.find();
Output:
[
{
_id: ObjectId("64303cf7ac9443cfeb5e44a9"),
first_name: 'John',
last_name: 'Jayde'
}
]
To add numerous documents to the collection:
tutornix-db> db.workers.insertMany([{first_name:"Kim", last_name:"Rock"},{first_name: "Jack", last_name:"Harris",gender:"Male",title:"Managing Director"}]);
Output:
{
acknowledged: true,
insertedIds: {
'0': ObjectId("64303d5bac9443cfeb5e44aa"),
'1': ObjectId("64303d5bac9443cfeb5e44ab")
}
}
Observe how insertOne and insertMany are used in the aforementioned instructions instead of Collection.insert(), which has been deprecated since MongoDB version 6.
Let’s now have a look at the collection’s documents.
tutornix-db> db.workers.find();
Output:
[
{
_id: ObjectId("64303cf7ac9443cfeb5e44a9"),
first_name: 'John',
last_name: 'Jayde'
},
{
_id: ObjectId("6431024696aad233c4d6647b"),
first_name: 'Kim',
last_name: 'Rock'
},
{
_id: ObjectId("6431024696aad233c4d6647c"),
first_name: 'Jack',
last_name: 'Harris',
gender: 'Male',
title: 'Managing Director'
}
]
You’ll see that the developer may quickly create a new field using MongoDB. In this instance, we have added the customer Jack Harris’ gender and title.
Relational databases cannot be used for this since each field needs to be predefined.
iv) Modify Collection Field
I’ll add Kim Rock’s gender and title:
tutornix-db> db.workers.updateOne({first_name:"Kim"},{$set:{first_name:"Kim", last_name:"Rock", gender:"Male", title: "Sales person"}}, {upsert: true});
Output:
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
In MongoDB version 6, updating documents necessitates the usage of atomic operators. This is why I’ve created a $set and upsert.
Examine the document for changes.
tutornix-db> db.workers.find();
Output:
[
{
_id: ObjectId("64303cf7ac9443cfeb5e44a9"),
first_name: 'John',
last_name: 'Jayde'
},
{
_id: ObjectId("6431024696aad233c4d6647b"),
first_name: 'Kim',
last_name: 'Rock'
gender: 'Male',
title: 'Sales person'
},
{
_id: ObjectId("6431024696aad233c4d6647c"),
first_name: 'Jack',
last_name: 'Harris',
gender: 'Male',
title: 'Managing Director'
}
]
Using the $set operator is a simpler method in this case. Let’s add gender to John Jayde as an illustration.
tutornix-db> db.workers.updateOne({first_name:"John"},{$set:{gender:"Male"}})
Output:
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
Check :
tutornix-db> db.workers.find();
Output:
[
{
_id: ObjectId("64303cf7ac9443cfeb5e44a9"),
first_name: 'John',
last_name: 'Jayde',
gender: 'Male'
}
]
Quit by issuing the following command:
quit();
Verdict
We have come to the end of our guide on how to Install MongoDB 6.0 on Rocky Linux 9|AlmaLinux 9. The MongoDB database is excellent. It is an extremely adaptable database because it is not required to utilize schemas. It is quite popular among developers because it employs the JSON format.
Read more about MongoDB.
Other Linux guides: