How to Install MongoDB on AlmaLinux

Posted on by Mohammed Noufal
Reading Time: 5 minutes

MongoDB is a document-oriented NoSQL database with high speed and scalability to handle heavy traffic and massive amounts of data. In MongoDB, data is structured in a JSON-like format inside records referred to as documents. Compared to a SQL database, the data is stored in rows and columns inside tables. 

In this article, you will learn how to install MongoDB AlmaLinux.

Requirements

  • AlmaLinux OS 9.
  • MongoDB 6.0.
  • Access to the Linux system as root using the sudo command or as a user with admin privileges. 

Install MongoDB on AlmaLinux

In this part of the article, you will learn how to install MongoDB on AlmaLinux. For this setup, use the latest version of MongoDB, version 6.0.5, and an AlmaLinux 9.1 server.

Step 1: Update Your AlmaLinux System

Update your AlmaLinux System by executing the below command.

[root@nouf-mongodb-almalinux9 ~]# sudo dnf -y update

Step 2: MongoDB Installation

You cannot directly install MongoDB on AlmaLinux 9 using the system's default repository. So, you must add a custom repository to get the latest version of MongoDB. Execute the below command to create a MongoDB repository.

[root@nouf-mongodb-almalinux9 ~]# sudo vi /etc/yum.repos.d/mongodb.repo

Add the below entries to this repository file.

[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

Rebuild the system repository cache using the DNF command below.

[root@nouf-mongodb-almalinux9 ~]# sudo dnf update

To check if the repository is configured correctly, execute the below command.

[root@nouf-mongodb-almalinux9 ~]# sudo dnf repolist

Here is the output.

[root@nouf-mongodb-almalinux9 ~]# sudo dnf repolist
repo id                  repo name
appstream          AlmaLinux 9 - AppStream
baseos             AlmaLinux 9 - BaseOS
extras             AlmaLinux 9 - Extras
mongodb-org-6.0    MongoDB Repository

Execute the below command to check whether the MongoDB repository is enabled.

[root@nouf-mongodb-almalinux9 ~]# sudo dnf repolist mongodb-org-6.0

Here is the output.

[root@nouf-mongodb-almalinux9 ~]# sudo dnf repolist mongodb-org-6.0
repo id                          repo name                  status 
mongodb-org-6.0                  MongoDB Repository         enabled
[root@nouf-mongodb-almalinux9 ~]#

Install MongoDB AlmaLinux by using the below command.

[root@nouf-mongodb-almalinux9 ~]# sudo dnf -y install mongodb-org

Once you install MongoDB AlmaLinux, the service will automatically start running on your system. You can check the service status using the below command.

[root@nouf-mongodb-almalinux9 ~]# sudo systemctl status mongod

Here is the output.

[root@nouf-mongodb-almalinux9 ~]# sudo systemctl status mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
     Active: active (running) since Wed 2023-03-29 08:25:03 UTC; 2s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 18711 (mongod)
     Memory: 68.0M
        CPU: 626ms
     CGroup: /system.slice/mongod.service
             └─18711 /usr/bin/mongod -f /etc/mongod.conf

Mar 29 08:25:03 nouf-mongodb-almalinux9 systemd[1]: Started MongoDB Database Server.

Step 3: Start and Enable MongoDB

If the MongoDB service does not start automatically, execute the below command to start it.

[root@nouf-mongodb-almalinux9 ~]# sudo systemctl start mongod

Execute the below command to enable the MongoDB service automatically when the system boots.

[root@nouf-mongodb-almalinux9 ~]# sudo systemctl enable mongod

Step 4: MongoDB Verification

You can verify that MongoDB is operational by executing the below commands.

[root@nouf-mongodb-almalinux9 ~]# mongosh
[root@nouf-mongodb-almalinux9 ~]# mongosh --eval 'db.runCommand({ connectionStatus: 1 })'

Here are the outputs.

[root@nouf-mongodb-almalinux9 ~]# mongosh
Current Mongosh Log ID:	6423f6cc8363d74a18fb80fe
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
test>
~]# mongosh --eval 'db.runCommand({ connectionStatus: 1 })'
Current Mongosh Log ID:	6427b4b840aa2ab491a2e74e
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
{
  authInfo: { authenticatedUsers: [], authenticatedUserRoles: [] },
  ok: 1
}

