Database

Secure MySQL Accounts and Least-Privilege Access

Separate runtime, migration, backup, and administration credentials and grant only the privileges each role needs.

Overview

Database security starts with limiting what a compromised application credential can do. A web application that only needs to read and write a defined schema should not connect as a database administrator or retain privileges to create users and grant access.

Start with the workload and the invariant

Create separate accounts for runtime access, schema migrations, backups, and human administration. Restrict each account by host where practical. Grant only required operations on the intended database and avoid global privileges. Review grants periodically, especially after deployments and staff changes.

Practical example

CREATE USER 'app_runtime'@'localhost'
IDENTIFIED BY 'use-a-secret-manager-generated-value';

GRANT SELECT, INSERT, UPDATE, DELETE
ON app_database.*
TO 'app_runtime'@'localhost';

Do not use the illustrative password literally. Store credentials outside the web root with restrictive filesystem permissions or in a supported secret store. Do not commit secrets to source control, include them in error pages, or print them in diagnostic logs. Rotate credentials through a planned process that avoids downtime.

Trade-offs and failure modes

Use TLS for remote database connections when the network path is not trusted, and validate certificates rather than merely enabling encryption. Restrict network exposure with firewall rules and bind the database service only to interfaces that need it.

Verification and rollout

Audit privileged accounts, anonymous accounts, remote root access, and unused grants. Remove old credentials after confirming that no scheduled job or application instance still depends on them. Backups and monitoring agents also need carefully scoped access.

Engineering checklist

Test the application using the actual runtime account in staging. This catches hidden dependencies on excessive privileges before production. Least privilege is effective when permissions are both minimal and verified against real application behavior.

Before shipping, review the database version, storage engine, SQL mode, character set, collation, and deployment process used by the target environment. Features and DDL behavior can differ between MySQL and MariaDB releases. Validate the exact statements on a representative staging database, capture a baseline, and keep a tested recovery path for changes that affect production data.

Share this page Help other developers discover this resource.