How to Install MongoDB on CentOS/RHEL 7

How to Install MongoDB on CentOS/RHEL 7

How to Install MongoDB on CentOS/RHEL 7

To install MongoDB on CentOS/RHEL 7, follow these steps. The installation process involves adding the MongoDB repository, installing the MongoDB packages, and starting the MongoDB service.

1. Add MongoDB Repository

MongoDB provides official repositories for CentOS and RHEL, but you need to add them manually because MongoDB isn't included in the default CentOS/RHEL package repositories.

  1. Open a terminal and create the MongoDB repository file in /etc/yum.repos.d/:

    sudo vi /etc/yum.repos.d/mongodb-org-4.4.repo

    (You can also use nano or any other text editor.)

  2. Add the following content to the file (for MongoDB version 4.4, as of writing):

    [mongodb-org-4.4] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/centos/7/mongodb-org/4.4/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
    • baseurl: This points to the MongoDB official repository for CentOS 7.
    • gpgcheck: Ensures that the packages are signed.
    • enabled: Set to 1 to enable this repository.
  3. Save and close the file (in vi, press Esc, type :wq, and hit Enter).

2. Install MongoDB

Now that the repository is added, you can install MongoDB.

  1. Update your system’s package index:

    sudo yum update -y
  2. Install MongoDB:

    sudo yum install -y mongodb-org

    This will install the latest stable version of MongoDB (at the time of writing, version 4.4).

3. Start MongoDB Service

After installation, start the MongoDB service.

  1. Start the MongoDB service:

    sudo systemctl start mongod
  2. Enable MongoDB to start automatically on system boot:

    sudo systemctl enable mongod

    This ensures MongoDB starts automatically when the system reboots.

  3. Verify MongoDB is running:

    sudo systemctl status mongod

    You should see output that indicates MongoDB is running, such as:

    Active: active (running)

4. Access MongoDB

To access the MongoDB shell and interact with the database, use:

mongo

This will start the MongoDB shell, allowing you to execute MongoDB commands.

5. Firewall Configuration (Optional)

If your system has a firewall enabled (which it likely does), you'll need to allow traffic on MongoDB’s default port (27017).

  1. Open the port for MongoDB (assuming you're using firewalld):

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

    This will open port 27017 to allow MongoDB connections.

  2. If you're running SELinux (Security-Enhanced Linux) and it's enabled, you may need to adjust its settings:

    sudo setsebool -P mongod_enable_home_dir 1

6. Verify Installation

You can verify that MongoDB is running properly by checking its version:

mongo --version

This should display the installed version of MongoDB.

7. (Optional) Enable Authentication (Security)

For production systems, it's important to enable authentication to secure your MongoDB instance. By default, MongoDB allows anyone to access the database. To enable authentication:

  1. Edit the MongoDB configuration file (/etc/mongod.conf):

    sudo vi /etc/mongod.conf
  2. Find the security section and uncomment or add the following:

    security: authorization: "enabled"
  3. Save the file and restart MongoDB:

    sudo systemctl restart mongod
  4. After authentication is enabled, you'll need to create an admin user to authenticate.

8. Troubleshooting

  • MongoDB Service Not Starting: If the MongoDB service fails to start, check the log files for errors:

    sudo journalctl -u mongod
  • Permissions Errors: Ensure that the MongoDB data directory (/var/lib/mongo by default) is accessible and owned by the mongod user:

    sudo chown -R mongod:mongod /var/lib/mongo

9. Uninstall MongoDB (If Necessary)

If you want to uninstall MongoDB later, you can use the following commands:

  1. Stop the MongoDB service:

    sudo systemctl stop mongod
  2. Uninstall MongoDB:

    sudo yum remove -y mongodb-org
  3. Optionally, remove MongoDB’s data directory:

    sudo rm -rf /var/lib/mongo

Summary

  • Added the MongoDB repository for CentOS/RHEL 7.
  • Installed MongoDB using yum.
  • Started the MongoDB service and enabled it to start on boot.
  • Configured the firewall and opened port 27017.
  • Accessed MongoDB using the mongo shell.
  • Enabled authentication (optional) for added security.

MongoDB should now be installed and running on your CentOS/RHEL 7 system! Let me know if you need help with other configurations or troubleshooting. 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close