How-to

Create MySQL Users and Grant Privileges

MySQL 8 changed how accounts authenticate and added roles and lockout rules. Use them, and every application and person gets exactly the access it needs.

Which MySQL versions does this cover?

MySQL 8.0, shipped by Ubuntu 24.04 and the Enterprise Linux AppStream, and the 8.4 long-term release from the vendor's own repositories. Both use caching_sha2_password as the default authentication plugin. In 8.4 the older mysql_native_password plugin is disabled by default, and MySQL 9 removes it, so new accounts should use the default. For MariaDB, whose syntax differs in places, see MariaDB users and grants.

How do I create an account?

On Ubuntu, log in with sudo mysql; the root account uses socket authentication there. Then:

CREATE USER 'billing'@'localhost' IDENTIFIED BY 'k2V-generated-secret-88';

Generate the secret with openssl rand -base64 24 and store it in the app's config file with mode 600. If an old client library cannot speak caching_sha2, upgrade the library rather than downgrading the account; every maintained connector supports it.

How narrow should grants be?

As narrow as the code allows. An application normally needs data statements on its own database, plus schema changes if it runs its own migrations:

GRANT SELECT, INSERT, UPDATE, DELETE ON billing.* TO 'billing'@'localhost';
GRANT CREATE, ALTER, INDEX, DROP, REFERENCES ON billing.* TO 'billing_migrate'@'localhost';

Splitting runtime and migration accounts means a SQL injection bug in the running app cannot drop tables. Deploy scripts use the migration account; the app uses the runtime one.

How do roles work in MySQL 8?

CREATE ROLE 'app_read', 'app_write';
GRANT SELECT ON billing.* TO 'app_read';
GRANT INSERT, UPDATE, DELETE ON billing.* TO 'app_write';
GRANT 'app_read', 'app_write' TO 'billing'@'localhost';
SET DEFAULT ROLE ALL TO 'billing'@'localhost';

Without a default role, granted roles are inactive until the session runs SET ROLE, a common source of confusion. Alternatively, set activate_all_roles_on_login=ON in the server configuration.

Which safety options are worth adding?

OptionEffect
FAILED_LOGIN_ATTEMPTS 5 PASSWORD_LOCK_TIME 1Lock the account for a day after five bad passwords
PASSWORD EXPIRE INTERVAL 180 DAYForce rotation for human accounts
WITH MAX_USER_CONNECTIONS 50Cap concurrent sessions
REQUIRE SSLRefuse unencrypted connections
ACCOUNT LOCKDisable without deleting
ALTER USER 'billing'@'localhost' FAILED_LOGIN_ATTEMPTS 5 PASSWORD_LOCK_TIME 1 WITH MAX_USER_CONNECTIONS 50;

Do not set password expiry on application accounts; an expired app password takes the site down at a random moment.

What are dynamic privileges?

MySQL 8 split the old all-powerful SUPER privilege into named pieces such as BACKUP_ADMIN, REPLICATION_SLAVE_ADMIN, SYSTEM_VARIABLES_ADMIN and CONNECTION_ADMIN. Grant the specific one a tool needs:

CREATE USER 'xtrabk'@'localhost' IDENTIFIED BY 'another-generated-secret';
GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT, BACKUP_ADMIN ON *.* TO 'xtrabk'@'localhost';
GRANT SELECT ON performance_schema.log_status TO 'xtrabk'@'localhost';

That set matches what Percona XtraBackup asks for on MySQL 8. Check your backup tool's documentation for its exact list.

How do remote accounts work between our machines?

Machines communicate over public addresses, since private networks are not offered. Use the partner's exact address as the host part, require TLS and open port 3306 only to that address:

CREATE USER 'report'@'198.51.100.12' IDENTIFIED BY 'remote-secret' REQUIRE SSL;
GRANT SELECT ON billing.* TO 'report'@'198.51.100.12';

Set bind-address in /etc/mysql/mysql.conf.d/mysqld.cnf on Ubuntu, or /etc/my.cnf.d/ on Enterprise Linux, so the server listens on the public address, then allow the partner in the firewall. MySQL 8 generates TLS certificates on first start, so encrypted connections work out of the box.

Does the account behave as intended?

SHOW GRANTS FOR 'billing'@'localhost' USING 'app_read', 'app_write';
SELECT user, host, plugin, account_locked FROM mysql.user ORDER BY user;

Then log in as the account from the shell, mysql -u billing -p billing, and try something it should not be allowed to do. A refused DROP TABLE is a good sign. Removing accounts later is covered in Remove a MySQL user.

Keep Reading

Remove a MySQL user

Drop MySQL or MariaDB accounts safely: find every host variant, check live sessions and stored-object definers, lock first, then drop.

Open

Fix "Access denied for user root@localhost"

Why MySQL or MariaDB refuses root on a fresh server, the socket authentication behind it, and safe ways to log in or reset the password.

Open

MySQL servers

Give InnoDB a buffer pool that holds your working set, NVMe for the redo log and no hypervisor between query and disk.

Open

MariaDB users and grants

Create MariaDB accounts with the least privilege they need, choose host patterns, add roles, require TLS and check grants.

Open

FAQ

Quick Answers

The default caching_sha2_password. mysql_native_password is disabled by default in 8.4 and removed in MySQL 9.

Roles need SET DEFAULT ROLE or activate_all_roles_on_login; otherwise the session must run SET ROLE.

Use ALTER USER with FAILED_LOGIN_ATTEMPTS and PASSWORD_LOCK_TIME, available since MySQL 8.0.19.

Dynamic privileges such as BACKUP_ADMIN and SYSTEM_VARIABLES_ADMIN, granted one by one.

Create the account with that server's address as host, require SSL, set bind-address and open port 3306 only to that address.