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.
Open a terminal and create the MongoDB repository file in
/etc/yum.repos.d/
:(You can also use
nano
or any other text editor.)Add the following content to the file (for MongoDB version 4.4, as of writing):
baseurl
: This points to the MongoDB official repository for CentOS 7.gpgcheck
: Ensures that the packages are signed.enabled
: Set to1
to enable this repository.
Save and close the file (in
vi
, pressEsc
, type:wq
, and hit Enter).
2. Install MongoDB
Now that the repository is added, you can install MongoDB.
Update your system’s package index:
Install MongoDB:
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.
Start the MongoDB service:
Enable MongoDB to start automatically on system boot:
This ensures MongoDB starts automatically when the system reboots.
Verify MongoDB is running:
You should see output that indicates MongoDB is running, such as:
4. Access MongoDB
To access the MongoDB shell and interact with the database, use:
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).
Open the port for MongoDB (assuming you're using
firewalld
):This will open port
27017
to allow MongoDB connections.If you're running SELinux (Security-Enhanced Linux) and it's enabled, you may need to adjust its settings:
6. Verify Installation
You can verify that MongoDB is running properly by checking its 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:
Edit the MongoDB configuration file (
/etc/mongod.conf
):Find the
security
section and uncomment or add the following:Save the file and restart MongoDB:
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:
Permissions Errors: Ensure that the MongoDB data directory (
/var/lib/mongo
by default) is accessible and owned by themongod
user:
9. Uninstall MongoDB (If Necessary)
If you want to uninstall MongoDB later, you can use the following commands:
Stop the MongoDB service:
Uninstall MongoDB:
Optionally, remove MongoDB’s data directory:
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. 🚀