Cloud VPS from $1.98/mo · pay with Bitcoin, Ethereum, Monero, USDT or Solana · paid in crypto
Databases

Install MongoDB 9.0 on Ubuntu and turn on authentication

Install MongoDB 9.0 Community on Ubuntu 24.04 or 26.04 from the official repository, enable authentication, add least-privilege users and connect safely.

CloudSat teamNovember 18, 20266 min read
A stack of rounded document cards resting inside a softly glowing padlock outline on a dark background.
On this pageBefore you start

MongoDB 9.0 became generally available at the end of September 2026, and its Community Edition packages are in MongoDB's own APT repository for Ubuntu 22.04, 24.04 and 26.04. A fresh install is safe from the network, because it listens on localhost only, but it starts with access control switched off: any process on the machine can read or drop any database. This guide installs the server, creates users, turns authorization on, and shows how to reach the database from elsewhere without publishing port 27017.

Before you start

  • A supported system. Ubuntu 24.04 (noble) or 26.04 (resolute), on x86_64 or ARM64, with sudo access.
  • Memory. By default, the WiredTiger storage engine uses the larger of 50% of (RAM minus 1 GB) or 256 MB as its cache. That is 512 MB of cache on a 2 GB server and 1.5 GB on a 4 GB server, before connections and the operating system take their share.
  • Disk. MongoDB strongly recommends XFS for data files with WiredTiger. Ext4 works for small deployments; if the database will be busy, put /var/lib/mongodb on an XFS volume.
  • License. MongoDB Community Server is published under the Server Side Public License (SSPL) v1. See the FAQ below.

On CloudSat, MongoDB is also a one-click app, and the MongoDB hosting page covers which plan sizes fit which workloads.

Step 1: add MongoDB's repository

Import MongoDB's signing key, then add the 9.0 repository for your Ubuntu release. The codename is read from the system, so the same commands work on 24.04 and 26.04:

sudo apt-get install -y gnupg curl
curl -fsSL https://pgp.mongodb.com/server-9.asc | \
  sudo gpg -o /usr/share/keyrings/mongodb-server-9.gpg --dearmor
CODENAME=$(. /etc/os-release && echo "$VERSION_CODENAME")
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-9.gpg ] https://repo.mongodb.org/apt/ubuntu $CODENAME/mongodb-org/9.0 multiverse" | \
  sudo tee /etc/apt/sources.list.d/mongodb-org-9.0.list

The repository line names 9.0, so routine apt upgrade runs bring 9.0 patch releases only. A new major version is a deliberate change to that line, never a surprise.

Step 2: install and start the server

sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl enable --now mongod
systemctl status mongod --no-pager

The mongodb-org package pulls in the server, the mongosh shell and the database tools such as mongodump. If systemd reports that mongod.service is not found, run sudo systemctl daemon-reload and start it again. Data lives in /var/lib/mongodb, logs in /var/log/mongodb/mongod.log, and the configuration in /etc/mongod.conf.

Step 3: create an administrator

Open the shell with mongosh, then create the first user in the admin database. passwordPrompt() asks for the password, so it never lands in your shell history:

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

Step 4: turn on authorization

Add this block to /etc/mongod.conf, keeping YAML's two-space indentation:

security:
  authorization: enabled

Restart and log in again, this time as the administrator:

sudo systemctl restart mongod
mongosh -u admin --authenticationDatabase admin

From now on, an unauthenticated mongosh session can still connect, but commands such as show dbs fail with an authentication error. That is the behavior you want.

Step 5: give each application its own user

Applications should never share the administrator account. Create one user per application, scoped to its own database:

use shop
db.createUser({
  user: "shop_app",
  pwd: passwordPrompt(),
  roles: [ { role: "readWrite", db: "shop" } ]
})

The application then connects with mongodb://shop_app:PASSWORD@127.0.0.1:27017/shop?authSource=shop. Percent-encode characters such as @, : and / if they appear in the password.

Built-in role Scope Use it for
read One database Reporting and read-only tools
readWrite One database Application accounts
dbAdmin One database Indexes and statistics, without reading data
backup and restore Whole deployment Accounts used by mongodump and mongorestore
userAdminAnyDatabase Whole deployment Managing users; it can grant itself any role, so guard it like root

