Back to site

Install MongoDB Community

Install MongoDB Community Edition on a HolyCloud VPS via the official repository, secure access, create users, and enable the systemd service.

Install MongoDB Community

MongoDB stores JSON documents (BSON), suited for Node.js APIs, analytics, or structured caches. On a HolyCloud Linux VPS running Ubuntu/Debian, installation uses the official MongoDB Inc. repository — this guide covers installation, minimal configuration, authentication, and firewall.

Prerequisites

  • HolyCloud VPS Ubuntu 22.04/24.04 (guide tested on Ubuntu; Debian requires the matching repository)
  • At least 2 GB RAM (4 GB recommended in production)
  • sudo access
  • HolyCloud snapshot before exposing port 27017
  • WiredTiger is the default engine (included)

Tip: Bind mongod to 127.0.0.1 unless you explicitly need remote access filtered by IP.

Step 1: Dependencies and GPG key

sudo apt update
sudo apt install -y gnupg curl ca-certificates

Import MongoDB 7.0 key (check MongoDB documentation for the current version):

curl -fsSL https://pgp.mongodb.com/server-7.0.asc | \
  sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor

Step 2: APT repository

Ubuntu 22.04 (jammy):

echo "deb [ 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

Ubuntu 24.04 (noble) — replace jammy with noble if the 7.0 repository supports it; otherwise use the series documented for your version.

sudo apt update
sudo apt install -y mongodb-org

Installed packages:

dpkg -l | grep mongodb-org

Step 3: Start and service

sudo systemctl enable --now mongod
sudo systemctl status mongod

Log file:

sudo tail -30 /var/log/mongodb/mongod.log

Step 4: Basic configuration

sudo cp /etc/mongod.conf /etc/mongod.conf.bak.$(date +%F)
sudo nano /etc/mongod.conf

Recommended excerpt for a web/API VPS:

storage:
  dbPath: /var/lib/mongodb
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
net:
  port: 27017
  bindIp: 127.0.0.1
processManagement:
  timeZoneInfo: /usr/share/zoneinfo
sudo systemctl restart mongod
sudo ss -tlnp | grep 27017

Step 5: Enable authentication

Local connection without auth (first time):

mongosh

In mongosh:

db.getSiblingDB('admin').createUser({
  user: 'adminMongo',
  pwd: 'MotDePasseFortIci!',
  roles: [ { role: 'root', db: 'admin' } ]
})
db.getSiblingDB('monapp').createUser({
  user: 'appUser',
  pwd: 'MotDePasseAppIci!',
  roles: [ { role: 'readWrite', db: 'monapp' } ]
})
exit

Enable security in /etc/mongod.conf:

security:
  authorization: enabled
sudo systemctl restart mongod
mongosh -u adminMongo -p --authenticationDatabase admin

Step 6: Firewall and remote access (optional)

By default, do not open 27017 to the Internet. If a remote app must connect:

sudo ufw allow from IP_APP_AUTORISEE to any port 27017 proto tcp

In mongod.conf, add the VPS local IP or 0.0.0.0 only with a strict firewall:

net:
  bindIp: 127.0.0.1,10.0.0.2

Step 7: Logical backup

mongodump --uri="mongodb://appUser:[email protected]:27017/monapp" --out=/var/backups/mongo-$(date +%F)
tar -czf /var/backups/mongo-$(date +%F).tar.gz /var/backups/mongo-$(date +%F)

Test restore:

mongorestore --uri="mongodb://appUser:[email protected]:27017" --drop /var/backups/mongo-$(date +%F)/monapp

Verification

systemctl is-active mongod
mongosh --eval 'db.runCommand({ connectionStatus: 1 })' -u appUser -p --authenticationDatabase monapp
sudo ss -tlnp | grep 27017

bindIp must reflect your policy (often 127.0.0.1 only).

HolyCloud support

  • apt cannot find the package: wrong Ubuntu series in the .list file
  • mongod crashes on start: dbPath permissions, disk space df -h, log /var/log/mongodb/mongod.log
  • Auth failed: user created before authorization: enabled — recreate via localhost
  • HolyCloud support: mongod --version, log excerpt, RAM free -h