MongoDB is a popular NoSQL database known for its flexibility, scalability, and performance. It stores data in flexible, JSON-like documents, which is perfect for developers working with dynamic, fast-evolving applications. If you’ve opted to run MongoDB on your own Virtual Private Server (VPS), you’re in for a rewarding experience that will give you full control over your database environment. This guide will take you through the step-by-step process of setting up MongoDB on a VPS.
Prerequisites
Before we start, ensure you have the following:
- A VPS with root access.
- Ubuntu 20.04 or a similar Linux distribution.
- SSH access to your VPS.
- At least 2GB of RAM (recommended) for MongoDB.
- Basic knowledge of command-line interface (CLI) and Linux commands.
Step 1: Update the System
First, make sure your VPS is up-to-date. Log in via SSH and run the following commands to update your system’s package list and install any pending updates:
sudo apt update
sudo apt upgrade -y
After the system is updated, it’s good practice to reboot the server to ensure all updates are applied properly:
sudo reboot
Step 2: Install MongoDB
MongoDB is not included in the default Ubuntu repositories, so you’ll need to add its official repository before installing it. Here’s how to install MongoDB:
Add the MongoDB Repository
- Import the MongoDB public GPG key:
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
- Create a list file for MongoDB:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
- Update your local package list:
sudo apt update
Install MongoDB Packages
Now, install the MongoDB packages by running the following command:
sudo apt install -y mongodb-org
This installs MongoDB along with other required components (such as mongod, the MongoDB daemon).
Step 3: Start and Enable MongoDB
Once installed, you need to start MongoDB and enable it to run at startup:
sudo systemctl start mongod
sudo systemctl enable mongod
To confirm that MongoDB is running, check its status:
sudo systemctl status mongod
You should see MongoDB listed as active (running). If everything looks good, you’re ready to move on to configuration.
Step 4: Secure MongoDB
By default, MongoDB allows unauthenticated access, which is not ideal for a production environment. To secure MongoDB, you should enable authentication.
Create an Admin User
- First, access the MongoDB shell:
mongo
- Switch to the admin database:
use admin
- Create an admin user by running the following command, replacing adminuser and password with your desired username and password:
db.createUser({
user: "adminuser",
pwd: "password",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
})
- Exit the MongoDB shell:
exit
Enable Authentication
- Open the MongoDB configuration file with a text editor:
sudo nano /etc/mongod.conf
- In the configuration file, find the following line:
#security:
- Uncomment it and add the following line beneath it:
security:
authorization: enabled
- Save the changes and exit the editor (in Nano, press Ctrl + X, then Y and Enter).
- Restart MongoDB to apply the changes:
sudo systemctl restart mongod
Step 5: Configure MongoDB Remote Access (Optional)
By default, MongoDB listens only on localhost (127.0.0.1), which means it’s accessible only from the VPS itself. If you need remote access, you’ll have to configure MongoDB to allow connections from external IP addresses. Here’s how:
- Open the MongoDB configuration file:
sudo nano /etc/mongod.conf
- Find the following line:
bindIp: 127.0.0.1
- Replace it with:
bindIp: 0.0.0.0
This allows MongoDB to accept connections from all IP addresses.
- Save the changes and exit the editor.
- Restart MongoDB:
sudo systemctl restart mongod
Secure Remote Access with a Firewall
Allow only trusted IP addresses to connect to MongoDB by configuring your VPS firewall. If you’re using UFW (Uncomplicated Firewall), here’s how to allow remote connections on MongoDB’s default port (27017):
- Allow access from a specific IP (replace your_ip with your IP address):
sudo ufw allow from your_ip to any port 27017
- To enable UFW (if it’s not enabled):
sudo ufw enable
- Check UFW status to confirm the rule was added:
sudo ufw status
Step 6: Test MongoDB Setup
To verify that MongoDB is working correctly, you can access the MongoDB shell and authenticate using the user you created earlier:
- Connect to MongoDB:
mongo -u adminuser -p --authenticationDatabase admin
- You should now be logged into the MongoDB shell as the admin user.
Step 7: Backup and Maintenance
It’s critical to back up your MongoDB databases regularly, especially in a production environment. You can use the mongodump tool to back up your data:
mongodump --out /path/to/backup/directory
You can restore the data using mongorestore:
mongorestore /path/to/backup/directory
Conclusion
By following these steps, you’ve successfully installed and configured MongoDB on your VPS. You now have full control over your MongoDB environment, which can be fine-tuned to meet your specific needs. Whether you’re using MongoDB for web applications, big data, or just experimenting, your VPS-based MongoDB installation will offer both flexibility and performance.
Remember to monitor your MongoDB instance regularly and keep it secure by enabling authentication and limiting access through firewalls. Happy coding!