1. Metadata lock wait 出现的场景mysql
建立、删除索引sql
修改表结构数据库
表维护操做(optimize table、repair table 等)并发
删除表ide
获取表上表级写锁 (lock table tab_name write)fetch
注:支持事务的 InnoDB 引擎表和不支持事务的 MyISAM 引擎表,都会出现 Metadata Lock Wait 等待现象。spa
一旦出现 Metadata Lock Wait 等待现象,后续全部对该表的访问都会阻塞在该等待上,致使链接堆积,业务受影响。orm
2. Metadata lock wait 的含义索引
为了在并发环境下维护表元数据的数据一致性,在表上有活动事务(显式或隐式)的时候,不能够对元数据进行写入操做。所以 MySQL引入了metadata lock ,来保护表的元数据信息。事件
所以在对表进行上述操做时,若是表上有活动事务(未提交或回滚),请求写入的会话会等待在 Metadata lock wait
3. 致使 Metadata lock wait 等待的活动事务
当前有对表的长时间查询
显示或者隐式开启事务后未提交或回滚,好比查询完成后未提交或者回滚。
表上有失败的查询事务
4. 解决方案
a.show processlist 查看会话有长时间未完成的查询,使用kill 命令终止该查询。
b.查询 information_schema.innodb_trx 看到有长时间未完成的事务, 使用 kill 命令终止该查询。
select concat('kill ',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);
c.若是上面两个检查没有发现,或者事务过多,建议使用下面的查询将相关库上的会话终止
-- MySQL 5.6 select concat('kill ', a.owner_thread_id, ';') from information_schema.metadata_locks a left join (select b.owner_thread_id from information_schema.metadata_locks b, information_schema.metadata_locks c where b.owner_thread_id = c.owner_thread_id and b.lock_status = 'granted' and c.lock_status = 'pending') d ON a.owner_thread_id = d.owner_thread_id where a.lock_status = 'granted' and d.owner_thread_id is null; -- MySQL 5.5 select concat('kill ', p1.id, ';') from information_schema.processlist p1, (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', 'trunc'))) p2 where p1.time >= p2.time and p1.command in ('Sleep' , 'Query') and p1.id not in (connection_id() , p2.id);
5. 如何避免出现长时间 metadata lock wait 致使表上相关查询阻塞,影响业务
在业务低峰期执行上述操做,好比建立删除索引。
在到数据库链接创建后,设置会话变量 autocommit 为 1 或者 on,好比 set autocommit=1; 或 set autocommit=on; 。
考虑使用事件来终止长时间运行的事务,好比下面的例子中会终止执行时间超过60分钟的事务。
create event my_long_running_trx_monitor on schedule every 60 minute starts '2018-08-08 11:00:00' on completion preserve enable do begin declare v_sql varchar(500); declare no_more_long_running_trx integer default 0; declare c_tid cursor for select concat ('kill ',trx_mysql_thread_id,';') from information_schema.innodb_trx where timestampdiff(minute,trx_started,now()) >= 60; declare continue handler for not found set no_more_long_running_trx=1; open c_tid; repeat fetch c_tid into v_sql; set @v_sql=v_sql; prepare stmt from @v_sql; execute stmt; deallocate prepare stmt; until no_more_long_running_trx end repeat; close c_tid; end;