MySQL & MariaDB error 1142: Command denied to user for table
SELECT command denied to user 'appuser'@'127.0.0.1' for table 'salaries' (5.6.51) · SELECT command denied to user 'appuser'@'127.0.0.1' for table `t_0`.`salaries` (10.6.28) The account has no privilege for this statement on this table (for example SELECT, INSERT, DROP). The message names the command, the account and the table.
| MySQL | MariaDB | |
|---|---|---|
| Symbol | ER_TABLEACCESS_DENIED_ERROR | ER_TABLEACCESS_DENIED_ERROR |
| SQLSTATE | 42000 | 42000 |
| Type | Server error | Server error |
| Versions | every release since 5.0 active | every release since 5.5 active |
What it means
SELECT command denied to user 'appuser'@'10.0.0.5' for table 'salaries'. Privileges are checked per account (user plus host) at global, database, table and column level. This account has none that allow this command on this table.
Why it happens
- The account was granted privileges on specific tables, and the query touches another one, including through a view, a JOIN or a stored routine.
- A grant was made for a different host than the one the application connects from (
'app'@'localhost'vs'app'@'%'); checkSELECT CURRENT_USER();. - Missing privileges for administrative commands:
DROP,ALTER,TRIGGER,REFERENCES(for foreign keys),LOCK TABLES(mysqldump),SHOW VIEW. - Roles (MySQL 8.0, MariaDB 10.0.5+) that are granted but not active:
SET DEFAULT ROLEwas never run.
How to fix it
- Check what the account has:
SHOW GRANTS FOR 'appuser'@'%'; - Grant what is needed, as narrowly as possible:
GRANT SELECT ON shop.salaries TO 'appuser'@'%';or a database-wideGRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO …. - For roles:
SET DEFAULT ROLE ALL TO 'appuser'@'%';
Reproduce it yourself
These steps are the exact test our harness runs. Use a scratch server, never production.
Connect with
mysql -u root -pand create a scratch database and the test data:CREATE DATABASE demo; USE demo; CREATE TABLE salaries (id INT, amount INT); CREATE TABLE public_info (id INT);Run:
CREATE USER 'appuser'@'%' IDENTIFIED BY 'Test#1234'; GRANT SELECT ON demo.public_info TO 'appuser'@'%';In a shell on the database host:
mysql -uappuser -p'Test#1234' -h127.0.0.1 demo -e 'SELECT * FROM salaries'
You get: ERROR 1142 (42000) at line 1: SELECT command denied to user 'appuser'@'127.0.0.1' for table `t_0`.`salaries`(11.8.9)
Verified fixes
Grant the privilege on that table (or the whole database)
CREATE USER 'appuser'@'%' IDENTIFIED BY 'Test#1234'; GRANT SELECT ON demo.salaries TO 'appuser'@'%';mysql -uappuser -p'Test#1234' -h127.0.0.1 demo -N -e 'SELECT COUNT(*) FROM salaries'
Tested on
| Step | MySQL | MariaDB | |||||||
|---|---|---|---|---|---|---|---|---|---|
| 5.6 | 5.7 | 8.0 | 8.4 | 9 | 10.6 | 10.11 | 11.4 | 11.8 | |
| A user with SELECT on one table reads another repro | ✓ 1142 | ✓ 1142 | ✓ 1142 | ✓ 1142 | ✓ 1142 | ✓ 1142 | ✓ 1142 | ✓ 1142 | ✓ 1142 |
| Grant the privilege on that table (or the whole database) fix | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Related errors
How the message changed between versions
| MariaDB from | Symbol | Message |
|---|---|---|
| 5.5.68 | ER_TABLEACCESS_DENIED_ERROR | %-.16s command denied to user '%s'@'%s' for table '%-.192s' |
| 10.2.44 | ER_TABLEACCESS_DENIED_ERROR | %-.100T command denied to user '%s'@'%s' for table '%-.192s' |
| 10.3.39 | ER_TABLEACCESS_DENIED_ERROR | %-.100T command denied to user '%s'@'%s' for table %`s.%`s |
| 11.8.10 | ER_TABLEACCESS_DENIED_ERROR | %-.100sT command denied to user '%s'@'%s' for table %sQ.%sQ |