How to Install MongoDB 4.0 Community Edition on CentOS/RHEL 7 and Fedora
Updated
MongoDB 4.0 Community Edition can be installed on RPM-based Linux systems (Red Hat Enterprise Linux 7, CentOS 7, Scientific Linux 7, and recent Fedora releases) by adding the official MongoDB 4.0 repository, then installing the mongodb-org package with yum. This guide covers the repository setup, the install command, starting and enabling the service, and how to confirm the installation worked. It applies to 64-bit RPM-family systems; if you are on a non-RPM distribution such as Debian or Ubuntu, the package manager and repository steps differ.
Prerequisites
Before you start, confirm the following:
- A 64-bit CentOS 7, RHEL 7, Scientific Linux 7, or a recent Fedora release.
- Root or
sudoaccess, since repository files and service management require elevated privileges. - A working network connection to reach the MongoDB repository.
- SELinux and firewall settings you can adjust if you plan to accept remote connections (the default install listens locally).
Step 1: Add the MongoDB 4.0 Repository
MongoDB is not in the default CentOS/RHEL repositories, so you add a repo file that points to the official 4.0 packages. Create the repository definition:
sudo vi /etc/yum.repos.d/mongodb-org-4.0.repo
Add the following content:
[mongodb-org-4.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.0.asc
Save and exit. The baseurl uses $releasever, which yum resolves to your system's major version, so the same file works across the supported RPM releases. gpgcheck=1 with the MongoDB GPG key means packages are verified before installation.
Step 2: Install the MongoDB Packages
Install the mongodb-org metapackage, which pulls in the server, shell, and related tools:
sudo yum install -y mongodb-org
Expected result: yum downloads the packages from the repository you added and installs them. When it finishes, the MongoDB server binaries, the mongo shell, and the default configuration file (/etc/mongod.conf) are in place.
If you want to prevent yum from automatically upgrading MongoDB to a newer minor version later, you can pin the version by editing /etc/yum.conf and adding an exclude line for the MongoDB packages. This is optional and only matters if you need strict version control.
Step 3: Start and Enable the MongoDB Service
Start the service and set it to start on boot:
sudo systemctl start mongod
sudo systemctl enable mongod
Expected result: systemctl start returns without error, and enable creates the symlink so MongoDB starts automatically after a reboot.
Check the service status:
sudo systemctl status mongod
You should see an active (running) state. If the service fails to start, the most common causes are a permissions problem on the data directory (/var/lib/mongo) or a configuration error in /etc/mongod.conf.
Step 4: Verify the Installation
Confirm the server is reachable and check its version by opening the MongoDB shell:
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
A successful connection returns a document showing ok: 1 and the authenticated/available users. To check the exact server version:
mongod --version
The output should report MongoDB 4.0.x, confirming the 4.0 Community Edition is installed.
Common Issues and Fixes
| Symptom | Likely cause | Fix |
|---|---|---|
yum install cannot find the package |
Repository file missing or malformed | Recheck /etc/yum.repos.d/mongodb-org-4.0.repo and the baseurl |
| Service fails to start | Data directory permissions or bad config | Check journalctl -u mongod and verify /var/lib/mongo ownership |
| Cannot connect remotely | Default bind address is localhost | Adjust bindIp in /etc/mongod.conf and open the firewall port |
| GPG check fails | Missing or outdated MongoDB GPG key | Confirm the gpgkey URL in the repo file is reachable |
What MimasTech Covers
MimasTech is a Linux technical blog and services site that publishes tutorials on Linux OS, virtualization, databases, and security. Its MongoDB 4.0 installation article is part of a series that also covers installing PHP 7.x versions on CentOS 7/RHEL 7, using the awk command for text processing, installing Docker on Debian 9, and installing Google Chrome on Ubuntu 18.04. The site also invites readers to submit their own experience for publication. If you are working through RPM-based server setup, its PHP and MongoDB guides are the most directly relevant.
Next Steps
After MongoDB is running, the usual follow-up tasks are creating an admin user, enabling authentication in /etc/mongod.conf, and configuring backups. The install itself is complete once mongod --version reports 4.0.x and the service is active.