Description: If user1 has an active transaction on a table and then user2 drops this table, then user1 does COMMIT, then in the binlog we have something like: DROP TABLE t; BEGIN; INSERT INTO t ... ; COMMIT; which is wrong.
根据查询到的trx_started时间以及trx_mysql_thread_id到MySQL的general log中查找,当然前提是开启了general log的功能,在general日志中对应的时间发现该thread执行了语句: set autocommit=0; 关闭了自动提交,再往下看,oh my god......下面居然是一堆SELECT语句!
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from test;
+----------+
| date |
+----------+
| 20150616 |
| 20150617 |
| 20150619 |
+----------+
3 rows in set (0.00 sec)
会话2:对test表执行DDL:
mysql> alter table test add index `date`(`date`);
语句被阻塞,show processlist查看状态:
mysql> show processlist;
+-------+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-------+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-------------------------------------------+
| 16 | system user | | NULL | Connect | 540155 | Waiting for master to send event | NULL |
| 17 | system user | | NULL | Connect | 529732 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 51673 | root | localhost | test | Sleep | 55 | | NULL |
| 51681 | root | localhost | test | Query | 0 | init | show processlist |
| 51683 | root | localhost | test | Query | 29 | Waiting for table metadata lock | alter table test add index `date`(`date`) |
+-------+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-------------------------------------------+
5 rows in set (0.00 sec)
可以看到alter table语句的状态为Waiting for table metadata lock
会话3:对test表进行查询:
mysql> select * from test; 同样被阻塞:
mysql> show processlist;
+-------+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-------+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-------------------------------------------+
| 16 | system user | | NULL | Connect | 540305 | Waiting for master to send event | NULL |
| 17 | system user | | NULL | Connect | 529882 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL |
| 51673 | root | localhost | test | Sleep | 205 | | NULL |
| 51681 | root | localhost | test | Query | 0 | init | show processlist |
| 51683 | root | localhost | test | Query | 179 | Waiting for table metadata lock | alter table test add index `date`(`date`) |
| 51703 | root | localhost | test | Query | 18 | Waiting for table metadata lock | select * from test |
+-------+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+-------------------------------------------+
6 rows in set (0.00 sec)