Optional: a single-member replica set

Multi-document transactions and change streams only work on a replica set, and some libraries, the Prisma ORM among them, refuse to connect to MongoDB without one. A replica set with one member gives you those features on a single server. With authorization on, members must also share a key file, even when there is only one member:

openssl rand -base64 756 | sudo tee /etc/mongod.key > /dev/null
sudo chown mongodb:mongodb /etc/mongod.key
sudo chmod 400 /etc/mongod.key

Extend the security block in /etc/mongod.conf and add a replication block:

security:
  authorization: enabled
  keyFile: /etc/mongod.key
replication:
  replSetName: rs0

Restart mongod, log in as the administrator, grant it the clusterAdmin role, and initiate the set with an explicit member address. Without one, MongoDB records the machine's hostname, which clients then try to reach on an interface mongod does not listen on:

use admin
db.grantRolesToUser("admin", [ { role: "clusterAdmin", db: "admin" } ])
rs.initiate({ _id: "rs0", members: [ { _id: 0, host: "127.0.0.1:27017" } ] })

Connection strings then gain replicaSet=rs0, for example mongodb://shop_app:PASSWORD@127.0.0.1:27017/shop?authSource=shop&replicaSet=rs0.

Connect from elsewhere without opening port 27017

For yourself, an SSH tunnel is the simplest answer. Run this on your computer, then point mongosh or MongoDB Compass at mongodb://127.0.0.1:27017/?authSource=admin:

ssh -N -L 27017:127.0.0.1:27017 admin@203.0.113.10

If something else on your computer already uses port 27017, pick another local port, such as 27018:127.0.0.1:27017.

For an application server on another machine, add the database server's own address to bindIp and allow only that client through the firewall. Turn on TLS as well (net.tls.mode: requireTLS with a certificate), since the traffic now crosses a network:

net:
  port: 27017
  bindIp: 127.0.0.1,203.0.113.10
sudo ufw allow from 198.51.100.40 to any port 27017 proto tcp

Day-two basics

  • Back up with a dedicated account. Create a user with the backup role, store its password in a file readable only by root, and point mongodump at it: mongodump --username backup --authenticationDatabase admin --config=/root/.mongodump.yaml --gzip --archive=/var/backups/mongo-$(date +%F).gz. The YAML file holds one line, password: .... Copy the archive off the server, and restore it into a test instance now and then with mongorestore --gzip --archive=FILE.
  • Keep swapping rare. MongoDB recommends a low vm.swappiness: echo 'vm.swappiness = 1' | sudo tee /etc/sysctl.d/90-mongodb.conf && sudo sysctl --system.
  • Ignore old advice on huge pages. Guides written for MongoDB 7.0 and earlier tell you to disable Transparent Huge Pages. From 8.0 on, MongoDB's production notes recommend enabling them instead.
  • Watch the log. Startup warnings in /var/log/mongodb/mongod.log point at most configuration problems before they hurt.
  • Know your exact version. mongod --version prints the patch level. Check it against the release notes whenever a 9.0.x update lands, and apply security fixes within days, not months.

FAQ

Why can another server not reach my MongoDB?

The default bindIp is 127.0.0.1, so mongod only accepts local connections. Change it only together with authentication, a firewall rule for the specific client and TLS, or use an SSH tunnel instead.

Can I use MongoDB Community for a commercial product?

Running it as the database behind your own application is the common case and costs nothing. The SSPL's additional conditions are aimed at offering MongoDB itself as a service to third parties. If your plans come anywhere near that, have someone qualified read the license for your situation.

How do I upgrade from MongoDB 8.0?

To reach 9.0, a deployment must run 8.0 or 8.3 with its feature compatibility version set to match. Older deployments step through 8.0 first. Switch the repository line to 9.0, upgrade the packages, restart, and after a burn-in period run db.adminCommand({ setFeatureCompatibilityVersion: "9.0", confirm: true }).

Not in the server package. MongoDB Search for Community runs as a separate component that you install with Docker or as Linux packages, following MongoDB's self-managed Search documentation.

Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.