Managing MongoDB on a Linux VPS involves installation, configuration, security, and performance optimization.
1. Installing MongoDB on Linux VPS
MongoDB can be installed using package managers like apt (for Debian/Ubuntu) or yum (for CentOS/RHEL).
For Ubuntu/Debian
- Update package list:
- Import MongoDB GPG key:
- Add MongoDB repository:
- Install MongoDB:
- Start and Enable MongoDB:
For CentOS/RHEL
- Add MongoDB repository:
- Install MongoDB:
- Start and Enable MongoDB:
2. Securing MongoDB
By default, MongoDB listens on localhost (127.0.0.1). To secure it:
Enable Authentication
- Create an admin user:
- Enable authentication in MongoDB config file:
Find the security section and add:
- Restart MongoDB:
Restrict External Access
- Modify MongoDB to listen only to localhost:
Change:
- Use UFW (Ubuntu) or FirewallD (CentOS) to allow only specific IPs:
3. Managing MongoDB
Check MongoDB Service Status
Restart MongoDB
Stop MongoDB
Enable MongoDB to Start on Boot
4. Managing Databases in MongoDB
Connect to MongoDB
Create a Database
Show Databases
Create a Collection
Insert Data
Find Data
Delete a Database
5. Backups and Restores
Backup MongoDB Database
Restore MongoDB Database
6. Performance Optimization
Indexing for Faster Queries
Monitoring Performance
Limit Memory Usage
Modify wiredTigerCacheSizeGB in /etc/mongod.conf:
Use Connection Pooling
Modify /etc/mongod.conf:
7. Logs and Debugging
View MongoDB Logs
Check Error Logs
8. Uninstall MongoDB (If Needed)
For Ubuntu/Debian
For CentOS/RHEL
Conclusion
Managing MongoDB on a Linux VPS requires proper installation, security hardening, and performance optimization. By following these steps, you ensure your MongoDB database runs efficiently and securely. 🚀
Would you like help with automation scripts for managing MongoDB? 😊