MongoDB is a popular open-source NoSQL database that offers high performance, scalability, and flexibility. To take advantage of its benefits and ensure the security of your data, it is essential to install and securing MongoDB on Ubuntu 22.04 system. In this guide, we will provide a step-by-step walkthrough to help you install MongoDB and implement necessary security measures.
To install and securing MongoDB on Ubuntu, you need to satisfy the following requirements:
– A Linux VPS with Ubuntu Operating System
– Root access to Server
– 4GB of RAM
Step.1: Update System
Update your system by running the following command in the terminal:
sudo apt update && sudo apt upgrade
Step.2: Remove Existing MongoDB Packages (if any)
If you had an older version of MongoDB installed, remove it using the following commands:
sudo systemctl stop mongod
sudo apt purge mongodb-org*
Step.3: Set Up MongoDB Repository
Add the MongoDB repository to your Ubuntu system using the following command:
echo "deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
Step.4: Import Public GPG Key
Import the public GPG key used to sign the MongoDB packages by running the following command:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
Step.5: Update Repository
Update the repository by running the following command:
sudo apt update
Now you are ready to install MongoDB on Ubuntu 22.04. You can proceed with the installation by running the following command:
sudo apt install -y mongodb-org
Once the installation is complete, verify the version of MongoDB installed:
mongod --version
After the installation, MongoDB will be running as a service on your Ubuntu system. You can start and then enable MongoDB using the following command:
sudo systemctl start mongod
sudo systemctl enable mongod
Then, you can confirm if the service is running:
sudo systemctl status mongod
To secure MongoDB on Ubuntu 22.04, you can follow these steps:
Step.1: Update MongoDB
Start by updating your MongoDB installation to the latest version by running the following commands:
sudo apt update
sudo apt upgrade mongodb
Step.2: Enable Authentication
MongoDB does not enable authentication by default. So, you need to enable it for secure access. Open the MongoDB configuration file using a text editor:
sudo nano /etc/mongodb.conf
Add the following line at the end of the file:
security:
authorization: enabled
Save and exit the file.
Step.3: Create Administrative Use
Switch to the MongoDB shell:
mongo
Switch to the admin database:
use admin
Create the administrative user with a username and password:
db.createUser({ user: "admin-user", pwd: "admin-password", roles: ["root"] })
Replace “admin-user” with your desired username and “admin-password” with your desired password.
Step.4: Restart MongoDB
Restart the MongoDB service to apply the changes:
sudo systemctl restart mongodb
Step.5: Verify Authentication
Try accessing MongoDB with the newly created administrative user:
mongo -u admin-user -p --authenticationDatabase admin
You will be prompted for the password. If you can successfully log in, it means authentication is working.
Step.6: Adjust Firewall Rules
If you have a firewall enabled, allow access to the MongoDB port (default is 27017) to ensure network security.
By following these steps, you can secure MongoDB on Ubuntu 22.04 by enabling authentication and creating an administrative user.
Here are some common issues that you may encounter when installing MongoDB on Ubuntu 22.04 and their corresponding troubleshooting steps:
1. Repository not found:
– Make sure you have added the MongoDB repository correctly. Check if the repository URL is correct and accessible.
– Check if the package repository is enabled in the “/etc/apt/sources.list” file or “/etc/apt/sources.list.d/” directory.
2. Dependency issues:
– Run the following command to fix any dependency issues:
sudo apt --fix-broken install
– Ensure that you have installed the required dependencies for MongoDB.
3. Unable to locate package:
– Double-check if you have entered the correct package name.
– Update the package lists:
sudo apt update
4. Permission issues:
– Make sure you have proper permissions to install packages. Use “sudo” if necessary.
– Check the ownership and permissions of relevant directories.
5. MongoDB service not starting:
– Check the MongoDB service status using the following command:
sudo systemctl status mongod
– Inspect the MongoDB logs in “/var/log/mongodb/” for any error messages.
– Restart the MongoDB service:
sudo systemctl restart mongod
6. Port in use:
– If MongoDB fails to start due to the port being already in use, identify the process using the port and terminate it.
– Check for open ports using below command:
sudo netstat -tuln | grep
– Kill the process:
sudo kill
You have successfully installed and secured MongoDB on your Ubuntu 22.04 system. By following these steps, you have ensured that MongoDB is running on the local interface only and authentication is required to access the database. This will help protect your data and minimize the risk of unauthorized access. Remember to always keep your MongoDB installation up to date with security patches and follow best practices for securing your entire system.
How useful was this post?
Click on a star to rate it!
Average rating 5 / 5. Vote count: 2
No votes so far! Be the first to rate this post.
The impact of the virtual machine in the system is such that it abstracts the computer hardware such...
Xfce is a lightweight desktop environment that is known for its speed, simplicity, and customization...
What is your opinion about this Blog?