Tutorials  /  Linux Basics

Install MongoDB on Rocky Linux 9 – Secure Setup Guide

Ccentron Redaktion · April 2025 ·6 min read ·Linux Basics, Tutorial

MongoDB is a free, open-source NoSQL database that stores information in a flexible, JSON-like format. Its schema-less structure enables storage of unstructured data and documents with varying fields, while still supporting full indexing, ad-hoc queries, and replication through replica sets for high availability.

This tutorial outlines the steps to install MongoDB on a Rocky Linux 9 system. You’ll also learn how to secure the database and test the setup by creating sample entries.

System Requirements

Ensure the following prerequisites are met:

  • A working Rocky Linux 9 server instance.
  • SSH access as a non-root user with sudo rights.
  • System packages are updated.
VM

Matching infrastructure at centron

No hardware needed to follow along: ccloud³ VMs with full root access start at €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →

MongoDB Installation on Rocky Linux 9

Since MongoDB is not part of the default Rocky Linux 9 repositories, you'll need to manually add its repository and then install the database using DNF.

Start by checking the official MongoDB releases page to confirm the most recent version. For this example, version 8.0 is used.

Create the MongoDB repository configuration file:

Console
$ sudo nano /etc/yum.repos.d/mongodb-org-8.0.repo

Paste the configuration below into the file:

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

Save your changes and refresh the package index:

Console
$ sudo dnf update

Now install MongoDB:

Console
$ sudo dnf -y install mongodb-org

Switch the default MongoDB shell package to a version that supports OpenSSL 3:

Console
$ sudo dnf -y swap mongodb-mongosh mongodb-mongosh-shared-openssl3

Since Rocky Linux 9 uses OpenSSL 3 by default, this swap ensures compatibility between MongoDB and your server’s cryptographic libraries.

Managing MongoDB as a Systemd Service

MongoDB operates through the mongod system service. To configure it to start automatically and verify its operational status, run the following:

Enable auto-start at boot:

Console
$ sudo systemctl enable mongod

Start the MongoDB service:

Console
$ sudo systemctl start mongod

Check that MongoDB is running:

Console
$ sudo systemctl status mongod

Expected output:

YAML
● mongod.service - MongoDB Database Server
  Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
  Active: active (running)
    Docs: https://docs.mongodb.org/manual
 Main PID: 6919 (mongod)
  Memory: 149.8M
  CGroup: /system.slice/mongod.service
          └─6919 /usr/bin/mongod --config /etc/mongod.conf

Securing MongoDB with User Authentication

By default, MongoDB does not enforce authentication. To secure the database, enable password-based authentication with the steps below.

Launch the MongoDB shell:

Console
$ mongosh

Switch to the admin database:

Code
> use admin

Create a user with administrative rights:

Code
> db.createUser( 
 {
   user: "mongodbadmin",
   pwd: passwordPrompt(),
   roles: [ 
    { role: "userAdminAnyDatabase", db: "admin" },
    { role: "readWriteAnyDatabase", db: "admin" },
    { role: "dbAdminAnyDatabase", db: "admin" }
   ]    
 }
)

You should see the following confirmation:

Code
Enter password
***{ ok: 1 }

List all configured users to confirm creation:

Code
> db.getUsers()

Expected result:

Code
{
  users: [
    {
      _id: 'admin.mongodbadmin',
      userId: UUID('93f37099-85f9-4b6e-a072-09c2b1045462'),
      user: 'mongodbadmin',
      db: 'admin',
      roles: [
        { role: 'dbAdminAnyDatabase', db: 'admin' },
        { role: 'userAdminAnyDatabase', db: 'admin' },
        { role: 'readWriteAnyDatabase', db: 'admin' }
      ],
      mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
    }
  ],
  ok: 1
}

Type the following to leave the shell:

Code
> exit

Edit the MongoDB configuration file to enforce user authentication:

Console
$ sudo nano /etc/mongod.conf

Add this to the bottom of the file:

YAML
security:
    authorization: enabled

Finally, restart the MongoDB service for the settings to take effect:

Console
$ sudo systemctl restart mongod

Accessing MongoDB and Managing Databases

Follow the steps below to log into the MongoDB shell and perform operations like creating users, defining databases, and inserting data.

Log into the MongoDB shell using the previously configured administrative account:

Console
$ mongosh -u mongodbadmin -p --authenticationDatabase admin

When prompted, provide the password associated with the mongodbadmin user.

Next, create a database named centron_test:

Code
> use centron_test

Define a new user (e.g., testuser) with complete access to this new database:

Code
> db.createUser(
  {
   user: "testuser",
   pwd: passwordPrompt(),
   roles: [ { role: "readWrite", db: "centron_test" } ]
  }
)

Input a strong password for this user when prompted. The confirmation output should resemble:

Code
Enter password:
********{ ok: 1 }

Exit the current MongoDB session:

Code
> exit

Using the New MongoDB User and Inserting Data

Now connect to the MongoDB shell as testuser. You will be asked to enter the user’s password:

Console
$ mongosh -u testuser -p --authenticationDatabase centron_test

Switch to the target database:

Code
> use centron_test

Insert a sample document with the message Greetings into a new collection called messages:

Mongodb
> db.messages.insertOne({message: "Greetings" })

The output should confirm the successful insertion:

Code
{
  acknowledged: true,
  insertedId: ObjectId('67750a2667bb6ae4a4fad9e0')
}

Retrieve all documents from the messages collection to verify the data:

Mongodb
> db.messages.find()

Expected output:

Code
[
  {
    _id: ObjectId('67750a2667bb6ae4a4fad9e0'),
    message: 'Greetings'
  }
]

Exit the MongoDB shell when done:

Code
> exit

Final Thoughts

You’ve successfully installed MongoDB on Rocky Linux 9 and verified the installation by creating a database, user, and inserting a test document. MongoDB’s flexible design enables you to manage collections, user roles, and large volumes of unstructured data. For additional configurations and features, consult the official MongoDB documentation.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Linux Basics
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren