๐Ÿš€ how to deploy mongodb on ubuntu vps
Learn how to deploy mongodb on ubuntu vps with this step-by-step guide.

This article demonstrates how to deploy MongoDB on Ubuntu VPS. This guide walks you through the step-by-step process of deploying MongoDB on an Ubuntu VPS (Ubuntu 20.04 or 22.04).

What is MongoDB?

MongoDB is a NoSQL, open-source, document-oriented database designed for high performance, scalability, and flexibility. Unlike traditional relational databases (like MySQL or PostgreSQL), MongoDB stores data in JSON-like documents (called BSON โ€“ Binary JSON), which allows for more dynamic and hierarchical data structures.

๐Ÿ”‘ Key Features:

  • Schema-less: Documents in the same collection can have different structures.
  • Scalable: Supports horizontal scaling via sharding.
  • Flexible data model: Great for unstructured or semi-structured data.
  • Rich query language: Supports ad-hoc queries, indexing, and aggregation.
  • High performance: Efficient storage and fast read/write operations.

๐Ÿ“ฆ Common Use Cases:

  • Real-time analytics
  • Content management systems (CMS)
  • IoT applications
  • Mobile and web apps requiring rapid iteration

๐Ÿ“‹ Example Document (BSON):

{
"name": "Jane Doe",
"email": "jane@example.com",
"orders": [
{ "product": "Book", "price": 9.99 },
{ "product": "Pen", "price": 1.49 }
]
}

๐Ÿ“˜ Editions:

  • MongoDB Community Edition: Free and open-source.
  • MongoDB Enterprise: Includes advanced security, monitoring, and support.
  • MongoDB Atlas: Fully managed cloud version.

โœ… Prerequisites

Launch 100% ssd ubuntu vps from $2. 49/mo!
Instantly launch 100% ssd ubuntu vps servers!

๐Ÿ› ๏ธ How to Deploy MongoDB on Ubuntu VPS

To deploy MongoDB on Ubuntu VPS, follow the steps provided below:

  1. Update Your Server

    Login as root user via SSH, and run the following command:

    sudo apt update && sudo apt upgrade -y
  2. Import MongoDB GPG Key

    sudo apt-get install gnupg curl curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \ sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \ --dearmor
  3. Add MongoDB Repository

    • For Ubuntu 22.04 (Jammy):

      echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
    • For Ubuntu 20.04 (Focal):

      echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

      Then update the package index:

      sudo apt update
  4. Install MongoDB

    sudo apt install -y mongodb-org
    

    This installs:

    • mongod: the MongoDB daemon
    • mongo: the MongoDB shell
    • mongos, mongodb-org-server, etc.
  5. Start and Enable MongoDB

    sudo systemctl start mongod sudo systemctl enable mongod

    Check status:

    sudo systemctl status mongod
  6. Secure MongoDB

    By default, MongoDB listens on 127.0.0.1. For external access:

    1. Allow access from remote IPs (if needed):

      Edit the config file:

      sudo nano /etc/mongod.conf

      Find the bindIp line and change:

       bindIp: 127.0.0.1,0.0.0.0

      Then restart MongoDB:

      sudo systemctl restart mongod

      โš ๏ธ Use a firewall to restrict access to trusted IPs only.

    2. Create admin user and enable authentication:

      Start Mongo shell:

      mongosh

      Switch to admin and create user:

      use admin db.createUser({ user: "admin", pwd: "strong_password_here", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })

      Exit shell and enable authentication in config file:

      sudo nano /etc/mongod.conf

      Add or uncomment:

      security: authorization: enabled

      Restart:

      sudo systemctl restart mongod

      Test login:

      mongosh -u admin -p --authenticationDatabase admin
  7. Configure UFW Firewall (Optional but Recommended)

    sudo ufw enable sudo ufw allow OpenSSH sudo ufw allow 27017/tcp sudo ufw status

    Replace 27017 with the specific port if you change the default.

  8. Monitor and Manage MongoDB

    Basic commands:

    # Check MongoDB status sudo systemctl status mongod # Stop MongoDB sudo systemctl stop mongod # Start MongoDB sudo systemctl start mongod # Restart MongoDB sudo systemctl restart mongod
Launch 100% ssd ubuntu vps from $2. 49/mo!
Instantly launch 100% ssd ubuntu vps servers!

Conclusion

You now know how to deploy MongoDB on Ubuntu VPS. Having following the steps outlined above, MongoDB is now installed, secured, and running on your Ubuntu VPS. You can now begin developing applications or integrating MongoDB with other services on your stack.

Share this:
Avatar of editorial staff

Editorial Staff

Rad Web Hosting is a leading provider of web hosting, Cloud VPS, and Dedicated Servers in Dallas, TX.
lg