Once you have completed all necessary mongosh commands, you must exit the prompt or the remaining commands will not work. Type quit followed by Enter to return to the Linux prompt.

Step 5: Basic MongoDB Commands

Here are 19 helpful basic MongoDB commands.

1. MongoDB Help

You can execute the below command to get a list of MongoDB commands.

db.help

2. Create a New Database

You can execute the below command to create a new database.

use dbname

Here is the sample output.

test> use admin
switched to db admin
admin>

3. Find the Currently Selected Database

You can execute the below command to find the currently selected database.

db

Here is the sample output.

admin> db
admin

4. View All Databases

You can execute the below command to view all databases.

show dbs

Here is the sample output.

admin> show dbs
admin   132.00 KiB
testmongodb 116.00 KiB
config  108.00 KiB
local    40.00 KiB
admin>

5. Drop a Database

You can execute the below command to drop a database.

db.dropDatabase()

Here is the sample output.

test> use testmongodb
switched to db testmongodb
testmongodb> db.dropDatabase()
{ ok: 1, dropped: 'testmongodb' }
testmongodb>

6. Create an Administrator User and Add a Role

You can execute the below command to create an administrator user and add a role.

db.createUser

Here is the sample output.

admin> db.createUser(
{
 user: "nouf_mongoadmin",
 pwd: "mongodb@123",
 roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
                    )
{ ok: 1 }
admin>

7. List All the Users Created Under the Current Database

You can execute the below command to list all the users created under the current database.

show users

Here is the sample output.

admin> show users
[
  {
    _id: 'admin.nouf_mongoadmin',
    userId: new UUID("17237c6c-de2a-4d59-b4c5-b69ac65172b4"),
    user: 'nouf_mongoadmin',
    db: 'admin',
    roles: [ { role: 'userAdminAnyDatabase', db: 'admin' } ],
    mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
  }
]
admin>

8. Create a Collection

You can execute the below command to create a collection.

 db.createCollection("name")

Here is the sample output.

admin> db.createCollection("sites");
{ ok: 1 }
admin>

9. Check the Collections List

You can execute the below command to check the collections list.

show collections

Here is the sample output.

admin> show collections
Domains
sites
SubDomains

10. Drop a Collection

You can execute the below command to drop a collection.

db.collection_name.drop()

Here is the sample output.

admin> db.sites.drop()
true
admin> show collections
Domains
SubDomains
admin>

11. Insert a Document Into the Collection

You can execute the below command to insert a document into the collection.

db.collection_name.insertOne(document)

Here is the sample output.

admin> db.Domains.insertOne({name: 'mohammednoufal.com', php: '8.0', SSL: 'active'})
{
  acknowledged: true,
  insertedId: ObjectId("6427b90593dbc7a26664b4d1")
}
admin>

12. Insert Multiple Documents Into the Selected Collection

You can execute the below command to insert multiple documents into the selected collection.

db.collection.insertMany(document1,document2)

Here is the sample output.

admin> db.Domains.insertMany([{name: 'mohammednoufal.com', php: '8.0', SSL: 'active'},{name: 'mohammed_noufal.com', php: '7.2', SSL: 'active'}])
{
  acknowledged: true,
  insertedIds: {
    '0': ObjectId("6423fd0791b03766a0353963"),
    '1': ObjectId("6423fd0791b03766a0353964")
  }
}
admin>

13. Get the Collection Document

You can execute the below command to get the collection document.

db.collection_name.find()

Here is the sample output.

admin> db.Domains.find()
[
  {
    _id: ObjectId("6423fd0791b03766a0353963"),
    name: 'mohammednoufal.com',
    php: '8.1',
    SSL: 'active'
  },
  {
    _id: ObjectId("6423fd0791b03766a0353964"),
    name: 'mohammed_noufal.com',
    php: '7.2',
    SSL: 'active'
  }
]
admin>

14. Update a Document in the Collection

You can execute the below command to update a document in the collection.

db.collection_name.updateOne(selection_criteria,updated_data)

Here is the sample output.

test> db.Domains.updateOne({"name":"mohammed_noufal.com"},{$set:{"name": "mohammednoufalnew.com"}})
{
  acknowledged: true,
  insertedId: null,
  matchedCount: 1,
  modifiedCount: 1,
  upsertedCount: 0
}
test> db.Domains.find()
[
  {
    _id: ObjectId("6427aa1b0431682c7904052c"),
    name: 'mohammednoufal.com',
    php: '8.0',
    SSL: 'active'
  },
  {
    _id: ObjectId("6427aa1b0431682c7904052d"),
    name: 'mohammednoufalnew.com',
    php: '7.2',
    SSL: 'active'
  }
]

15. Update Multiple Documents in the Collection

You can execute the below command to update multiple documents in the collection.

db.collection.updateMany(selection_criteria1,updated_data1, selection_criteria2,updated_data2)

16. Insert a New Document Into the Collection

You can execute the below command to insert a new document into the collection.

db.collection_name.insertOne(new_data)

Here is the sample output.

admin> db.Domains.insertOne({name: 'mohammednoufal_new.com', php: '7.4', SSL: 'active'})
{
  acknowledged: true,
  insertedId: ObjectId("6427ac900431682c7904052e")
}
admin> db.Domains.find()
[
  {
    _id: ObjectId("6423fd0791b03766a0353963"),
    name: 'mohammednoufal.com',
    php: '8.1',
    SSL: 'active'
  },
  {
    _id: ObjectId("6423fd0791b03766a0353964"),
    name: 'noufal.com',
    php: '7.2',
    SSL: 'active'
  }
 {
    _id: ObjectId("6427ac900431682c7904052e"),
    name: 'mohammednoufal_new.com',
    php: '7.4',
    SSL: 'active'
  }
]
admin>

17. Insert Multiple Documents Into the Collection

You can execute the below command to insert multiple documents into the collection.

db.collection.insertMany(new_data1,new_data2)

18. Delete a Document

You can execute the below command to delete a document from the collection.

db.Domains.deleteOne(document)

Here is the sample output.

admin> db.Domains.deleteOne({'name': 'mohammednoufal_2.com'})
{ acknowledged: true, deletedCount: 0 }
admin> db.Domains.find()
[
  {
    _id: ObjectId("6427aa1b0431682c7904052c"),
    name: 'mohammednoufal.com',
    php: '8.0',
    SSL: 'active'
  },
  {
    _id: ObjectId("6427aa1b0431682c7904052d"),
    name: 'mohammed_noufal.com',
    php: '7.2',
    SSL: 'active'
  },
  {
    _id: ObjectId("6423fd0791b03766a0353963"),
    name: 'mohammednoufal_new.com',
    php: '7.4',
    SSL: 'active'
  }
]

19. Delete Multiple Documents From the Collection

You can execute the below command to delete multiple documents from the collection.

db.collection_name.deleteMany(document1,document2)

Stop the MongoDB Services

You can execute the below command to stop the MongoDB Service.

[root@nouf-mongodb-almalinux9 ~]# sudo systemctl stop mongod

Uninstall MongoDB

Remove MongoDB Packages

You can execute the below command to remove MongoDB packages.

[root@nouf-mongodb-almalinux9 ~]# dnf erase $(rpm -qa | grep mongodb-org)

Remove MongoDB Data Directories

You can execute the below commands to remove MongoDB data directories.

[root@nouf-mongodb-almalinux9 ~]# sudo rm -r /var/log/mongodb
[root@nouf-mongodb-almalinux9 ~]# sudo rm -r /var/lib/mongo

Final Thoughts

You can easily install MongoDB AlmaLinux with the help of this article. The MongoDB commands mentioned here are the best practice solutions for maintaining highly available, efficient, and scalable operations, which are essential in today's business.

Your next AlmaLinux server should be a Liquid Web server. There are VPS Hosting, Cloud Dedicated Servers, and Dedicated Server options available. Contact the sales team to get the right fit for your business.

Avatar for Mohammed Noufal

About the Author: Mohammed Noufal

Mohammed Noufal is a B.Tech graduate with a decade of experience in server administration and web hosting. He is a father to two daughters and finds fulfillment in their growth. In his free time, he enjoys blogging, sharing experiences, and listening to music. With a strong technical background, family commitment, and creative outlets, he represents a well-rounded life journey.

Latest Articles

How does Apache Web Server work? An FAQ

Read Article

Email security best practices for using SPF, DKIM, and DMARC

Read Article

Linux dos2unix command syntax — removing hidden Windows characters from files

Read Article

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article