15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started
18.09.2025

How to Create MongoDB on VPS

MongoDB is a popular, high-performance NoSQL database used for modern applications that need flexible data models, fast reads/writes, and easy horizontal scaling. If you’re deploying an API, SaaS platform, CRM, e-commerce backend, or any data-driven service, running MongoDB on a VPS gives you full control over performance, security, and costs.

In this guide, you’ll learn how to install MongoDB Community Edition (latest supported release) on a Linux VPS using the official MongoDB repositories, start and enable the service, create users and databases, and apply essential security best practices (authentication, safe network binding, and firewall rules). The steps are written for production-style deployments and include common troubleshooting tips so you can get MongoDB running reliably from day one.

Prerequisites (quick checklist)

  • A VPS with root/sudo access

  • 64-bit OS (MongoDB 8.0 supports Debian 12 Bookworm, Ubuntu LTS)

  • Recommended for production: read MongoDB’s production notes (filesystems, memory, ulimits, etc.)

Update your server first:

sudo apt-get update && sudo apt-get -y upgrade
sudo apt-get install -y gnupg curl

Important: don’t use the distro mongodb package (it can conflict with mongodb-org).

 Install MongoDB on Debian 12 (Bookworm)

Add the official key + repository

curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-8.0.gpg
echo "deb [signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg] https://repo.mongodb.org/apt/debian bookworm/mongodb-org/8.0 main" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list > /dev/null

Install

sudo apt-get update
sudo apt-get install -y mongodb-org

(These commands are from MongoDB’s official Debian 12 install docs.)

Install MongoDB on Ubuntu LTS (24.04 / 22.04)

Add the official key + repository

curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor

Ubuntu 24.04 (Noble):

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

Ubuntu 22.04 (Jammy):

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

Install

sudo apt-get update
sudo apt-get install -y mongodb-org

 Start MongoDB (and fix “mongod.service not found”)

sudo systemctl daemon-reload
sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod --no-pager

MongoDB specifically notes daemon-reload if you see “Unit mongod.service not found”.

“Create” your database + users (recommended flow)

Open the shell locally on the VPS:

mongosh

Create an admin user

In mongosh:

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

Create an application database + user

use myapp
db.createUser({
user: "myapp_user",
pwd: "STRONG_PASSWORD_HERE",
roles: [ { role: "readWrite", db: "myapp" } ]
})

 Secure MongoDB (don’t skip this)

MongoDB’s security checklist strongly recommends hardening before exposing anything.

A) Keep MongoDB private (best practice)

MongoDB warns to bind only to trusted networks.
In /etc/mongod.conf, ensure it listens only on localhost (or a private LAN IP):

net:
bindIp: 127.0.0.1

Restart:

sudo systemctl restart mongod

B) Enable authentication

Edit /etc/mongod.conf:

security:
authorization:enabled

Restart:

sudo systemctl restart mongod

C) Firewall rule (only if you truly need remote access)

Avoid opening 27017 to the public internet. If you must allow access, restrict to a single IP (example with UFW):

sudo ufw allow from YOUR.PUBLIC.IP.ADDRESS to any port 27017 proto tcp
sudo ufw enable
sudo ufw status

D) Safer remote access (SSH tunnel)

From your laptop:

ssh -L 27017:127.0.0.1:27017 root@YOUR_VPS_IP

Then connect locally:

mongosh "mongodb://myapp_user:PASS@127.0.0.1:27017/myapp?authSource=myapp"

 Backup (simple and reliable)

MongoDB provides mongodump for logical backups.

Example:

mongodump --archive=/root/mongo-backup.archive --gzip

Restore:

mongorestore --archive=/root/mongo-backup.archive --gzip

 Common checks (when something fails)

sudo journalctl -u mongod --no-pager -n 200
sudo ss -lntp | grep 27017
mongod --version
15%

Save 15% on All Hosting Services

Test your skills and get Discount on any hosting plan

Use code:

Skills
Get Started