Home›Operate›Security
Course · ArchitectureLesson 29 of 330 read

Security: who can reach the data

Who can connect, what can they do, and what happens to a copy?

Most database breaches are not clever. A port open to the internet, an application that logs in as the superuser, a production copy on a laptop. This page covers the five layers I check, the commands that show where you stand in each engine, and what breaks when one is missing.

The 5-minute version

If you read nothing else on this page

  1. Keep it off the internet. Private network only, reached through the application or a bastion. No exception for "just for testing".
  2. Every connection is encrypted and authenticated. TLS required, strong password hashing (SCRAM, caching_sha2), no shared logins.
  3. The application is not an administrator. One role owns the schema and runs migrations; the application role can only read and write rows.
  4. Copies are part of the attack surface. Backups, replicas and dev copies need the same care, and dev copies should not hold real personal data.
  5. Write down who did what. Connection logging and an audit log for privileged actions, kept somewhere the database admin cannot quietly edit.

The rest of the page goes deeper, one section per item.

Layer 1 · network

Not reachable from where it should not be

A database listening on a public address with weak or no authentication is found by scanners within hours. Public MongoDB and Elasticsearch servers were emptied and held for ransom by the tens of thousands in the 2017 waves; the same thing still happens to every engine.

Layer 2 · connections

Encrypted, and with a real identity

PostgreSQL

  • ssl = on, and hostssl lines in pg_hba.conf so plain connections are refused.
  • Passwords hashed with scram-sha-256. MD5 is deprecated in 18 and warns when set.
  • No trust lines except the local socket for the postgres OS user, if that.

MySQL

  • require_secure_transport = ON.
  • caching_sha2_password is the default; mysql_native_password is disabled by default in 8.4.
  • No accounts with host '%' and broad grants; no anonymous users.

MongoDB

  • security.authorization: enabled; without it anyone who connects is an administrator.
  • net.tls.mode: requireTLS.
  • SCRAM-SHA-256 for users; x.509 or LDAP/Kerberos in Enterprise.

On a managed service, IAM authentication (RDS, Aurora, Cloud SQL) replaces long-lived passwords with tokens, which removes the password rotation problem for the application.

Layer 3 · privileges

The application can change rows, not the schema

If the application connects as an administrator, any SQL injection or bug can drop a table, read every schema or create a new admin. I split it into at least three roles:

RoleCanUsed by
OwnerCreate, alter and drop tables; owns the objectsMigrations, run by the deploy pipeline
ApplicationSELECT, INSERT, UPDATE, DELETE on its tables; nothing elseThe service, through its pool
Read-onlySELECTReports, analysts, AI agents, support tools
-- PostgreSQL
CREATE ROLE app_owner NOLOGIN;
CREATE ROLE app LOGIN PASSWORD '...';
GRANT USAGE ON SCHEMA shop TO app;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA shop TO app;
ALTER DEFAULT PRIVILEGES FOR ROLE app_owner IN SCHEMA shop
  GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO app;
-- read-only everything (14 and later): GRANT pg_read_all_data TO reporting;

-- MySQL (roles since 8.0)
CREATE ROLE app_rw;
GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO app_rw;
CREATE USER 'app'@'10.0.%' IDENTIFIED BY '...' DEFAULT ROLE app_rw;

// MongoDB
db.getSiblingDB("admin").createUser({ user: "app", pwd: passwordPrompt(),
  roles: [{ role: "readWrite", db: "shop" }] })

For tenants or rows a user must never see, row-level security (PostgreSQL), views with a definer, or a filter enforced in one data access layer. For columns, column privileges or a view without them. The agents review applies the same idea to AI agents.

Layer 4 · copies

Backups, replicas and dev copies are the data too

Layer 5 · audit

Know who did what, afterwards

PostgreSQL

  • log_connections and log_disconnections.
  • The pgaudit extension for DDL, role changes and reads of sensitive tables.

MySQL

  • The audit log plugin (MySQL Enterprise, or Percona's audit log for Percona Server).
  • The general log only for short investigations: it is heavy.

MongoDB

  • Auditing in Enterprise and Atlas.
  • Community: connection logs and the profiler give partial coverage.

Ship the logs somewhere the database administrators cannot edit. An audit log the attacker can delete is a diary.

Check it yourself

Where do I stand right now?

-- PostgreSQL: who is a superuser, which hba rules exist, who is on TLS
SELECT rolname FROM pg_roles WHERE rolsuper OR rolcreaterole;
SELECT type, database, user_name, address, auth_method FROM pg_hba_file_rules;
SELECT a.usename, s.ssl, s.version FROM pg_stat_ssl s JOIN pg_stat_activity a USING (pid);

-- MySQL: accounts open to any host, their plugins, and the TLS rule
SELECT user, host, plugin FROM mysql.user ORDER BY host = '%' DESC, user;
SHOW GRANTS FOR 'app'@'10.0.%';
SHOW VARIABLES LIKE 'require_secure_transport';

// MongoDB: is authorization on, and who exists
db.adminCommand({ getCmdLineOpts: 1 }).parsed.security
db.getSiblingDB("admin").getUsers()

The config checker flags several of these from a pasted configuration.

What breaks

The incidents I would plan against

What happensWhyWhat I do
Data deleted and a ransom note leftPort open to the internet, auth off or a weak passwordPrivate network, auth on, an alert on any connection from outside the expected subnets.
A bug or injection drops a tableThe application connects as the owner or a superuserSeparate owner and application roles; the application cannot run DDL.
Personal data found in a test environmentA raw production copy for "realistic testing"Masked or generated data only; the copy job masks before anything leaves production.
Outage at 3 AM: certificates expiredTLS turned on, renewal forgottenExpiry monitoring, and renewal tested like a restore.
Outage after a password rotationOne shared password changed everywhere at onceTwo application users and alternate between them, or IAM tokens.
Your turn

An analyst needs to query production for a weekly report, and a new AI agent needs read access to answer support questions. What do you give each of them?

Write your answer down first. Then open mine.

Show my answer

Neither gets the application's login. Each gets its own role, so their queries show up under their name and can be limited or revoked on their own.

Analyst: read-only, on a replica rather than the primary, with a statement timeout so one heavy query cannot hurt production. Columns with personal data either hidden behind a view or masked.

Agent: read-only on the few tables it needs, never on secrets or payment data, with a row limit and a short timeout, on a replica. Every query it sends logged, because it writes its own SQL.

Connected

Where this shows up

The same mechanism, backups and restore, elsewhere on the site: in real incidents, in measurements, and in designs.

Check yourself · 4 questions

Check yourself

Pick an answer. You will see whether it is right, and why.

1. The application role can run ALTER TABLE. Why is that a problem?

2. MongoDB is started without security.authorization. What can an anonymous client do?

3. Which protects a stolen backup file?

4. What is wrong with copying production into a test environment for realistic data?

Part 4 of 4 · Operate · 15 pagesAll pages

How do I run it in production? What to check when it is slow or broken, where it runs and what it costs, how big it must be, pooling, backups, security, upgrades and other changes made safely.