Tablux

Tablux › Guides › MySQL read-only user

Guide

How to create a read-only user in MySQL

Create the user, then grant it SELECT on one database: CREATE USER 'reporting'@'%' IDENTIFIED BY '…'; GRANT SELECT, SHOW VIEW ON appdb.* TO 'reporting'@'%'; The grant covers every table and view in appdb, including ones created later. No FLUSH PRIVILEGES is needed. Check the result with SHOW GRANTS FOR 'reporting'@'%';.

Create the user and grant SELECT

Connect as an admin (root, or your RDS master user) and run:

CREATE USER 'reporting'@'%' IDENTIFIED BY 'use-a-long-random-password';
GRANT SELECT, SHOW VIEW ON appdb.* TO 'reporting'@'%';

SELECT ON appdb.* covers every table and view in appdb, including tables created later; unlike PostgreSQL there are no default privileges to manage. SHOW VIEW lets the user run SHOW CREATE VIEW and EXPLAIN queries that use views, which GUI clients and mysqldump rely on. Add SHOW_ROUTINE (MySQL 8.0.20 and later, granted globally with ON *.*) only if the user needs to read stored procedure definitions.

Avoid GRANT SELECT ON *.*. It includes the mysql system schema, so the "read-only" user could read mysql.user, password hashes included. Grant per database instead, one line each.

Underscores are a trap in database names: in a database-level grant, _ and % are wildcards, so app_db.* also matches a database called appxdb. Escape them (GRANT SELECT ON `app\_db`.* …). MySQL 8.2 deprecated this wildcard behaviour, but it still applies.

Pick the host part

A MySQL account is a user name and a host: 'reporting'@'%' and 'reporting'@'localhost' are two different accounts with separate passwords and grants.

Host partMatches
'%'Any host (also what you get if you leave the host out)
'localhost'Connections from the server itself
'10.0.4.17'That one address, such as a BI server or bastion
'10.0.0.0/255.255.0.0'A subnet, in netmask form (MySQL 8.4 also accepts CIDR such as 10.0.0.0/16)

Use the narrowest host you can. A firewall or security group should still limit who can reach port 3306; the host part is a second check, not the only one.

Use a role for several read-only users (MySQL 8.0+)

With more than one reader, grant privileges to a role and the role to the users:

CREATE ROLE 'app_read';
GRANT SELECT, SHOW VIEW ON appdb.* TO 'app_read';

CREATE USER 'analyst'@'10.0.4.17' IDENTIFIED BY 'another-long-password';
GRANT 'app_read' TO 'analyst'@'10.0.4.17';
SET DEFAULT ROLE ALL TO 'analyst'@'10.0.4.17';

Do not skip SET DEFAULT ROLE. MySQL does not activate granted roles at login unless they are default roles (or activate_all_roles_on_login is on), so without it the user connects with no privileges and queries fail with errors such as SELECT command denied. SELECT CURRENT_ROLE(); shows what is active in a session.

FLUSH PRIVILEGES is not needed

Many tutorials end with FLUSH PRIVILEGES. It only matters if you edit the grant tables directly with INSERT or UPDATE. CREATE USER, GRANT and REVOKE reload the privileges in memory immediately. MySQL 9.2 deprecated FLUSH PRIVILEGES and now warns when you run it.

"Immediately" has one nuance for sessions that are already connected: table-level changes apply on the client's next statement, database-level changes on its next USE, and global privileges only for new connections.

Check the grants

SHOW GRANTS FOR 'reporting'@'%';
-- +----------------------------------------------------------+
-- | GRANT USAGE ON *.* TO `reporting`@`%`                    |
-- | GRANT SELECT, SHOW VIEW ON `appdb`.* TO `reporting`@`%`  |
-- +----------------------------------------------------------+

SHOW GRANTS FOR 'analyst'@'10.0.4.17' USING 'app_read';   -- expands the role

USAGE means "no privileges" at that level, so the first line is expected. Then log in as the user and try a write; you should get INSERT command denied to user.

Extra layers: read-only sessions, timeouts, limits

A client can make its own session read-only:

SET SESSION TRANSACTION READ ONLY;

Writes to permanent tables then fail even if the grants allow them (DML on temporary tables still works). It is a safety net, not a boundary: any session may switch back with SET SESSION TRANSACTION READ WRITE. The grants are what stop writes.

Cap long reports and connection counts too:

ALTER USER 'reporting'@'%' WITH MAX_USER_CONNECTIONS 5;

SET SESSION max_execution_time = 30000;   -- MySQL: milliseconds, SELECT only
SET SESSION max_statement_time = 30;      -- MariaDB: seconds

MySQL cannot attach the timeout to a user, so set it in the client session or globally. For heavy reporting, point the user at a replica; a replica running with super_read_only refuses writes from everyone.

MariaDB differences

CREATE USER, GRANT SELECT, SHOW VIEW and SHOW GRANTS work the same. Roles differ: a user has one default role, set with FOR rather than TO, and there is no ALL:

CREATE ROLE app_read;
GRANT SELECT, SHOW VIEW ON appdb.* TO app_read;
GRANT app_read TO 'analyst'@'10.0.4.17';
SET DEFAULT ROLE app_read FOR 'analyst'@'10.0.4.17';

The statement timeout is max_statement_time, in seconds, and MariaDB can set it per user: ALTER USER 'reporting'@'%' WITH MAX_STATEMENT_TIME 30;. MariaDB has not deprecated FLUSH PRIVILEGES (it is still unnecessary after GRANT).

Managed services: RDS, Aurora and PlanetScale

Amazon RDS and Aurora MySQL. Connect as the master user and run the same statements. The master user has no SUPER privilege, but it can create users and grant SELECT.

PlanetScale (Vitess, MySQL-compatible). You do not manage MySQL users yourself; you create a password for a branch with a role. Choose Read-only in the dashboard, or use the CLI:

pscale password create appdb main reporting --role reader

The role of a PlanetScale password cannot be changed later; create a new password instead.

In a database client

Marking a MySQL connection read-only in Tablux makes the app refuse writes and also run the session in read-only transaction mode on the server, as above. Use it together with a read-only user, not instead of one.

Questions

Do I need FLUSH PRIVILEGES after GRANT?

No. GRANT, REVOKE and CREATE USER take effect without it. FLUSH PRIVILEGES is only for direct edits to the grant tables, and MySQL 9.2 deprecated it.

Why does my read-only user get SELECT command denied after I granted a role?

Roles are not active at login by default. Run SET DEFAULT ROLE ALL TO the user (MySQL) or SET DEFAULT ROLE role FOR the user (MariaDB), then reconnect.

Should I grant SELECT ON *.* for a read-only user?

Usually not. It includes the mysql system schema, where account details and password hashes live. Grant SELECT on each application database instead.

Does a read-only user see tables created later?

Yes, if you granted at the database level (ON appdb.*). Table-level grants (ON appdb.orders) cover only the tables you name.

What is the difference between user@'%' and user@'localhost'?

They are separate accounts. '%' matches any host; 'localhost' matches only connections from the server itself. Each has its own password and grants.

Updated 2026-11-25