Log inSign up

MongoDB on a VPS with authentication switched on

Add the official MongoDB repository for your distribution, install the mongodb-org package and start mongod. Then create an administrator in mongosh, set security.authorization: enabled in /etc/mongod.conf, keep net.bindIp on 127.0.0.1 and restart. That gives you a server that refuses anonymous access and is not reachable from the internet.

Topic: Databases · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

What do you need before installing MongoDB?

You need a 64-bit server on a supported distribution, a CPU that exposes the AVX instruction set, and enough memory for the WiredTiger cache. MongoDB does not ship in Ubuntu, Debian, Rocky or Alma archives, so the vendor repository is the only maintained source.

Check AVX first, because recent releases stop at start-up with an illegal instruction error when it is missing:

grep -o -m1 'avx[^ ]*' /proc/cpuinfo
uname -m

The 8.0 series is the current long-lived release used in this guide and supports Ubuntu 24.04 and 22.04, Debian 12 and Red Hat compatible 9 systems. Debian 13 was not on the 8.0 platform list when this was written, so check the MongoDB platform support page before installing there, or choose Debian 12. By default WiredTiger takes about half of RAM minus 1 GB for its cache, so 2 GB is a practical minimum and 4 GB is comfortable.

How do you install MongoDB on Ubuntu?

Import the signing key into a keyring file, add the repository line for your Ubuntu codename, then install the meta package.

sudo apt update
sudo apt install -y gnupg curl
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list
sudo apt update
sudo apt install -y mongodb-org
sudo systemctl enable --now mongod
mongod --version

lsb_release -cs prints noble on 24.04 and jammy on 22.04, so one command covers both.

How do you install MongoDB on Debian?

The steps match Ubuntu, but the repository path uses debian, the codename and the main component.

sudo apt update
sudo apt install -y gnupg curl
curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor
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
sudo apt update
sudo apt install -y mongodb-org
sudo systemctl enable --now mongod

The codename is written out here as bookworm (Debian 12) on purpose: pointing a Debian 13 server at the bookworm path may install, but it is not a combination the vendor tests.

How do you install MongoDB on Rocky Linux or AlmaLinux?

Create a repo file that points at the Red Hat 9 tree, then install with dnf.

sudo tee /etc/yum.repos.d/mongodb-org-8.0.repo >/dev/null <<'EOF'
[mongodb-org-8.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/9/mongodb-org/8.0/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://pgp.mongodb.com/server-8.0.asc
EOF
sudo dnf install -y mongodb-org
sudo systemctl enable --now mongod
mongod --version

The packaged unit, paths and SELinux labels work together as long as you keep the default data directory /var/lib/mongo. If you move data to another path, label it for SELinux and adjust ownership to the mongod user before starting the service.

How do you create the first administrator?

While access control is still off, connect locally with mongosh and create a user in the admin database with user and database management roles.

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

passwordPrompt() asks for the password interactively so it never lands in your shell history. Do this right after install; until the next step the server accepts anyone who can reach the local port.

How do you enable authentication and set bindIp?

Edit /etc/mongod.conf so the net and security blocks read as below, then restart mongod.

net:
  port: 27017
  bindIp: 127.0.0.1

security:
  authorization: enabled
sudo systemctl restart mongod
sudo ss -ltnp | grep 27017

YAML is picky: use spaces, not tabs, and keep the two-space indent. If an application on a second server needs the database, list the addresses it should listen on, for example bindIp: 127.0.0.1,198.51.100.24, and allow only that client in the firewall:

sudo ufw allow from 203.0.113.50 to any port 27017 proto tcp      # Ubuntu, Debian
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.50" port port="27017" protocol="tcp" accept'
sudo firewall-cmd --reload                                         # Rocky, Alma

Never use bindIp: 0.0.0.0 with authorization off; scanners look for exactly that. For laptop access, an SSH tunnel such as ssh -N -L 27018:127.0.0.1:27017 deploy@198.51.100.24 avoids opening the port at all, as described in remote database access.

How do you add an application user and test it?

Log in as the administrator, create a user scoped to the app database with readWrite, then connect as that user and write a document.

mongosh -u siteadmin -p --authenticationDatabase admin
use shopdb
db.createUser({ user: "shopapp", pwd: passwordPrompt(), roles: [ { role: "readWrite", db: "shopdb" } ] })
exit

mongosh "mongodb://shopapp@127.0.0.1:27017/shopdb"
db.orders.insertOne({ sku: "vps-a2", qty: 1, at: new Date() })
db.orders.find()

The connection string your application uses follows the same pattern: mongodb://shopapp:PASSWORD@127.0.0.1:27017/shopdb. Percent-encode special characters in the password.

How do you back up, upgrade and troubleshoot MongoDB?

Use mongodump for logical backups, upgrade one major series at a time, and read /var/log/mongodb/mongod.log when the service fails.

mongodump --uri="mongodb://siteadmin@127.0.0.1:27017/?authSource=admin" --gzip --archive=mongo-$(date +%F).gz
sudo journalctl -u mongod -n 40 --no-pager
sudo tail -n 40 /var/log/mongodb/mongod.log

For a major upgrade, set the feature compatibility version on the old release first, then change the series in the repo file and install. Common start-up failures are a YAML indentation mistake, wrong ownership of the data directory after a restore, a full disk and a missing AVX flag. For higher availability, three servers in a replica set are the usual step; see the replica set guide. The optional daily backup add-on adds a whole-disk copy, but keep your own dumps too.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A2-4room for the WiredTiger cache2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months
A4-8larger collections and indexes4 vCPU, 8 GB RAM, 180 GB NVMe$28.80/mo$86.40 for 3 months

Questions about Install MongoDB

Why is MongoDB not in the Ubuntu or Debian archive?

Its licence (SSPL) is not accepted by those distributions, so they stopped packaging it. The vendor repository is the supported way to get current releases with security fixes.

Which MongoDB version does this guide install?

The 8.0 series. To use a newer series, replace 8.0 in the key URL, keyring name and repo path with the new number, after checking that your distribution is on its platform list.

mongod exits with an illegal instruction error. Why?

The CPU flags do not include AVX, which current releases require. Check with grep avx /proc/cpuinfo. If nothing prints, older 4.4 builds are the only option, and those no longer receive fixes.

Is authorization enabled by default?

No. A fresh install accepts any local connection until you set security.authorization: enabled. Create the admin user and switch it on before loading real data.

How much RAM does MongoDB need?

The WiredTiger cache defaults to about half of RAM minus 1 GB, with a floor of 256 MB. 2 GB works for development; production databases are more comfortable at 4 GB or more.