MySQL查看锁的sql

MySQL查看锁的sql

(1)查看当前有无锁等待
mysql> show status like 'innodb_row_lock%';
(2)查看哪个事务在等待(被阻塞了)
mysql> select * from information_schema.INNODB_TRX WHERE trx_state='LOCK WAIT'\G
(3)查询该事务被哪个事务给阻塞了 从innodb_trx获取到被阻塞的trx_id是3934,阻塞该事务的事务id是3933
mysql> SELECT * FROM performance_schema.threads WHERE processlist_id=970\G
(4)根据trx_id,从innodb_trx表可查询到trx_mysql_thread_id线程id为970
mysql> select * from information_schema.innodb_trx where trx_id=3933 \G
(5)根据线程id,查询表拿到thread_id为995
mysql> SELECT * FROM performance_schema.threads WHERE processlist_id=970\G
**(6)根据thread_id,查询当前锁源的sql
mysql> SELECT * FROM performance_schema.events_statements_current WHERE thread_id=995\G

查看数据库状态

会显示加锁的信息等等

show engine innodb status;  

查看正在执行的线程信息

show full processlist;

查看正在锁的表

show open tables where in_use > 0;
show open tables;

查看锁的类型、状态

show status like '%lock%';

5.0后,增加了3个关于锁的表
MySQL5.7版本
INFORMATION_SCHEMA.innodb_trx 当前运行的所有事务
INFORMATION_SCHEMA.innodb_locks 当前出现的锁
INFORMATION_SCHEMA.innodb_lock_waits 锁等待的对应关系
MySQL8.0版本
8.0后,performance_schema.data_locks代替了INFORMATION_SCHEMA.innodb_locks ,performance_schema.data_lock_waits代替了INFORMATION_SCHEMA.innodb_lock_waits

查看正在锁的事务

SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS; -- 5.7
SELECT * FROM performance_schema.data_locks; -- 8.0

查看等待锁的事务

SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS; -- 5.7
SELECT * FROM performance_schema.data_lock_waits; -- 8.0

查看行锁情况

show status like 'InnoDB_row_lock%';

查看表锁情况

show status like 'table%';

开启锁监控
开启监控后,可以看到锁的具体信息以及加锁顺序等,更容易分析死锁。

方式一:创建监控表,监控某个数据库
开启:

CREATE TABLE innodb_lock_monitor (a INT) ENGINE=INNODB;
 
关闭:

DROP TABLE innodb_lock_monitor;
 
注意:在未拥有设置全局属性权限下也可以开启该功能。

方式二,开启全局监控
开启:

set GLOBAL innodb_status_output=ON;
set GLOBAL innodb_status_output_locks=ON;
 
关闭:

set GLOBAL innodb_status_output=OFF;
set GLOBAL innodb_status_output_locks=OFF;

查看数据库状态

show engine innodb status \G;

其他监控
标准监控(Standard InnoDB Monitor):监视活动事务持有的表锁、行锁;事务锁等待;线程信号量等待;文件IO请求;buffer pool统计信息;InnoDB主线程purge和change buffer merge活动。

方式一

CREATE TABLE innodb_monitor (a INT) ENGINE=INNODB;
DROP TABLE innodb_monitor;

方式二

set GLOBAL innodb_status_output=ON;
set GLOBAL innodb_status_output=OFF;

锁监控(InnoDB Lock Monitor):提供额外的锁信息。

# 方式一

CREATE TABLE innodb_lock_monitor (a INT) ENGINE=INNODB;
DROP TABLE innodb_lock_monitor;
# 方式二
set GLOBAL innodb_status_output=ON;
set GLOBAL innodb_status_output_locks=ON;
set GLOBAL innodb_status_output=OFF;
set GLOBAL innodb_status_output_locks=OFF;

表空间监控(InnoDB Tablespace Monitor):显示共享表空间中的文件段以及表空间数据结构配置验证。

CREATE TABLE innodb_tablespace_monitor (a INT) ENGINE=INNODB;
DROP TABLE innodb_tablespace_monitor;

表监控(InnoDB Table Monitor):显示内部数据字典的内容。

CREATE TABLE innodb_tablespace_monitor (a INT) ENGINE=INNODB;
DROP TABLE innodb_tablespace_monitor;

你可能感兴趣的:(mysql,sql,数据库)