How to Install MongoDB on AlmaLinux 9

By Raman Kumar

Updated on Aug 19, 2024

In this tutorial, we'll explain how to install MongoDB on AlmaLinux 9 and Managing Your First Database.

MongoDB is an open-source, document-oriented NoSQL database that provides high performance, high availability, and easy scalability. Unlike traditional relational databases, MongoDB uses a flexible schema that allows you to store data in JSON-like documents, making it ideal for handling unstructured data or rapidly changing data structures. 

MongoDB's document model is intuitive and powerful, enabling developers to store related data together, query it efficiently, and scale seamlessly as their application grows. As a leading database choice for modern applications, MongoDB is widely used in various industries, including finance, healthcare, and e-commerce, for tasks ranging from simple data storage to complex analytics.

This guide walks you through the installation of MongoDB Community Edition on an Ubuntu system and introduces the basic operations you need to get started with managing your data. Whether you're building a small-scale application or a large, distributed system, MongoDB offers the tools and flexibility required to handle the demands of modern data-driven environments. 

Install MongoDB on AlmaLinux

Prerequisites:

  • A AlmaLinux 9 installed dedicated server or KVM VPS.
  • A root user access or normal user administrative privileges.
  • Basic Linux command knowledge.

Step 1: Update System

Keep the system updated

dnf update -y

Step 2: Install MongoDB on AlmaLinux

First, add repository. For latest version visit official documentation:

sudo vi /etc/yum.repos.d/mongodb-org-7.0.repo

Add the following lines to the file:

[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/9/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-7.0.asc

Save and exit the file.

Install MongoDB using YUM:

sudo yum install -y mongodb-org-7.0.12 mongodb-org-database-7.0.12 mongodb-org-server-7.0.12 mongodb-org-mongos-7.0.12 mongodb-org-tools-7.0.12 mongodb-mongosh-shared-openssl3

Note: Here we are installing individual because we have faced OpenSSL error/issue with mongodb-mongosh package. So we are using mongodb-mongosh-shared-openssl3. It resolved our error. 

Start the MongoDB service:

sudo systemctl start mongod

Enable MongoDB to start on boot:

sudo systemctl enable mongod

Verify the installation:

mongod --version

You should see output confirming the MongoDB version installed.

db version v7.0.12
Build Info: {
    "version": "7.0.12",
    "gitVersion": "b6513ce0781db6818e24619e8a461eae90bc94fc",
    "openSSLVersion": "OpenSSL 3.0.7 1 Nov 2022",
    "modules": [],
    "allocator": "tcmalloc",
    "environment": {
        "distmod": "rhel90",
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

Step 3: Access MongoDB Shell

MongoDB comes with an interactive shell that allows you to manage the database. To access it, type:

mongosh

This will open the MongoDB shell where you can execute commands.

Step 4: Create a Database

In MongoDB, databases are created dynamically when you insert data. However, you can create a database explicitly by switching to a new database:

use mydatabase

Replace mydatabase with your desired database name. MongoDB will switch to this database, and if it doesn’t exist, it will be created.

Step 5: Create a Collection (Table Equivalent)

Collections in MongoDB are equivalent to tables in relational databases. You can create a collection by inserting a document into it:

db.createCollection("mycollection")

Replace mycollection with your desired collection name. However, MongoDB will automatically create a collection when you insert a document if it doesn’t exist.

Step 6: Insert Data into the Collection

To insert a document into the collection, use the insertOne method:

db.mycollection.insertOne({ name: "John Doe", age: 30, city: "New York" })

This command inserts a single document into the mycollection collection with fields name, age, and city.

Step 7: Query the Collection

To retrieve data from the collection, you can use the find method:

db.mycollection.find()

This command returns all documents in the mycollection collection. You can also use queries to filter the data:

db.mycollection.find({ age: 30 })

This command returns all documents where the age field is 30.

Step 8: Update a Document

You can update an existing document using the updateOne method:

db.mycollection.updateOne({ name: "John Doe" }, { $set: { age: 31 } })

This command updates the age field to 31 for the document where the name is "John Doe".

Step 9: Delete a Document

To delete a document, use the deleteOne method:

db.mycollection.deleteOne({ name: "John Doe" })

This command deletes the document where the name is "John Doe".

Conclusion

We've seen how to install MongoDB on AlmaLinux 9 and managing your first database, created a database, added a collection, inserted data, and performed basic CRUD operations. MongoDB is a powerful NoSQL database, and this tutorial should give you a solid foundation to start working with it.