How to Install MongoDB 8 on Rocky Linux 9

MongoDB is a popular NoSQL database that stores data as JSON-like documents.
It allows fast, scalable, and flexible data management, supports replication and sharding,
and uses the MongoDB Query Language (MQL) for access.

Add MongoDB Repository

Before installing MongoDB, manually add its repository because it is not available in the default Rocky Linux 9 repository.

Create a repository file:

vi /etc/yum.repos.d/mongodb.repo

Add the following lines and save 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.asc

Install MongoDB

dnf clean all
dnf makecache
dnf install -y mongodb-org

Start and Enable MongoDB Service

systemctl start mongod
systemctl enable mongod

Verify MongoDB is running:

systemctl status mongod

Output:

● mongod.service - MongoDB Database Server
   Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor pres>
   Active: active (running) since Sat 2025-09-13 17:56:49 UTC; 15s ago
     Docs: https://docs.mongodb.org/manual
 Main PID: 9760 (mongod)
   Memory: 98.0M
   CGroup: /system.slice/mongod.service
           └─9760 /usr/bin/mongod -f /etc/mongod.conf

Access MongoDB Shell

mongosh

First-time output:

Current Mongosh Log ID: 68c5b0b8ad945d8beece5f46
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.5.8
Using MongoDB:          8.0.13
Using Mongosh:          2.5.8

For mongosh info see: https://www.mongodb.com/docs/mongodb-shell/

To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
   The server generated these startup warnings when booting
   2025-09-13T17:56:49.728+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2025-09-13T17:56:51.626+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2025-09-13T17:56:51.627+00:00: For customers running the current memory allocator, we suggest changing the contents of the following sysfsFile
   2025-09-13T17:56:51.627+00:00: We suggest setting the contents of sysfsFile to 0.
   2025-09-13T17:56:51.627+00:00: vm.max_map_count is too low
   2025-09-13T17:56:51.627+00:00: We suggest setting swappiness to 0 or 1, as swapping can cause performance problems.
------

test>

Create MongoDB Admin User

Switch to the admin database:

> use admin

Create an admin user (replace placeholders with your values):

admin> db.createUser(
...  {
...    user: "Enter-A-user-Here",
...    pwd: "Enter-A-Password-Here",
...    roles: [ { role: "root", db: "admin" } ]
...  }
...  )

List MongoDB users:

> show users

Output:

admin> test> use admin
... switched to db admin
... admin> db.createUser(
... ...  {
... ...    user: "Enter-A-user-Here",
... ...    pwd: "Enter-A-Password-Here",
... ...    roles: [ { role: "root", db: "admin" } ]
... ...  }
... ...  )
... { ok: 1 }

Enable Authentication in MongoDB

Edit the configuration file:

vi /etc/mongod.conf

Find the #security: section and update as follows:

security:
  authorization: enabled

Restart MongoDB:

systemctl restart mongod

Test without authentication:

> show users

You will get:

MongoServerError: command usersInfo requires authentication

Authenticate using the admin user:

> db.auth('Enter-A-user-Here', 'Enter-A-Password-Here')

Once authenticated, you can run any command securely.

Exit MongoDB shell:

> exit

(Optional) Allow MongoDB in Firewall

If you are accessing MongoDB remotely, open port 27017:

firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --reload

This concludes the installation and configuration of MongoDB 8 on Rocky Linux 9.


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS