MongoDB with auth bolted on as an afterthought — fixing the four classic mistakes

Four checks and fixes for the misconfigurations a fresh mongod ships with: no authentication on by default, the bind-all-interfaces footgun, over-privileged exporter users, and an unprotected mongodb_exporter `:9216/metrics` endpoint.

Published 2026-06-039 min readMongoDBSecurity 101Tested on MongoDB 7.0
On this page
  1. Why this post exists
  2. Is authentication actually on?
  3. Is mongod listening on a public interface?
  4. Does the exporter run as root?
  5. Is the exporter's :9216 metrics port exposed?

Why this post exists

MongoDB's defaults privilege getting-started speed over security: a fresh mongod binds to localhost-only and runs with `--noauth` until you opt in. The moment a Helm chart, a Docker compose file, or a hosting tutorial flips `--bind_ip_all` without also enabling `--auth`, you have an unauthenticated database reachable on the network. Our agent watches the operational state (replica set health, oplog, page faults, locks) but cannot see the auth posture. This post covers what only you can fix.

Every command below was run on a fresh `mongo:7.0` container on 2026-06-03 (full traces in [the spec](https://github.com/elk-utilities/homelab-apps/blob/main/.ai/specs/mongodb-exporter.md)). The single most important fix — turning on authentication — has a one-liner that converts a development cluster into a production-shaped one without rebuilding anything.

Is authentication actually on?

A staggering number of MongoDB clusters in production run with no authentication at all because the team flipped `--bind_ip_all` for "remote access" and never set `--auth`. The check is trivial; the fix is one config line plus an admin-user bootstrap.

Check

Check: is the security.authorization flag set?

Run from a host that can reach mongod. If the command returns `{ "authenticated": false }` AND any operation succeeds without credentials, you are running with `--noauth`.

bash✓ MongoDB 7.0.5
mongosh --quiet --eval 'JSON.stringify(db.runCommand({ connectionStatus: 1 }))'
mongosh --quiet --eval 'JSON.stringify(db.adminCommand({ getParameter: 1, authenticationMechanisms: 1 }))'

What to look for: On a properly secured cluster the `connectionStatus` JSON has `authInfo.authenticatedUsers` non-empty and the second command lists `SCRAM-SHA-256`. If both commands return `{ ok: 1 }` with no credentials and no authenticated user, you are unauthenticated.

Fix

Fix: enable authorization and create a root user

On MongoDB the order matters: create the bootstrap admin user FIRST (while still running with --noauth), then flip `--auth` on and restart. After the flip, every connection must authenticate.

bash✓ MongoDB 7.0.5 (mongod + mongosh)

Verify it worked: mongosh --quiet --eval 'db.serverStatus().ok' || echo 'expected: command failed unauthorized' mongosh "mongodb://root@MONGO_HOST:27017/admin" --quiet --eval 'db.serverStatus().ok'

Note: Don't skip step 1. If you flip `--auth` on without a bootstrap admin you cannot log in and have to either restart with `--auth` off again (defeats the purpose) or use the `localhost exception` — both make the rollout fragile.

Is mongod listening on a public interface?

`--bind_ip_all` is a common compose / Helm chart default for convenience, but combined with public network reachability it is the second-largest source of leaked MongoDB clusters in shodan-style scans.

Check

Check: which interfaces is mongod bound to?

The `net.bindIp` config + `--bind_ip*` runtime flag determines who can reach mongod. If you see `0.0.0.0` or `::` in the listening sockets, the only thing keeping the world out of your DB is the firewall / NetworkPolicy upstream.

bash✓ MongoDB 7.0 on Ubuntu 24.04
# On the mongod host:
sudo ss -ltnp | grep :27017
mongosh "mongodb://[email protected]:27017/admin" --quiet \
  --eval 'JSON.stringify(db.serverCmdLineOpts().parsed.net)'

What to look for: If `ss` prints `0.0.0.0:27017` or `:::27017`, mongod is reachable on every interface the host has. The `bindIp` JSON should reflect a private list — never `0.0.0.0`.

Fix

Fix: bind to the internal network and a Kubernetes NetworkPolicy

On a single-host / VM deployment, bind to `127.0.0.1` plus the internal RFC1918 interface (never `0.0.0.0`). On Kubernetes, put mongod on a `ClusterIP`-only Service and add a NetworkPolicy that allows ONLY the application namespace and the monitor-agent (via the exporter sidecar) to reach port 27017.

YAML✓ Kubernetes NetworkPolicy v1, Calico/Cilium-compatible.

Verify it worked: sudo ss -ltnp | grep :27017 # must NOT show 0.0.0.0:27017 kubectl -n mongo describe networkpolicy mongo-from-app-only

Note: Avoid `--bind_ip_all` in production. If you must bind to all interfaces (e.g. for replica set elections across hosts without DNS), restrict at the firewall layer and run the Auth check above religiously.

Does the exporter run as root?

A common mistake when wiring up Percona's `mongodb_exporter`: hand it the `root` credential because "it just works". That credential then sits in the exporter's env, exposed to anyone who can read the container or pull the systemd unit. The fix is the `clusterMonitor` role — built-in, narrowest scope possible for an exporter.

Check

Check: who has root / dbOwner / userAdminAnyDatabase?

Run as an admin. Any user in this list with the `mongodb_exporter` name or similar is the smell — exporters should never have write privileges.

bash✓ MongoDB 7.0

What to look for: Any returned user MUST be a human admin or a known operator account, never an exporter / pipeline service account.

Fix

Fix: replace the exporter user with a clusterMonitor-scoped one

Identical to the [/exporters/mongodb/scoped-user](/exporters/mongodb/scoped-user) page — repeated here so the post is self-contained. Drop the old root-equivalent user as the last step.

JSON✓ MongoDB 7.0

Verify it worked: mongosh "mongodb://prometheus_exporter@MONGO_HOST:27017/?authSource=admin" --quiet --eval 'db.adminCommand({ serverStatus: 1 }).ok' # must print 1 mongosh "mongodb://prometheus_exporter@MONGO_HOST:27017/test?authSource=admin" --quiet --eval 'try { db.t.insertOne({x:1}); print("BAD: writes allowed") } catch (e) { print("OK: writes denied") }'

Is the exporter's :9216 metrics port exposed?

Even with a perfectly-scoped MongoDB user, the `mongodb_exporter` process holds the password in its env. Its `/metrics` endpoint itself does NOT require auth — exposing port 9216 to the public internet means anyone can probe whether the exporter is even running. Worse, the exporter logs can leak the connection string on failures. Treat `:9216` the same as `:27017`.

Check

Check: can :9216 be reached from outside?

From a host outside your trust boundary, probe the port. If you get the Prometheus exposition format back, the port is too open.

bash✓ percona/mongodb_exporter:0.43.1
# From outside your VPC / cluster:
curl --max-time 5 http://MONGO_EXPORTER_HOST:9216/metrics | head -5
# Expected: connection timed out, or 404 if a reverse proxy intercepts.

What to look for: If you see lines starting with `# HELP mongodb_*` or `# TYPE mongodb_*`, the exporter's metrics port is reachable from outside your trust boundary — fix immediately.

Fix

Fix: NetworkPolicy lets only the monitor-agent reach :9216

Mirror of the ClickHouse pattern: tag the exporter pod, allow only the `monitor-agent` namespace, deny everything else.

YAML✓ Kubernetes NetworkPolicy v1, Calico/Cilium-compatible.

Verify it worked: # From a host outside the cluster: curl --max-time 5 http://MONGO_EXPORTER_HOST:9216/metrics # must timeout / connection refused # From inside the monitor-agent namespace: kubectl -n monitor-agent run probe --rm -it --image=curlimages/curl --restart=Never -- \ curl -sS http://mongodb-exporter.<mongo-ns>.svc.cluster.local:9216/metrics | head

Note: This is the same NetworkPolicy our [/exporters/mongodb/scoped-user](/exporters/mongodb/scoped-user) page recommends — repeated here so the post is self-contained.