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.
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:
$ sudo nano /etc/yum.repos.d/mongodb-org-8.0.repoPaste the configuration below into the file:
[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.ascSave your changes and refresh the package index:
$ sudo dnf updateNow install MongoDB:
$ sudo dnf -y install mongodb-orgSwitch the default MongoDB shell package to a version that supports OpenSSL 3:
$ sudo dnf -y swap mongodb-mongosh mongodb-mongosh-shared-openssl3Since 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:
$ sudo systemctl enable mongodStart the MongoDB service:
$ sudo systemctl start mongodCheck that MongoDB is running:
$ sudo systemctl status mongodExpected output:
● 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.confSecuring 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:
$ mongoshSwitch to the admin database:
> use adminCreate a user with administrative rights:
> 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:
Enter password
***{ ok: 1 }List all configured users to confirm creation:
> db.getUsers()Expected result:
{
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:
> exitEdit the MongoDB configuration file to enforce user authentication:
$ sudo nano /etc/mongod.confAdd this to the bottom of the file:
security:
authorization: enabledFinally, restart the MongoDB service for the settings to take effect:
$ sudo systemctl restart mongodAccessing 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:
$ mongosh -u mongodbadmin -p --authenticationDatabase adminWhen prompted, provide the password associated with the mongodbadmin user.
Next, create a database named centron_test:
> use centron_testDefine a new user (e.g., testuser) with complete access to this new database:
> 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:
Enter password:
********{ ok: 1 }Exit the current MongoDB session:
> exitUsing 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:
$ mongosh -u testuser -p --authenticationDatabase centron_testSwitch to the target database:
> use centron_testInsert a sample document with the message Greetings into a new collection called messages:
> db.messages.insertOne({message: "Greetings" })The output should confirm the successful insertion:
{
acknowledged: true,
insertedId: ObjectId('67750a2667bb6ae4a4fad9e0')
}Retrieve all documents from the messages collection to verify the data:
> db.messages.find()Expected output:
[
{
_id: ObjectId('67750a2667bb6ae4a4fad9e0'),
message: 'Greetings'
}
]Exit the MongoDB shell when done:
> exitFinal 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.
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.