Linux VPS

MongoDB Community installieren

MongoDB Community Edition über das offizielle Repository auf einem HolyCloud VPS installieren, den Zugriff absichern, Benutzer anlegen und den systemd-Dienst aktivieren.

~13 Minuten Lesezeit Dazwischenliegend #mongodb#nosql#database#mongod

MongoDB speichert JSON-Dokumente (BSON) und eignet sich für Node.js-APIs, Analytics oder strukturierte Caches. Auf einem HolyCloud Linux-VPS mit Ubuntu/Debian erfolgt die Installation über das offizielle Repository von MongoDB Inc. – dieser Leitfaden behandelt Installation, Minimalkonfiguration, Authentifizierung und Firewall.

Voraussetzungen

  • HolyCloud VPS mit Ubuntu 22.04/24.04 (Leitfaden unter Ubuntu getestet; Debian erfordert das entsprechende Repository)
  • Mindestens 2 GB RAM (im Produktivbetrieb 4 GB empfohlen)
  • sudo-Zugriff
  • HolyCloud-Snapshot, bevor Port 27017 nach außen geöffnet wird
  • WiredTiger ist die Standard-Engine (enthalten)

Tipp: Binden Sie mongod an 127.0.0.1, es sei denn, Sie benötigen ausdrücklich einen per IP gefilterten Fernzugriff.

Schritt 1: Abhängigkeiten und GPG-Schlüssel

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

Importieren Sie den Schlüssel für MongoDB 7.0 (die aktuelle Version finden Sie in der MongoDB-Dokumentation):

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

Schritt 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) – ersetzen Sie jammy durch noble, sofern das 7.0-Repository dies unterstützt; verwenden Sie andernfalls die für Ihre Version dokumentierte Serie.

sudo apt update
sudo apt install -y mongodb-org

Installierte Pakete:

dpkg -l | grep mongodb-org

Schritt 3: Start und Dienst

sudo systemctl enable --now mongod
sudo systemctl status mongod

Logdatei:

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

Schritt 4: Grundkonfiguration

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

Empfohlener Auszug für einen 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

Schritt 5: Authentifizierung aktivieren

Lokale Verbindung ohne Authentifizierung (beim ersten Mal):

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

Aktivieren Sie die Sicherheit in /etc/mongod.conf:

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

Schritt 6: Firewall und Fernzugriff (optional)

Öffnen Sie Port 27017 standardmäßig nicht zum Internet. Falls sich eine entfernte Anwendung verbinden muss:

sudo ufw allow from IP_APP_AUTORISEE to any port 27017 proto tcp

Tragen Sie in mongod.conf die lokale IP des VPS oder 0.0.0.0 nur in Verbindung mit einer strikten Firewall ein:

net:
  bindIp: 127.0.0.1,10.0.0.2

Schritt 7: Logisches 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)

Wiederherstellung testen:

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

Überprüfung

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

bindIp muss Ihrer Richtlinie entsprechen (häufig nur 127.0.0.1).

HolyCloud-Support

  • apt findet das Paket nicht: falsche Ubuntu-Serie in der .list-Datei
  • mongod stürzt beim Start ab: Berechtigungen von dbPath, Speicherplatz laut df -h, Log /var/log/mongodb/mongod.log
  • Authentifizierung fehlgeschlagen: Benutzer vor authorization: enabled angelegt – über localhost neu anlegen
  • HolyCloud-Support: mongod --version, Log-Auszug, RAM laut free -h