dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Home/Error Library/mysql-1142-select-command-denied
MySQL
MySQL 5.7–8.4 LTSSecurity & Privileges
7 min read
ERROR 1142 (42000): SELECT command denied to user for table / ERROR 1143 for column

MySQL Error 1142 — Granted Permissions, Why Still Access Denied?

Resolving Error 1142/1143: USER() vs CURRENT_USER() discrepancy, host-specific account precedence, table vs column grants, and activating MySQL 8 default roles.

Reviewed by:Principal Database Reliability Architect
Tested Versions: MySQL 8.4 LTS, 8.0.40 & 5.7.44 (Debian / Docker 29.7)

Operational Verification & Reliability Metadata

Last Technical Review: 30 Ekim 2026
Tested VersionsMySQL 8.4 LTS & 8.0.40 (Docker 29.7) | Tarihsel: 5.7.44
Required PrivilegesGRANT OPTION or admin privileges (to grant SELECT and configure roles)
Risk Assessment
MediumUnnecessary FLUSH PRIVILEGES or overly broad 'app'@'%' grants expand security exposure
Expected OutputVerification of USER() vs CURRENT_USER() mapping, activating roles via SET DEFAULT ROLE ALL with 0 errors
Rollback Procedure:REVOKE excessive privileges and enforce role-based access limited to specific subnet hosts
MySQL 8.4 Docs: Privileges & Role Activation
First 60 Seconds Emergency Protocol:

The error string is a symptom, not the root cause. Do not restart the server blindly. Run the diagnostic queries below to pinpoint the actual failure mode.

▸Symptom

plaintext
ERROR 1142 (42000): SELECT command denied to user 'app'@'10.0.2.15' for table 'siparis' ERROR 1143 (42000): SELECT command denied to user 'app'@'10.0.2.15' for column 'tutar' in table 'siparis'

SQLSTATE code: 42000.

This guide represents the MySQL counterpart to PostgreSQL's H-34 (permission denied for table) and SQL Server's H-39 (Msg 229: SELECT permission was denied), completing the cross-engine Object Authorization Triad.

Distinguish between the two error codes:

  • •1142: Access denied at the table or view level.
  • •1143: Access denied at the granular column level. If querying a sensitive column without explicit column-level rights, MySQL emits 1143.
IMPORTANT
MySQL Account Identity Architecture: The `user@host` Tuple In MySQL, an identity is not simply app, but strictly the `'user'@'host'` pair. 'app'@'localhost' and 'app'@'10.0.2.%' represent two distinct accounts possessing independent privilege grants. The vast majority of "I granted permissions but still got denied" inquiries stem from granting permissions to a host definition that does not match the client's actual matched account.

▸Diagnostic Step: `USER()` vs `CURRENT_USER()`

sql
SELECT USER() AS client_requested, CURRENT_USER() AS server_matched;
  • •`USER()`: The identity string and source IP submitted by the client connection.
  • •`CURRENT_USER()`: The specific privilege table record that MySQL's ACL resolver matched against.

If they differ, you are granting permissions to the wrong account! If CURRENT_USER() resolves to app@%, but you granted permissions on 'app'@'10.0.2.15', MySQL continues using the restricted % ruleset.

Inspecting Effective Grants

sql
SHOW GRANTS FOR CURRENT_USER(); SHOW GRANTS FOR 'app'@'10.0.2.%';

▸The MySQL 8 Role Activation Trap

In MySQL 8.0, roles were introduced:

sql
GRANT r_reader TO 'app'@'10.0.2.%';

This assigns the role, but does not activate it by default! When the user connects:

sql
SELECT CURRENT_ROLE(); -- Returns NONE!

Even though SHOW GRANTS displays the role membership, executing a SELECT still fails with Error 1142 until the role is activated.


▸Step-by-Step Resolution

1. Grant Least-Privilege Access to the Matched Account

sql
GRANT SELECT ON app.orders TO 'app'@'10.0.2.%';

2. Stop Unnecessary FLUSH PRIVILEGES Invocations

GRANT and REVOKE modify in-memory access control structures instantaneously. Executing FLUSH PRIVILEGES is completely redundant unless you directly mutate mysql.user via raw INSERT/UPDATE statements.

3. Enforce Automatic Role Activation

Make assigned roles active upon login using SET DEFAULT ROLE:

sql
CREATE ROLE r_reader; GRANT SELECT ON app.* TO r_reader; GRANT r_reader TO 'app'@'10.0.2.%'; -- Enforce default activation: SET DEFAULT ROLE ALL TO 'app'@'10.0.2.%';

▸Related Error Guides & Hub Links

Other Database Error Triage Playbooks

Other battle-tested diagnostic guides for high-severity database errors.

All Errors (50)