Skip to main content

Fixed: "ERROR 1142 (42000): SELECT command denied to user for table 'data_locks'" in MySQL

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Looking at MySQL's locks failed with ERROR 1142 (42000): SELECT command denied to user 'inventory_app'@'localhost' for table 'data_locks'. The table's fine, and so is the account: it just has no rights outside its own database. Here's the fix, on a separate account, so the application's account stays as it is.

What I Was Doing​

An UPDATE was stuck, and I wanted to see why. I was connected as the application's account, inventory_app, on MySQL 9.7.4 in Docker:

mysql
SELECT * FROM sys.innodb_lock_waits;
SELECT * FROM performance_schema.data_locks;

What I Expected​

A list of who's waiting for whom.

What I Got​

Output
ERROR 1142 (42000): SELECT command denied to user 'inventory_app'@'localhost' for table 'innodb_lock_waits'
ERROR 1142 (42000): SELECT command denied to user 'inventory_app'@'localhost' for table 'data_locks'

A mysql session where SELECT from sys.innodb_lock_waits and performance_schema.data_locks both fail with ERROR 1142 SELECT command denied.

What I Checked​

The account's rights.

mysql
SHOW GRANTS;
Output
+--------------------------------------------------------------+
| Grants for inventory_app@% |
+--------------------------------------------------------------+
| GRANT USAGE ON *.* TO `inventory_app`@`%` |
| GRANT ALL PRIVILEGES ON `inventory`.* TO `inventory_app`@`%` |
+--------------------------------------------------------------+
2 rows in set (0.000 sec)

ALL PRIVILEGES, on inventory.* only.

Where the tables live. sys and performance_schema are separate schemas. Reading a table in one takes a SELECT right that covers it, and this account has no such right.

Why It Happens​

The message names the table, which makes it look as if that table has a problem. It means this account has no SELECT right on it. Rights on inventory stop at inventory.

The Fix​

Give the rights to a separate account, as root, and leave the application's account alone. Choose your own password:

mysql: root session
CREATE USER IF NOT EXISTS 'lock_viewer'@'%' IDENTIFIED BY 'choose-a-strong-password';
GRANT PROCESS ON *.* TO 'lock_viewer'@'%';
GRANT SELECT ON performance_schema.* TO 'lock_viewer'@'%';
GRANT SELECT, EXECUTE ON sys.* TO 'lock_viewer'@'%';

The sys views also need EXECUTE, because they call functions, and PROCESS, because they read innodb_trx. Connect as lock_viewer, with no database on the command line, or you'll get ERROR 1044.

After the Fix​

mysql: as lock_viewer
SELECT waiting_pid, blocking_pid, wait_age FROM sys.innodb_lock_waits;
SELECT COUNT(*) AS locks FROM performance_schema.data_locks;
Output
Empty set (0.007 sec)

+-------+
| locks |
+-------+
| 0 |
+-------+
1 row in set (0.000 sec)

The lock_viewer account reading sys.innodb_lock_waits and performance_schema.data_locks without an error.

Empty set here is a real answer: nothing was waiting when I ran it, and there was no error. The application's tables are still out of reach for this account:

Output (SELECT COUNT(*) FROM inventory.products)
ERROR 1142 (42000): SELECT command denied to user 'lock_viewer'@'localhost' for table 'products'

Common Mistakes​

MistakeWhat happensDo this instead
Granting ALL on *.* to fix itThe account can do anythingGrant what's needed
Forgetting EXECUTE on sysERROR 1356Grant SELECT, EXECUTE
Forgetting PROCESSERROR 1227Grant it on *.*
Reading the message as a broken tableThe table's fineCheck SHOW GRANTS

Troubleshooting​

SymptomLikely causeFix
The same error on your own tableThe account has no right on itGrant SELECT on that table, or use the right account
The error names 'lock_viewer'@'localhost', and you granted to '%'That's normal: the message shows where you connected fromCheck SHOW GRANTS FOR 'lock_viewer'@'%'
Empty setNothing is waitingNo fix needed

Conclusion​

ERROR 1142 means the account has no SELECT right on that table. For performance_schema and sys, grant them to a small account that's just for looking, with PROCESS, and keep your application's account limited to its own database.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.