" ."
I met the same thing and found a reason in MySQL Docs:
MySQL8.0
As of MySQL 8.0.13, you can rename tables locked with a LOCK TABLES statement, provided that they are locked with a WRITE lock or are the product of renaming WRITE-locked tables from earlier steps in a multiple-table rename operation.
MySQL5.7
To execute RENAME TABLE, there must be no active transactions or tables locked with LOCK TABLES.
By the way, in MySQL 5.7, when the table is locked with the LOCK tables tbl WRITE statement, the lock will be released due to the ALTER TABLE tbl_0 RENAME TO tbl_1 command and strange behavior will occur in the same session and new session.
# MySQL 5.7
# session 0
mysql> lock tables tbl_0 WRITE;
Query OK, 0 rows affected (0.02 sec)
mysql> ALTER TABLE tbl_0 RENAME TO tbl_1;
Query OK, 0 rows affected (0.02 sec)
mysql> select * from tbl_1;
ERROR 1100 (HY000): Table 'tbl_1' was not locked with LOCK TABLES
# then start new session
# session 1
mysql> select * from tbl_1;
...
1 row in set (0.01 sec)
# session 0
mysql> unlock tables;
Hope this helps.
source
share