MySQL 8.4Lab-verified · 2026-11-29

MySQL ERROR 1142 (42000): command denied to user for table

The user is missing the privilege that this statement needs on the named table.

MySQL 8.4.13
1142 (42000): SELECT command denied to user 'lab_b'@'127.0.0.1' for table 'orders'

Jump to a cause

  1. 01No privilege at all on a table in another database
  2. 02The user has SELECT but not INSERT
  3. 03A JOIN reads a table that was not granted
  4. 04TRUNCATE needs the DROP privilege

Lab-verifiedReproduced and fixed on MySQL 8.4.13 · 2026-11-29 · container image mysql:8.4

ERROR 1142 tells you exactly what is missing: the command (SELECT, INSERT, DROP …), the user, and the table. The user can log in and may even see the database, but has no privilege for that command on that table.

In the tests below, lab_b worked on tables in the shop database that root created. Each fix was a GRANT run by root. Note that the command in the message is not always the statement you ran: TRUNCATE reported DROP.

01No privilege at all on a table in another database

lab_b ran SELECT * FROM shop.orders and got ERROR 1142: SELECT command denied to user 'lab_b'@'127.0.0.1' for table 'orders'.

SHOW GRANTS FOR lab_b listed only USAGE and ALL PRIVILEGES on its own lab_b database, nothing on shop. After GRANT SELECT ON shop.orders TO lab_b, the same SELECT returned the row.

Setup
Setuprun as root
CREATE DATABASE shop;
CREATE TABLE shop.orders (id INT PRIMARY KEY, customer_id INT, amount DECIMAL(10,2));
CREATE TABLE shop.customers (id INT PRIMARY KEY, name VARCHAR(50));
INSERT INTO shop.customers VALUES (1, 'Kim');
INSERT INTO shop.orders VALUES (1, 1, 100.00);
OUTPUT
Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)
Reproducerun as lab_b
SELECT * FROM shop.orders;
✕ ERROR
ERROR 1142 (42000): SELECT command denied to user 'lab_b'@'127.0.0.1' for table 'orders'
Checkrun as root
SHOW GRANTS FOR lab_b;
OUTPUT
+--------------------------------------------------+
| Grants for lab_b@%                               |
+--------------------------------------------------+
| GRANT USAGE ON *.* TO `lab_b`@`%`                |
| GRANT ALL PRIVILEGES ON `lab_b`.* TO `lab_b`@`%` |
+--------------------------------------------------+
2 rows in set (0.00 sec)
Fixrun as root
GRANT SELECT ON shop.orders TO lab_b;
OUTPUT
Query OK, 0 rows affected (0.01 sec)

Run SHOW GRANTS for the user first to see what it actually has before granting more.

After the fixrun as lab_b
SELECT * FROM shop.orders;
✓ SUCCESS
+----+-------------+--------+
| id | customer_id | amount |
+----+-------------+--------+
|  1 |           1 | 100.00 |
+----+-------------+--------+
1 row in set (0.00 sec)

02The user has SELECT but not INSERT

lab_b had GRANT SELECT ON shop.*, so reading worked, but INSERT INTO shop.orders failed with INSERT command denied to user … for table 'orders'.

SHOW GRANTS FOR lab_b showed only SELECT on shop.*. After GRANT INSERT ON shop.orders TO lab_b, the INSERT succeeded and the table had two rows.

Setup
Setuprun as root
CREATE DATABASE shop;
CREATE TABLE shop.orders (id INT PRIMARY KEY, customer_id INT, amount DECIMAL(10,2));
CREATE TABLE shop.customers (id INT PRIMARY KEY, name VARCHAR(50));
INSERT INTO shop.customers VALUES (1, 'Kim');
INSERT INTO shop.orders VALUES (1, 1, 100.00);
GRANT SELECT ON shop.* TO lab_b;
OUTPUT
Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)
Reproducerun as lab_b
INSERT INTO shop.orders VALUES (2, 1, 50.00);
✕ ERROR
ERROR 1142 (42000): INSERT command denied to user 'lab_b'@'127.0.0.1' for table 'orders'
Checkrun as root
SHOW GRANTS FOR lab_b;
OUTPUT
+--------------------------------------------------+
| Grants for lab_b@%                               |
+--------------------------------------------------+
| GRANT USAGE ON *.* TO `lab_b`@`%`                |
| GRANT SELECT ON `shop`.* TO `lab_b`@`%`          |
| GRANT ALL PRIVILEGES ON `lab_b`.* TO `lab_b`@`%` |
+--------------------------------------------------+
3 rows in set (0.00 sec)
Fixrun as root
GRANT INSERT ON shop.orders TO lab_b;
OUTPUT
Query OK, 0 rows affected (0.01 sec)
After the fixrun as lab_b
INSERT INTO shop.orders VALUES (2, 1, 50.00);
SELECT * FROM shop.orders;
✓ SUCCESS
Query OK, 1 row affected (0.00 sec)

