This error (ERROR 1100 (HY000): Table 'user' was not locked with LOCK TABLES
) encountered in MySQL is because after locking a specific table using the LOCK TABLES
command, execution related to global permissions or system Table-related operations (such as FLUSH PRIVILEGES
) that require access to unlocked system tables (such as user
tables).
explain
When we execute theLOCK TABLES
command, MySQL locks the specified tables to protect them from write operations by other sessions. In the locked state, the current session can only access the locked table. This means that after locking some tables, trying to access unlocked tables (such as system tables or other tables not included in theLOCK TABLES
command) will result in an error.
WhyFLUSH PRIVILEGES
throws an error
FLUSH PRIVILEGES
The command is used to reload the permission table (such as the user
table) to ensure that permission changes take effect immediately. Since the permissions table was not locked after executing LOCK TABLES
, when trying to execute FLUSH PRIVILEGES
, MySQL detected that the permissions table was not locked, thus causing an error.
How to solve
-
Unlock the table:
needs to be unlocked before performingFLUSH PRIVILEGES
or operations that require access to other tables Locked form. Use the following command:UNLOCK TABLES;
-
Execute the operation again:
After unlocking the table, you can performFLUSH PRIVILEGES
or other operations that require access to the unlocked The table is operated.
Precautions
- When using
LOCK TABLES
, please make sure we understand the scope of the lock to avoid unnecessary access conflicts or errors. - Use when you need to perform maintenance on the table or perform a specific operation
LOCK TABLES
, but remember to release the lock after the operation is completed.