Comprehensive Guide to Installing MongoDB Community Edition on Ubuntu
Install MongoDB Community Edition on Ubuntu
MongoDB Community Edition is a versatile NoSQL database suited for scalable, high-performance applications. Installing it on Ubuntu involves adding the official MongoDB repository, installing the required packages, and configuring MongoDB for use.
This guide provides a detailed, step-by-step procedure for installing MongoDB Community Edition on Ubuntu.
Prerequisites
Before proceeding, ensure the following:
1. Operating System: Ubuntu 20.04 or later.
2. User Privileges: Administrator access with sudo.
3. Internet Connection: To download MongoDB packages from the official repository.
Step 1: Import the MongoDB Public GPG Key
Import the public key for verifying downloaded packages:
# Import the MongoDB GPG key curl -fsSL https://pgp.mongodb.com/server-6.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-6.0.gpg --dearmor
Step 2: Add the MongoDB Repository
Add MongoDB's repository to your system:
# Create the MongoDB repository file echo "deb [signed-by=/usr/share/keyrings/mongodb-server-6.0.gpg] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
Step 3: Update the Package List and Install MongoDB
1. Update the package list:
# Update the system's package list sudo apt update
2. Install MongoDB:
# Install MongoDB Community Edition sudo apt install -y mongodb-org
Step 4: Start and Enable MongoDB
1. Start the MongoDB service:
# Start the MongoDB service sudo systemctl start mongod
2. Enable MongoDB to start on boot:
# Enable MongoDB to start automatically sudo systemctl enable mongod
Step 5: Verify Installation
Check the status of MongoDB:
# Check the MongoDB service status sudo systemctl status mongod
If the installation was successful, the status should indicate that the service is active.
Test the MongoDB shell:
# Access the MongoDB shell mongosh
Step 6: Configure Security (Optional)
Enable Authentication
To secure MongoDB with user authentication:
1. Edit the MongoDB configuration file:
sudo nano /etc/mongod.conf
2. Add or modify the security section:
security: authorization: "enabled"
3. Restart the MongoDB service:
sudo systemctl restart mongod
Step 7: Configure Firewall (Optional)
If you want MongoDB to accept remote connections, open port 27017:
# Allow MongoDB traffic through the firewall sudo ufw allow 27017/tcp
Example Commands for MongoDB on Ubuntu
Insert a Document
Code:
// Insert a sample document into the 'products' collection
db.products.insertOne({ name: "MongoDB", type: "Database", version: 6.0 });
Find a Document
Code:
// Find all documents in the 'products' collection
db.products.find();
Troubleshooting Tips
Issue | Solution |
---|---|
mongod fails to start | Check the logs in /var/log/mongodb/mongod.log. |
Key error during installation | Ensure you correctly imported MongoDB's GPG key. |
Connection refused remotely | Verify the firewall rules and ensure MongoDB is bound to the correct IP address. |
Additional Notes:
- Version Compatibility: Ensure you are installing a version of MongoDB supported by your Ubuntu version.
- SELinux Users: If SELinux is enabled, adjust policies to allow MongoDB operations.
- Backup: Before upgrading or modifying MongoDB, back up your data to prevent data loss.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics