排查Waiting for table metadata lock问题
1.问题描述
MySQL8版本,一个表41万条数据,创建索引时间长,未成功。
2.问题排查
检查正在锁的表:
show open tables where in_use > 0;
未发现有记录
检查正在锁的事务和检查等待锁的事务:
select * from performance_schema.data_locks;
select * from performance_schema.data_lock_waits;
未发现有记录
查看正在执行的线程信息:
show full processlist;
显示有 Waiting for table metadata lock
MySQL的系统库表有数据维护对应的信息,就在information_schema库中的INNODB_TRX表,包含事务中是否存在锁,事务开启时间,事务执行的语句等等。
查看执行完成,但未commit的sql语句 events_statements_current 。
select * from information_schema.innodb_trx t order by t.trx_started;
select * from performance_schema.events_statements_current;
查询到有3条select 查询在锁状态。
select * from information_schema.innodb_trx t order by t.trx_started;
trx_mysql_thread_id 字段的MySQL的sid 对应3个查询语句
kill 3439879;
kill 3442094;
kill 3443504;
再检查,没有锁定的用户select 。
再创建索引,几秒钟就完成了。
检查行锁
show status like ‘InnoDB_row_lock%’;
检查表锁
show status like ‘table%’;