Sorry for the long answer, but it will need to be answered in several parts.
1. When locking InnoDB tables with LOCK TABLES in general
Using LOCK TABLES with InnoDB actually works and can be demonstrated using two MySQL CLI instances connected to the same server (indicated by mysql-1 and mysql-2 ) in the example below. Generally, this should be avoided in any kind of production context due to exposure to customers, but sometimes this may be the only option.
Create a table and fill it with some data:
mysql-1> create table a (id int not null primary key) engine=innodb; Query OK, 0 rows affected (0.02 sec) mysql-1> insert into a (id) values (1), (2), (3); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0
Lock table:
mysql-1> lock tables a write; Query OK, 0 rows affected (0.00 sec)
Try pasting from mysql-2 , which will wait for a lock wait:
mysql-2> insert into a (id) values (4);
Now open the table from mysql-1 :
mysql-1> unlock tables; Query OK, 0 rows affected (0.00 sec)
And finally, mysql-2 unlocks and returns:
Query OK, 1 row affected (6.30 sec)
2. Using phpMyAdmin for testing
Your test method using phpMyAdmin is not valid because phpMyAdmin does not support a constant connection to the server between requests from its web interface. To use any LOCK TABLES , START TRANSACTION , etc. locks, you need to maintain a connection while the locks are held.
3. Lock all tables needed during operation
How MySQL locks tables, once you used LOCK TABLES to explicitly lock something, you wonβt be able to access any other tables that were not explicitly locked during the LOCK ... UNLOCK session. In the above example, you need to use:
LOCK TABLES my_table WRITE, new_table WRITE, table2 READ;
(I assume that table2 used in the subquery was not a typo.)
4. Atomic table exchange using RENAME TABLE
In addition, it should be noted that replacing an existing table with DROP TABLE , followed by RENAME TABLE , will lead to a brief moment when the table does not exist, and this can confuse clients who expect it to exist. As a rule, it is much better to do:
CREATE TABLE t_new (...); <Populate t_new using some method> RENAME TABLE t TO t_old, t_new TO t; DROP TABLE t_old;
This will result in an atomic replacement of the two tables.