
How to Install MongoDB on CentOS 8: A Step-by-Step Guide
MongoDB is a leading NoSQL database renowned for its flexibility, scalability, and performance, making it a top choice for modern application development. If you’re running a CentOS 8 server, getting MongoDB up and running is a straightforward process.
This guide provides a comprehensive, step-by-step walkthrough for installing MongoDB Community Edition on your CentOS 8 system, ensuring you have a stable and secure foundation for your projects.
Prerequisites
Before you begin the installation, make sure you have the following:
- A server running a clean installation of CentOS 8.
- A non-root user with
sudo
privileges.
Step 1: Configure the MongoDB Repository
CentOS 8’s default package repositories do not include MongoDB. Therefore, the first step is to create a repository file to tell your system’s package manager where to find the official MongoDB packages.
Create a new file named mongodb-org-6.0.repo
in the /etc/yum.repos.d/
directory using your preferred text editor, such as nano
or vi
.
sudo nano /etc/yum.repos.d/mongodb-org-6.0.repo
Paste the following content into the file. This configuration points to the official MongoDB 6.0 repository.
[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
Save the file and exit the editor. This step ensures you will install a trusted, official version of the database.
Step 2: Install MongoDB
With the repository configured, you can now install the MongoDB packages using the dnf
package manager. The mongodb-org
metapackage conveniently bundles all the necessary components, including the database server, shell, and other tools.
Execute the following command in your terminal:
sudo dnf install -y mongodb-org
This command will download and install the following key packages:
mongodb-org-server
: Themongod
daemon and its configuration scripts.mongodb-org-database
: Contains themongodb-org-server
package.mongodb-org-shell
: The new MongoDB Shell (mongosh
).mongodb-org-mongos
: Themongos
daemon for sharded clusters.mongodb-org-tools
: Contains essential command-line tools for import, export, and diagnostics.
Step 3: Start and Enable the MongoDB Service
After the installation is complete, the MongoDB service (named mongod
) will not be running. You need to start it manually and then enable it to launch automatically every time the server boots.
First, start the MongoDB service:
sudo systemctl start mongod
Next, enable the service to start on boot:
sudo systemctl enable mongod
Enabling the service is a critical step for production environments to ensure the database is available after a server reboot.
Step 4: Verify the Installation
To confirm that MongoDB is running correctly, you can check the status of the mongod
service.
sudo systemctl status mongod
If the installation was successful, you should see an active (running)
status in the output, indicating that the database server is operational.
You can also connect to the database using the MongoDB Shell to verify its functionality. The new shell, mongosh
, is now the standard.
mongosh
A successful connection will display the shell version and connect you to the default test
database, ready to accept commands.
Important: Securing Your MongoDB Installation
By default, a fresh MongoDB installation has some security weaknesses that must be addressed immediately, especially in a production environment.
1. Firewall Configuration
MongoDB listens on port 27017. If your application is running on a different server, you will need to allow traffic through the server’s firewall.
Use the following firewall-cmd
commands to permanently open the port:
sudo firewall-cmd --zone=public --add-port=27017/tcp --permanent
sudo firewall-cmd --reload
Warning: Only open this port if you require remote access. If your application and database are on the same server, it is more secure to leave the port closed to external traffic.
2. Enable Access Control
By default, MongoDB does not have authentication enabled. This means anyone who can connect to the database has full administrative permissions—a major security risk.
To secure your instance, you must enable role-based access control (RBAC). The process involves:
- Connecting to the shell and creating an administrative user.
- Editing the MongoDB configuration file to enable authentication.
- Restarting the
mongod
service.
To enable authentication, open the main configuration file:
sudo nano /etc/mongod.conf
Find the #security:
section, uncomment it, and add the authorization
setting:
security:
authorization: "enabled"
After creating an admin user and enabling this setting, you will need to authenticate with a username and password to perform any actions, dramatically improving your database’s security posture.
You have now successfully installed and performed the initial security hardening for your MongoDB instance on CentOS 8. Your database is ready to power your applications securely and efficiently.
Source: https://kifarunix.com/install-mongodb-community-edition-on-centos-8/