How to Install MongoDB 6.0.3 on Ubuntu 22.04

1. Check if MongoDB is Installed

            
mongod --version

This command will display the installed version of MongoDB if it is already present on your system.

2. Add MongoDB GPG Key

To securely install MongoDB, you need to add its official GPG key:

            
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -

3. Add MongoDB Repository

Next, add the MongoDB repository to your system's source list:

            
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list

4. Update Package Index

            
sudo apt update

5. Install MongoDB

After updating the package index, proceed with the installation:

            
sudo apt install -y mongodb-org

6. Verify Installation

            
mongod --version

This confirms that MongoDB has been successfully installed.

7. Enable MongoDB to Start on Boot

            
sudo systemctl enable mongod

8. Start MongoDB Service

            
sudo systemctl start mongod

9. Check MongoDB Status

            
sudo systemctl status mongod

This command will display the current status of the MongoDB service.

10. Configure MongoDB

Edit the MongoDB configuration file to enhance security and manage connections:

            
sudo nano /etc/mongod.conf

Modify the configuration as follows:

            
security: authorization: enabled net: port: 27017 bindIp: 127.0.0.1

11. Check Firewall Settings

            
sudo ufw status verbose

Ensure that the firewall allows traffic on the MongoDB port (27017).

12. Allow MongoDB Port Through the Firewall

            
sudo ufw allow 27017

13. Restart MongoDB

            
sudo systemctl restart mongod

14. Verify Remote Connections

Check if MongoDB is accepting remote connections:

            
sudo lsof -i | grep mongo

This command will list active connections to the MongoDB service.

15. Additional Security Recommendations

  • Enable Authentication: Always ensure that MongoDB authentication is enabled in production environments.
  • Use Firewall Rules: Limit access to the MongoDB port to specific IP addresses.
  • Regular Backups: Implement a backup strategy to protect your data.
  • Monitor Logs: Regularly check MongoDB logs for any unauthorized access attempts.