How to Install MongoDB 4.2 on Debian 10/9/8
To install MongoDB 4.2 on Debian 10/9/8, follow these steps. This guide involves adding the MongoDB 4.2 repository, installing the MongoDB packages, and starting the MongoDB service.
1. Add MongoDB 4.2 Repository
MongoDB provides official repositories for Debian-based distributions. Since MongoDB 4.2 is an older version, you need to explicitly use the MongoDB 4.2 repository.
Import the MongoDB public key to your system so the package manager can verify the authenticity of the packages:
Create a MongoDB 4.2 repository file under
/etc/apt/sources.list.d/
:For Debian 9 (Stretch), replace
buster
withstretch
in the URL:For Debian 8 (Jessie), use
jessie
instead ofbuster
orstretch
:
2. Update System Packages
Update the package index on your system:
3. Install MongoDB 4.2
Now that the repository has been added, you can install MongoDB 4.2.
Install MongoDB using
apt
:This will install the
mongodb-org
package, which includes MongoDB,mongod
, and other required binaries.
4. Start MongoDB Service
Start the MongoDB service:
Enable MongoDB to start automatically at boot:
Verify that MongoDB is running:
You should see output indicating that MongoDB is active and running, like:
5. Verify MongoDB Installation
Access the MongoDB shell:
This will connect you to the MongoDB shell, and you should see a prompt similar to this:
You can check the MongoDB version to confirm installation:
This should output the version
4.2.x
.
6. Configure Firewall (Optional)
If your system is running a firewall, you will need to open port 27017
(MongoDB’s default port) to allow remote connections.
Allow port 27017 through the firewall:
If using
firewalld
, open the port:
7. Enable Authentication (Optional)
For security purposes, it’s recommended to enable authentication to ensure that only authorized users can access MongoDB.
Edit the MongoDB configuration file:
Under the
security
section, add the following:Save the file and restart MongoDB:
Create an admin user to manage authentication. Connect to MongoDB:
Switch to the
admin
database and create an admin user:Exit the shell:
Reconnect to MongoDB with authentication:
8. Troubleshooting
MongoDB Not Starting: If MongoDB doesn't start, check the logs for errors:
Permissions Issues: Ensure that the MongoDB data directory (
/var/lib/mongo
) has proper ownership:
9. Uninstall MongoDB (If Necessary)
If you want to uninstall MongoDB at any point, follow these steps:
Stop the MongoDB service:
Remove MongoDB packages:
Optionally, remove MongoDB data directory:
Summary
- Added the MongoDB 4.2 repository for Debian 10/9/8.
- Installed MongoDB using the
apt
package manager. - Started and enabled MongoDB to run at boot.
- Secured MongoDB by enabling authentication and creating an admin user (optional).
- Opened port 27017 to allow remote connections (optional).
MongoDB should now be installed and running on your Debian system! Let me know if you need help with configurations or further steps. 🚀