Search code examples
mysqlinnodbinformation-schema

mysql innodb_locks table dump to file


According to mysql reference about InnoDB Transaction and Locking Information page:

  1. The data exposed by the transaction and locking tables (INNODB_TRX, INNODB_LOCKS, and INNODB_LOCK_WAITS) represents a glimpse into fast-changing data.
  2. For performance reasons, and to minimize the chance of misleading joins between the transaction and locking tables, InnoDB collects the required transaction and locking information into an intermediate buffer whenever a SELECT on any of the tables is issued.

So I'd like to know if there is a way to write this info/buffer into a file?


Solution

  • I don't think there's any way to access the buffer you speak of directly. You can use SELECT queries against the named INFORMATION_SCHEMA tables, which will read from those buffers indirectly.

    An alternative is to make the output of SHOW ENGINE INNODB STATUS be dumped to the MySQL Server error log every 15 seconds. You can optionally make this status include lock information. See https://dev.mysql.com/doc/refman/5.6/en/innodb-enabling-monitors.html

    Example: I enabled the InnoDB lock monitor with lock options.

    mysql> set global innodb_status_output_locks=on;
    mysql> set global innodb_status_output=on;
    

    Then I created one transaction, inserting into a test table. But don't commit yet.

    mysql> BEGIN;
    mysql> INSERT INTO t VALUES (1,1);
    

    In a second window, I begin another transaction, with another insert designed to conflict with the first one.

    mysql> INSERT INTO t VALUES (1,1);
    

    This hangs, waiting for the lock held by the first session.

    Then tail the MySQL error log, to observe the locks:

    ------------
    TRANSACTIONS
    ------------
    Trx id counter 3528210
    Purge done for trx's n:o < 3528208 undo n:o < 0 state: running but idle
    History list length 814
    LIST OF TRANSACTIONS FOR EACH SESSION:
    ---TRANSACTION 3528209, ACTIVE 30 sec inserting
    mysql tables in use 1, locked 1
    LOCK WAIT 2 lock struct(s), heap size 360, 1 row lock(s)
    MySQL thread id 1, OS thread handle 0x70000a4bd000, query id 23 localhost root update
    insert into t values (1,1,null,null)
    ------- TRX HAS BEEN WAITING 30 SEC FOR THIS LOCK TO BE GRANTED:
    RECORD LOCKS space id 3342 page no 3 n bits 72 index `PRIMARY` of table `test`.`t` trx id 3528209 lock mode S locks rec but not gap waiting
    ------------------
    TABLE LOCK table `test`.`t` trx id 3528209 lock mode IX
    RECORD LOCKS space id 3342 page no 3 n bits 72 index `PRIMARY` of table `test`.`t` trx id 3528209 lock mode S locks rec but not gap waiting
    ---TRANSACTION 3528208, ACTIVE 49 sec
    2 lock struct(s), heap size 360, 1 row lock(s), undo log entries 1
    MySQL thread id 2, OS thread handle 0x70000a501000, query id 17 localhost root
    TABLE LOCK table `test`.`t` trx id 3528208 lock mode IX
    RECORD LOCKS space id 3342 page no 3 n bits 72 index `PRIMARY` of table `test`.`t` trx id 3528208 lock_mode X locks rec but not gap