+----+-------------+--------+
| id | customer_id | amount |
+----+-------------+--------+
|  1 |           1 | 100.00 |
|  2 |           1 |  50.00 |
+----+-------------+--------+
2 rows in set (0.00 sec)

03A JOIN reads a table that was not granted

lab_b had SELECT on shop.orders only. A query joining shop.orders with shop.customers failed with SELECT command denied … for table 'customers'. The message names the table without the privilege, not the first table in the query.

After GRANT SELECT ON shop.customers TO lab_b, the join returned id 1, Kim, 100.00.

Setup
Setuprun as root
CREATE DATABASE shop;
CREATE TABLE shop.orders (id INT PRIMARY KEY, customer_id INT, amount DECIMAL(10,2));
CREATE TABLE shop.customers (id INT PRIMARY KEY, name VARCHAR(50));
INSERT INTO shop.customers VALUES (1, 'Kim');
INSERT INTO shop.orders VALUES (1, 1, 100.00);
GRANT SELECT ON shop.orders TO lab_b;
OUTPUT
Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)
Reproducerun as lab_b
SELECT o.id, c.name, o.amount FROM shop.orders o JOIN shop.customers c ON c.id = o.customer_id;
✕ ERROR
ERROR 1142 (42000): SELECT command denied to user 'lab_b'@'127.0.0.1' for table 'customers'
Fixrun as root
GRANT SELECT ON shop.customers TO lab_b;
OUTPUT
Query OK, 0 rows affected (0.01 sec)
After the fixrun as lab_b
SELECT o.id, c.name, o.amount FROM shop.orders o JOIN shop.customers c ON c.id = o.customer_id;
✓ SUCCESS
+----+------+--------+
| id | name | amount |
+----+------+--------+
|  1 | Kim  | 100.00 |
+----+------+--------+
1 row in set (0.00 sec)

04TRUNCATE needs the DROP privilege

lab_b had SELECT, INSERT and DELETE on shop.orders. TRUNCATE TABLE shop.orders still failed, and the message said DROP command denied … for table 'orders', not TRUNCATE.

After GRANT DROP ON shop.orders TO lab_b, TRUNCATE succeeded and COUNT(*) was 0.

Setup
Setuprun as root
CREATE DATABASE shop;
CREATE TABLE shop.orders (id INT PRIMARY KEY, customer_id INT, amount DECIMAL(10,2));
CREATE TABLE shop.customers (id INT PRIMARY KEY, name VARCHAR(50));
INSERT INTO shop.customers VALUES (1, 'Kim');
INSERT INTO shop.orders VALUES (1, 1, 100.00);
GRANT SELECT, INSERT, DELETE ON shop.orders TO lab_b;
OUTPUT
Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)
Reproducerun as lab_b
TRUNCATE TABLE shop.orders;
✕ ERROR
ERROR 1142 (42000): DROP command denied to user 'lab_b'@'127.0.0.1' for table 'orders'
Fixrun as root
GRANT DROP ON shop.orders TO lab_b;
OUTPUT
Query OK, 0 rows affected (0.00 sec)

DROP on a table also allows dropping that table. If you only want to remove rows, DELETE (which this user already had) does not need it.

After the fixrun as lab_b
TRUNCATE TABLE shop.orders;
SELECT COUNT(*) FROM shop.orders;
✓ SUCCESS
Query OK, 0 rows affected (0.02 sec)

+----------+
| COUNT(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

FAQ

How do I fix SELECT command denied to user for table in MySQL?

Grant SELECT on that table (or database) to the user as an admin. In the test, GRANT SELECT ON shop.orders TO lab_b fixed it immediately, without reconnecting.

How can I see what privileges a MySQL user has?

Run SHOW GRANTS FOR the user. In the test it showed that lab_b had only SELECT on shop.*, which explained why INSERT was denied.

Why does TRUNCATE give DROP command denied?

TRUNCATE TABLE requires the DROP privilege. In the test, a user with SELECT, INSERT and DELETE got DROP command denied until DROP was granted on the table.

What is the difference between ERROR 1142 and ERROR 1045?

1045 means the login itself failed. 1142 means the user is logged in but lacks a privilege for a command on a specific table.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary users created for the test, each with full rights on a database of the same name (its default database); root is the administrator. MySQL 8.4.13