How to Create MongoDB on VPS
Set Up MongoDB on Your AlexHost VPS: A Step-by-Step Guide
Why run MongoDB on AlexHost? MongoDB’s NoSQL flexibility and JSON-like documents make it a go-to for dynamic apps, and AlexHost’s VPS offers the perfect environment—root access, SSD speed, and robust security—to run it like a champ. This guide walks you through installing, securing, and managing MongoDB on an Ubuntu 20.04 VPS, ensuring a scalable, high-performance database setup.
Prerequisites
Before you start, make sure 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 RAM for MongoDB (recommended).
- Basic knowledge of the command line interface (CLI) and Linux commands.
Step 1: Update the System
First, make sure your VPS is up to date. Log in over SSH and run the following commands to update your system’s package list and install pending updates:
Once the system has been updated, it is good practice to restart the server to ensure that all updates have been properly applied:
Step 2: Install MongoDB
MongoDB is not included in the default Ubuntu repositories, so you need to add its official repository before installing it. Here’s how to install MongoDB:
Adding the MongoDB Repository
- Import the MongoDB public GPG key:
- Create a list file for MongoDB:
- Update your local package list:
Installing MongoDB Packages
Now, install the MongoDB packages by running the following command:
This installs MongoDB along with other necessary components (such as mongod, MongoDB daemon).
Step 3: Launch and Activate MongoDB
Once installed, you need to start MongoDB and get it running at startup:
Check the status of MongoDB to verify that it is running:
You should see MongoDB listed as active (running). If everything looks good, you are ready to proceed with the configuration.
Step 4: Secure MongoDB
By default, MongoDB allows unauthenticated access, which is not ideal for a production environment. To secure MongoDB, you must enable authentication.
Create Administrator User
- First, access the MongoDB shell:
- Switch to the administrator database:
- Create an admin user by running the following command and replacing adminuser and password with the desired username and password:
- Exit the MongoDB shell:
Enable Authentication
- Open the MongoDB configuration file with a text editor:
- Find the following line in the configuration file:
- Remove the comment and add the following line below it:
- Save the changes and exit the editor (press Ctrl X on Nano, then Y and Enter ).
- Restart MongoDB to apply the changes:
Step 5: Configure MongoDB Remote Access (Optional)
By default, MongoDB only listens on localhost (127.0.0.0.1), meaning it can only be accessed from the VPS itself. If you need remote access, you need to configure MongoDB to allow connections from external IP addresses. Here’s what we will do:
- Open the MongoDB configuration file:
- Find the line below:
- Replace it with this one:
This ensures that MongoDB accepts connections from all IP addresses.
- Save the changes and exit the editor.
- Restart MongoDB:
Secure Remote Access with Firewall
Configure your VPS firewall to allow only trusted IP addresses to connect to MongoDB. If you are 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):
- To enable UFW (if not enabled):
- Check the UFW status to verify that the rule has been added:
Step 6: Test MongoDB Installation
To verify that MongoDB is working correctly, you can access the MongoDB shell and authenticate using the user you created earlier:
- Connect to MongoDB:
- You should now be logged into the MongoDB shell as an admin user.
Step 7: Backup and Maintenance
It is very important to back up your MongoDB databases regularly, especially in a production environment. You can use the mongodump tool to back up your data:
You can restore the data using mongorestore:
Conclusion: MongoDB + AlexHost = Database Powerhouse
Setting up MongoDB on your AlexHost VPS is straightforward—install, secure, and back up for a robust NoSQL database ready for your apps. With AlexHost’s SSDs and root access, you get top-tier performance and control. Run mongo, create users, and automate backups to keep your data safe. Whether it’s a web app or big data project, you’re now set to dominate. AlexHost support’s got your back—happy coding!