Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
23.10.2024
No categories

How to Create a MongoDB on a VPS

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:

  1. A VPS with root access.
  2. Ubuntu 20.04 or a similar Linux distribution.
  3. SSH access to your VPS.
  4. At least 2GB of RAM (recommended) for MongoDB.
  5. 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

  1. Import the MongoDB public GPG key:
    wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
  2. 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
  3. 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

  1. First, access the MongoDB shell:
    mongo
  2. Switch to the admin database:
    use admin
  3. 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

  1. Open the MongoDB configuration file with a text editor:
    sudo nano /etc/mongod.conf
  2. In the configuration file, find the following line:
    #security:
  3. Uncomment it and add the following line beneath it:
    security:
    authorization: enabled
  4. Save the changes and exit the editor (in Nano, press Ctrl + X, then Y and Enter).
  5. 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:

  1. Open the MongoDB configuration file:
    sudo nano /etc/mongod.conf
  2. Find the following line:
    bindIp: 127.0.0.1
  3. Replace it with:
    bindIp: 0.0.0.0

    This allows MongoDB to accept connections from all IP addresses.

  4. Save the changes and exit the editor.
  5. 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):

  1. Allow access from a specific IP (replace your_ip with your IP address):
    sudo ufw allow from your_ip to any port 27017
  2. To enable UFW (if it’s not enabled):
    sudo ufw enable
  3. 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:

  1. Connect to MongoDB:
    mongo -u adminuser -p --authenticationDatabase admin
  2. 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!

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills