Install MongoDB on Ubuntu
Summary
MongoDB is a popular NoSQL database that stores data in flexible, JSON-like documents. This tutorial will help you install the latest MongoDB Community Edition on Ubuntu, verify the installation, and start using the MongoDB shell.
Key Vocabulary
Prerequisites
- Ubuntu 20.04 or later
- Sudo/root access
- Internet connection
Step-by-Step Instructions
Import the MongoDB GPG Key
Add MongoDB's official GPG key to verify package authenticity.
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-7.0.gpg
Add the MongoDB Repository
Add the official MongoDB repository to your system.
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.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
Update Package Lists
Refresh your package lists to include the new MongoDB repository.
sudo apt update
Install MongoDB
Install the MongoDB server and tools.
sudo apt install mongodb-org -y
Start MongoDB Service
Start the MongoDB service.
sudo systemctl start mongod
Enable MongoDB at Boot
Enable MongoDB to launch automatically at system boot.
sudo systemctl enable mongod
Check MongoDB Status
Verify that MongoDB is running.
sudo systemctl status mongod
Access the MongoDB Shell
Open the MongoDB shell to interact with your database.
mongosh
Check MongoDB Version
Check the installed MongoDB version inside the shell.
db.version()
Common Issues & Solutions
Solution: MongoDB may not be installed or not in your PATH. Try reinstalling or check your PATH.
Solution: Check logs with 'sudo journalctl -u mongod'. Common issues: port 27017 in use, permission problems, or config file errors.
Solution: MongoDB isn't running. Start it with 'sudo systemctl start mongod'.
Conclusion
Congratulations! You have successfully installed MongoDB on Ubuntu. You can now create databases, collections, and documents for your applications. For advanced usage, explore MongoDB's security, backup, and performance features.