How to Install MongoDB on Ubuntu
Introduction
MongoDB is a popular NoSQL database that provides high performance, scalability, and flexibility for modern applications. This guide will walk you through the step-by-step process of installing MongoDB on an Ubuntu system.
Step 1: Update System Packages
Before installing MongoDB, update the system package index to ensure you have the latest versions.
sudo apt update
sudo apt upgrade -y
Step 2: Import MongoDB GPG Key
MongoDB provides a GPG key to verify the integrity of the packages. Import it using the following command:
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-keyring.gpg
Step 3: Add MongoDB Repository
To install the latest stable version of MongoDB, add the official repository to your system:
echo "deb [signed-by=/usr/share/keyrings/mongodb-server-keyring.gpg] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Replace 7.0
with the latest available version if needed.
Step 4: Install MongoDB
After adding the repository, update the package list again and install MongoDB:
sudo apt update
sudo apt install -y mongodb-org
Step 5: Start and Enable MongoDB Service
Once the installation is complete, start and enable the MongoDB service to ensure it runs automatically at boot.
sudo systemctl start mongod
sudo systemctl enable mongod
To verify that MongoDB is running correctly, use:
sudo systemctl status mongod
You should see an output indicating that MongoDB is active (running).
Step 6: Verify MongoDB Installation
To check if MongoDB is working properly, connect to the MongoDB shell:
mongosh
If MongoDB is installed correctly, you should see the MongoDB shell prompt:
MongoDB shell version v7.0.0
>
To exit the MongoDB shell, type:
exit
Step 7: Configure Firewall (Optional)
If you need to access MongoDB remotely, allow traffic on port 27017 (default MongoDB port):
sudo ufw allow 27017
For security reasons, it is recommended to restrict access to only trusted IPs.
Step 8: Enable Authentication (Optional)
By default, MongoDB does not require authentication. To enable it:
- Open the MongoDB configuration file:
sudo nano /etc/mongod.conf
- Find the
security
section and uncomment/add:security: authorization: enabled
- Save the file (CTRL+X, then Y, then ENTER) and restart MongoDB:
sudo systemctl restart mongod
- Create an admin user in the MongoDB shell:
use admin db.createUser({user:"admin", pwd:"yourpassword", roles:[{role:"root", db:"admin"}]})
Step 9: Uninstall MongoDB (If Needed)
If you ever need to remove MongoDB, use the following commands:
sudo systemctl stop mongod
sudo apt purge mongodb-org -y
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
Conclusion
You have successfully installed and configured MongoDB on Ubuntu! You can now start using MongoDB to store and manage your data efficiently. If you need additional configurations, refer to the MongoDB Documentation: https://www.mongodb.com/docs/
Read This: https://iotbyhvm.ooo/what-is-a-time-series-database/
nice post.