偶遇MySQL Metadata Lock

在主库上truncate table后,发现从库延时开始增加,show processlist可见如下信息。


72105289  system user  NULL  Connect 2996302 Waiting for master to send event  NULL  0  0

72105290  system user  dbname  Connect 491  Waiting for table metadata lock truncate table tblname  0  0



MySQL用metadata lock控制对于数据库对象的并发访问,和保证数据的一致性。MySQL中不能对一个正在使用的表执行DDL语句,就是通过获取表的metadata lock实现的,其阻止了表结构的变更。即,若表正被一个事务使用,在该事务结束前,另一个会话是不能对此表执行DDL的。


Waiting for table metadata lock,表明在truncate table之前已有事务在使用tblname表了,且该事务还没结束,从show processlist中看不到进一步的信息了。



但information_schema中的innodb_trx表可以提供正在InnoDB中运行着的事务的信息,如事务是否在等待锁、事务的开始时间、事务中正在运行的SQL等。


查看innodb_trx表,发现了一个事务,Id为90916563(trx_mysql_thread_id对应show processlist中的Id),开始时间是2019-02-12 13:26:56,在主库执行truncate table的时间是17:35,其运行的时间有些异常;另trx_query为NULL,可判断该事务很可能卡在这,未正常结束,阻止了truncate table的继续。


mysql> select * from INNODB_TRX \G

*************************** 1. row ***************************

                    trx_id: 146633268809

                 trx_state: RUNNING

               trx_started: 2019-02-12 13:26:56

     trx_requested_lock_id: NULL

          trx_wait_started: NULL

                trx_weight: 0

       trx_mysql_thread_id: 90916563

                 trx_query: NULL

       trx_operation_state: NULL

         trx_tables_in_use: 0

         trx_tables_locked: 0

          trx_lock_structs: 0

     trx_lock_memory_bytes: 1136

           trx_rows_locked: 0

         trx_rows_modified: 0

   trx_concurrency_tickets: 0

       trx_isolation_level: REPEATABLE READ

         trx_unique_checks: 1

    trx_foreign_key_checks: 1

trx_last_foreign_key_error: NULL

 trx_adaptive_hash_latched: 0

 trx_adaptive_hash_timeout: 0

          trx_is_read_only: 0

trx_autocommit_non_locking: 0

1 row in set (0.00 sec)


从show processlist中看到Id为90916563线程的信息,其Time为1294。kill掉该线程,主从复制恢复正常。

90916563  dbname_read  192.168.2.36:63063  dbname  Sleep  1294  NULL  100  100



若想查看metadata lock的详细信息,可借助Performance Schema下的metadata_locks表。



PS. MySQL的版本是5.7.17-13-log。


猜你喜欢

转载自blog.51cto.com/coveringindex/2349945
今日推荐