dotlinux blog

How to Install MongoDB on Rocky Linux and AlmaLinux

MongoDB is a popular open-source NoSQL database known for its flexibility, scalability, and ease of use. It stores data in JSON-like documents, making it ideal for handling unstructured or semi-structured data. If you’re running Rocky Linux or AlmaLinux (both RHEL-based distributions), installing MongoDB requires a few extra steps since it’s not included in the default system repositories.

This guide will walk you through the entire process—from setting up MongoDB’s official repository to configuring security and testing the installation. Whether you’re a developer or system administrator, follow these steps to get MongoDB up and running smoothly on your Rocky Linux or AlmaLinux server.

2026-04

Table of Contents#

  1. Prerequisites
  2. Step 1: Add MongoDB Official Repository
  3. Step 2: Import MongoDB GPG Key
  4. Step 3: Install MongoDB
  5. Step 4: Start and Enable MongoDB Service
  6. Step 5: Verify MongoDB Installation
  7. Step 6: Configure MongoDB (Optional)
  8. Step 7: Test MongoDB Functionality
  9. Uninstall MongoDB (If Needed)
  10. Troubleshooting Common Issues
  11. Conclusion
  12. References

Prerequisites#

Before starting, ensure your system meets the following requirements:

  • A server running Rocky Linux 8/9 or AlmaLinux 8/9 (64-bit).
  • Sudo or root privileges to execute system commands.
  • Active internet connection to download packages.
  • Basic familiarity with Linux command-line operations.

First, update your system packages to ensure compatibility:

sudo dnf update -y  

Step 1: Add MongoDB Official Repository#

MongoDB isn’t available in the default Rocky/AlmaLinux repositories. We’ll add MongoDB’s official YUM repository to access the latest packages.

  1. Create a new repository file using a text editor (e.g., nano or vi):

    sudo nano /etc/yum.repos.d/mongodb-org-7.0.repo  
  2. Paste the following content into the file. This configures the repository for MongoDB 7.0 (the latest stable version as of 2024). Adjust the version (e.g., 7.0) if you need an older release:

    [mongodb-org-7.0]  
    name=MongoDB Repository  
    baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/7.0/x86_64/  
    gpgcheck=1  
    enabled=1  
    gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc  
  3. Save and exit the editor (for nano, press Ctrl+O, then Enter, followed by Ctrl+X).

Step 2: Import MongoDB GPG Key#

To ensure package integrity, import MongoDB’s GPG key:

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg --dearmor -o /etc/pki/rpm-gpg/RPM-GPG-KEY-mongodb  

Verify the key was imported successfully:

sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-mongodb  

Step 3: Install MongoDB#

With the repository configured, install the MongoDB packages. The mongodb-org metapackage installs the latest stable version, including the server, shell, tools, and monitoring utilities:

sudo dnf install -y mongodb-org  

If prompted to accept the GPG key, type y and press Enter.

Step 4: Start and Enable MongoDB Service#

MongoDB runs as a systemd service. Start the service and enable it to launch on boot:

  1. Start the MongoDB service:

    sudo systemctl start mongod  
  2. Enable the service to auto-start on reboot:

    sudo systemctl enable mongod  
  3. Verify the service status:

    sudo systemctl status mongod  

    You should see output indicating the service is active (running).

Step 5: Verify MongoDB Installation#

Check the installed MongoDB version to confirm success:

mongod --version  

You’ll see output like:

db version v7.0.5  
Build Info: { ... }  

Additionally, connect to the MongoDB shell to ensure the server is responsive:

mongosh  

The shell will launch, and you’ll see a prompt like test>, indicating you’re connected to the default test database. Type exit to quit the shell.

Step 6: Configure MongoDB (Optional)#

By default, MongoDB is configured for local use. For production environments, you may need to adjust settings like network access and authentication.

Update Bind IP for Remote Access#

By default, MongoDB binds to 127.0.0.1 (local loopback), restricting access to the local machine. To allow remote connections (e.g., from another server), update the bindIp setting:

  1. Open the MongoDB configuration file:

    sudo nano /etc/mongod.conf  
  2. Locate the net section and modify bindIp:

    net:  
      port: 27017  
      bindIp: 0.0.0.0  # Allows connections from any IP (not recommended for production)  
      # OR specify specific IPs: bindIp: 127.0.0.1,192.168.1.100  

    Note: Restricting bindIp to specific trusted IPs is more secure than using 0.0.0.0.

  3. Save the file and restart MongoDB for changes to take effect:

    sudo systemctl restart mongod  
  4. If using a firewall (e.g., firewalld), allow MongoDB’s default port (27017):

    sudo firewall-cmd --add-port=27017/tcp --permanent  
    sudo firewall-cmd --reload  

Enable Authentication#

MongoDB does not enforce authentication by default. To secure your database, enable username/password authentication:

  1. Connect to the MongoDB shell:

    mongosh  
  2. Switch to the admin database:

    use admin  
  3. Create an administrative user (replace your_username and your_strong_password with secure values):

    db.createUser({  
      user: "your_username",  
      pwd: "your_strong_password",  
      roles: [{ role: "root", db: "admin" }]  
    })  

    The root role grants full administrative access. For granular permissions, use roles like readWrite or dbAdmin.

  4. Exit the shell:

    exit  
  5. Edit the MongoDB config file to enable authentication:

    sudo nano /etc/mongod.conf  
  6. Add the security section (or uncomment it if present):

    security:  
      authorization: enabled  
  7. Restart MongoDB:

    sudo systemctl restart mongod  
  8. Test authentication by connecting with the admin user:

    mongosh -u "your_username" -p "your_strong_password" --authenticationDatabase "admin"  

    If successful, you’ll access the shell.

Step 7: Test MongoDB Functionality#

Let’s create a test database and insert a document to verify MongoDB works:

  1. Connect to the shell (with authentication if enabled):

    mongosh -u "your_username" -p "your_strong_password" --authenticationDatabase "admin"  
  2. Create a new database (e.g., mydb):

    use mydb  
  3. Insert a sample document into a collection (e.g., users):

    db.users.insertOne({  
      name: "John Doe",  
      email: "[email protected]",  
      age: 30  
    })  
  4. Query the document to confirm it was inserted:

    db.users.find()  

    You’ll see output like:

    { "_id" : ObjectId("650a1b3d1234567890abcdef"), "name" : "John Doe", "email" : "[email protected]", "age" : 30 }  

Uninstall MongoDB (If Needed)#

To remove MongoDB completely:

  1. Stop the service:

    sudo systemctl stop mongod  
  2. Remove MongoDB packages:

    sudo dnf remove -y mongodb-org mongodb-org-server mongodb-org-shell mongodb-org-mongos mongodb-org-tools  
  3. Delete data and log directories (this erases all databases!):

    sudo rm -rf /var/lib/mongo  
    sudo rm -rf /var/log/mongodb  
  4. Remove the repository file:

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

Troubleshooting Common Issues#

  • MongoDB service fails to start: Check the log file at /var/log/mongodb/mongod.log for errors (e.g., port conflicts or permission issues).
  • Remote connection refused: Ensure bindIp is configured correctly and the firewall allows port 27017.
  • Authentication errors: Verify the username, password, and authentication database (e.g., admin).
  • SELinux blocks MongoDB: Temporarily disable SELinux to test (sudo setenforce 0), then configure SELinux policies if needed.

Conclusion#

You’ve successfully installed MongoDB on Rocky Linux or AlmaLinux! This guide covered repository setup, installation, service management, configuration, and basic testing. For production use, always enable authentication, restrict network access, and back up your databases regularly.

References#