Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Table 'my_table' was not locked with Lock Tables

Tags:

mysql

I try and load tables via MySQL and get the following error?

MySQL said: Table 'cms' was not locked with LOCK TABLES

Why does the table need to be Locked? I haven't seen this before? is there any way to unlock? do you even want to?

like image 395
Kieran Headley Avatar asked Apr 07 '16 05:04

Kieran Headley


People also ask

How do you check if MySQL table is locked?

In MySQL, locked tables are identified using the SHOW OPEN TABLES command. In its simplest form is displays all locked tables. All open tables in the table cache are listed, but the IN_USE column indicates of the table is locked. When the first lock is taken, the value increments to 1.

How do I lock a table in MySQL?

LOCK TABLES works as follows: Sort all tables to be locked in an internally defined order (from the user standpoint the order is undefined). If a table is locked with a read and a write lock, put the write lock before the read lock. Lock one table at a time until the thread gets all locks.

Do transactions lock tables MySQL?

LOCK TABLES is not transaction-safe and implicitly commit any active transaction before attempting to lock the tables. Beginning a transaction, for instance, START TRANSACTION implicitly commits any current transaction and releases existing MySQL locks.


2 Answers

If in one session, you locked one table but want to select from another table, you must either lock that table too or unlock all tables.

mysql> LOCK TABLES t1 READ; mysql> SELECT COUNT(*) FROM t1; +----------+ | COUNT(*) | +----------+ |        3 | +----------+ mysql> SELECT COUNT(*) FROM t2; ERROR 1100 (HY000): Table 't2' was not locked with LOCK TABLES 
like image 102
Yu Jiaao Avatar answered Sep 20 '22 15:09

Yu Jiaao


The solution for me was to unlock the tables. They had been locked by a previous query which failed before reaching the unlock tables statement.

UNLOCK TABLES SELECT ... 
like image 32
sashoalm Avatar answered Sep 18 '22 15:09

sashoalm