Mysql 从库上出现Waiting for table metadata lock 分析
背景:
业务反馈在主库上做了一个DDL操作,执行时间很快,但是从机上却一直报主从延迟告警。
分析:
1. 在主机上查看当前的活动线程,发现从机回放进程处于 Waiting for table metadata lock中,但是并没有发现任何其它可疑进程。
--------+-------------+-----------------+--------+---------+---------+----------------------------------+-----------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +--------+-------------+-----------------+--------+---------+---------+----------------------------------+-----------------------------------+ | 6 | system user | | NULL | Connect | 3649022 | Waiting for master to send event | NULL | | 7 | system user | | testtt | Connect | 9 | Waiting for table metadata lock | alter table v_t add column aa int | | 181873 | vector | 127.0.0.1:41634 | testtt | Query | 0 | starting | show processlist | +--------+-------------+-----------------+--------+---------+---------+----------------------------------+-----------------------------------+
2. 查看系统的未提交事务,发现了一个业务进程开启了事务但是一直未提交。
+-----------------+--------+------+---------------------+---------------------+------------------------------------------------------------------------------------------------------+ | host | user | time | trx_mysql_thread_id | trx_started | left(p.info,100) | +-----------------+--------+------+---------------------+---------------------+------------------------------------------------------------------------------------------------------+ | 127.0.0.1:41634 | vector | 0 | 181873 | 2020-04-23 20:41:29 | select p.host,p.user,p.time,t.trx_mysql_thread_id,t.trx_started,left(p.info,100) from information_sc | +-----------------+--------+------+---------------------+---------------------+------------------------------------------------------------------------------------------------------+
3. 手动KILL掉这个线程后,业务恢复正常
总结: 从机上也是可以通过设置set autocommit=0等方式来显示开启事务,从而阻塞从机回放线程的。