Waitingfortablemetadatalock

【问题描述】:MySQL5.6的主从环境,在主库上添加字段始终添加不了,一直没有返回值。
【排查分析】:
1、在测试环境上执行没有问题,很快就返回。因为数据量不大,也就10w左右(线上也是10w左右)
2、在执行alter语句时,在另外的终端通过show processlist发现 有等待TML锁
Waiting for table metadata lock
3、通过如下sql查线程ID

成都创新互联是网站建设专家,致力于互联网品牌建设与网络营销,专业领域包括网站设计、成都做网站、电商网站制作开发、微信小程序开发、微信营销、系统平台开发,与其他网站设计及系统开发公司不同,我们的整合解决方案结合了恒基网络品牌建设经验和互联网整合营销的理念,并将策略和执行紧密结合,且不断评估并优化我们的方案,为客户提供全方位的互联网品牌整合方案!

SELECT distinct b.trx_id blocking_trx_id,
b.trx_mysql_thread_id 源头锁thread_id,
SUBSTRING(p. HOST, 1, INSTR(p. HOST, ':') - 1) blocking_host,
SUBSTRING(p. HOST, INSTR(p. HOST, ':') + 1) blocking_port,
IF(p.COMMAND = 'Sleep', p.TIME, 0) idel_in_trx,
b.trx_query blocking_query,
r.trx_id waiting_trx_id,
r.trx_mysql_thread_id waiting_thread,
TIMESTAMPDIFF(SECOND, r.trx_wait_started, CURRENT_TIMESTAMP) wait_time,
r.trx_query waiting_query,
l.lock_table waiting_table_lock
FROM information_schema.INNODB_LOCKS l
LEFT JOIN information_schema.INNODB_LOCK_WAITS w
ON w.requested_lock_id = l.lock_id
LEFT JOIN information_schema.INNODB_TRX b
ON b.trx_id = w.blocking_trx_id
LEFT JOIN information_schema.INNODB_TRX r
ON r.trx_id = w.requesting_trx_id
LEFT JOIN information_schema. PROCESSLIST p
ON p.ID = b.trx_mysql_thread_id
ORDER BY wait_time DESC;

如果没有返回值 ,使用下面的sql来查:

select i.trx_mysql_thread_id
from  information_schema.innodb_trx i,
  (select 
         id, time
     from
         information_schema.processlist
     where
         time = (select 
                 max(time)
             from
                 information_schema.processlist
             where
                 state = 'Waiting for table metadata lock'
                     and substring(info, 1, 5) in ('alter' , 'optim', 'repai', 'lock ', 'drop ', 'creat'))) p
  where timestampdiff(second, i.trx_started, now()) > p.time
  and i.trx_mysql_thread_id  not in (connection_id(),p.id);

Waiting for table metadata lock

根据返回的结果继续查相关的session:
select * from information_schema.processlist where id=104018;
Waiting for table metadata lock

可以根据情况对该会话进行处理。
Waiting for table metadata lock


文章标题:Waitingfortablemetadatalock
URL地址:http://pwwzsj.com/article/geipgs.html