如何查看MySQL加锁状态
information_schema.innodb_locks
这个表罗列了两类锁:
- 事务请求了但是还没有获取到的锁
- 事务持有的锁,并且这个锁阻塞了其他事务的执行
The INNODB_LOCKS table contains information about each lock that an InnoDB transaction has requested but not yet acquired, and each lock that a transaction holds that is blocking another transaction.
Column name | 描述 | Description |
---|---|---|
LOCK_ID | Unique lock ID number, internal to InnoDB. Treat it as an opaque string. Although LOCK_ID currently contains TRX_ID, the format of the data in LOCK_ID is subject to change at any time. Do not write applications that parse the LOCK_ID value. | |
LOCK_TRX_ID | ID of the transaction holding the lock. To obtain details about the transaction, join this column with the TRX_ID column of theINNODB_TRX table. | |
LOCK_MODE | How the lock is requested. Permitted values are S[,GAP], X[,GAP], IS[,GAP], IX[,GAP], AUTO_INC, and UNKNOWN. Lock modes other than AUTO_INC and UNKNOWN indicate gap locks, if present. For information about S, X, IS, IX, and gap locks, refer to Section 14.8.1, “InnoDB Locking”. | |
LOCK_TYPE | The type of lock. Permitted values are RECORD for a row-level lock, TABLE for a table-level lock. |
|
LOCK_TABLE | Name of the table that has been locked or contains locked records. | |
LOCK_INDEX | Name of the index, if LOCK_TYPE is RECORD; otherwise NULL. | |
LOCK_SPACE | Tablespace ID of the locked record, if LOCK_TYPE is RECORD ; otherwise NULL . |
|
LOCK_PAGE | Page number of the locked record, if LOCK_TYPE is RECORD ; otherwise NULL . |
|
LOCK_REC | Heap number of the locked record within the page, if LOCK_TYPE is RECORD; otherwise NULL. | |
LOCK_DATA | The data associated with the lock, if any. Values are primary key values of the locked record if LOCK_TYPE is RECORD, otherwise NULL. This column contains the values of the primary key columns in the locked row, formatted as a valid SQL string (ready to be copied to SQL statements). If there is no primary key, LOCK_DATA is the unique InnoDB internal row ID number. If a gap lock is taken for key values or ranges above the largest value in the index, LOCK_DATA reports supremum pseudo-record. When the page containing the locked record is not in the buffer pool (in the case that it was paged out to disk while the lock was held), InnoDB does not fetch the page from disk, to avoid unnecessary disk operations. Instead, LOCK_DATA is set to NULL. |
information_schema.innodb_lock_waits
这个表罗列了所有当前是阻塞的事务,指出
The INNODB_LOCK_WAITS table contains one or more rows for each blocked InnoDB transaction, indicating the lock it has requested and any locks that are blocking that request.
Column name | 描述 | Description |
---|---|---|
REQUESTING_TRX_ID | 请求事务Id | ID of the requesting (blocked) transaction. |
REQUESTED_LOCK_ID | 请求事务申请的锁id | ID of the lock for which a transaction is waiting. To obtain details about the lock, join this column with the LOCK_ID column of theINNODB_LOCKS table. |
BLOCKING_TRX_ID | 阻塞请求事务的其他事务id | ID of the blocking transaction. |
BLOCKING_LOCK_ID | 阻塞请求事务的锁的id | ID of a lock held by a transaction blocking another transaction from proceeding. To obtain details about the lock, join this column with the LOCK_ID column of the INNODB_LOCKS table. |
举个例子
1 | mysql> select * from information_schema.innodb_locks; |
另外注意,只有在存在锁冲突时,这两个表才会记录对应的数据。
show engine innodb status
输出 innodb 的状态信息,其中有事务的加锁情况
1 | set global innodb_status_output_locks=ON; // 输出的内容包含锁的详细信息 |
一个结果:
1 | | InnoDB | | |
资料:
https://dev.mysql.com/doc/refman/5.5/en/innodb-locks-table.html
https://dev.mysql.com/doc/refman/5.5/en/innodb-lock-waits-table.html
http://www.fanyilun.me/2017/04/20/MySQL%E5%8A%A0%E9%94%81%E5%88%86%E6%9E%90/
https://dev.mysql.com/doc/refman/5.5/en/innodb-standard-monitor.html