MySQL常用监控命令

一、查看mysql锁相关命令

1、通过INFORMATION_SCHEMA.INNODB_TRX、INNODB_LOCKS、INNODB_LOCK_WAITS 获取事务与锁的信息

1)查看哪些事务正在执行

命令:SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX;

trx_state表示事务的状态

MySQL常用监控命令

mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX \G
*************************** 1. row ***************************
trx_id: 4391
trx_state: LOCK WAIT   //事务状态,LOCK WAIT表示等待锁
trx_started: 2019-09-27 11:39:09
trx_requested_lock_id: 4391:57:962:104
trx_wait_started: 2019-09-27 11:39:09
trx_weight: 2
trx_mysql_thread_id: 9  //线程ID
trx_query: select * from employees where birth_date = '1953-09-02' for update
trx_operation_state: starting index read
trx_tables_in_use: 1
trx_tables_locked: 1
trx_lock_structs: 2
trx_lock_memory_bytes: 1136
trx_rows_locked: 1
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
*************************** 2. row ***************************
trx_id: 4390
trx_state: RUNNING    //事务状态,RUNNING表示正在运行的事务,如果一直运行中,有可能是block了
trx_started: 2019-09-27 11:38:41
trx_requested_lock_id: NULL
trx_wait_started: NULL
trx_weight: 65
trx_mysql_thread_id: 20   //线程ID
trx_query: NULL
trx_operation_state: NULL
trx_tables_in_use: 0
trx_tables_locked: 1
trx_lock_structs: 65
trx_lock_memory_bytes: 8400
trx_rows_locked: 127
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
2 rows in set (0.00 sec)

  

2)查看当前锁定的事务

命令:SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;

MySQL常用监控命令

mysql> select * from information_schema.INNODB_LOCKS\G
*************************** 1. row ***************************
lock_id: 17778:82:3:6 --当前锁ID
lock_trx_id: 17778 --该锁对应的事务ID
lock_mode: X -- 锁类型,排它锁X
lock_type: RECORD --锁范围,记录锁:record lock,其他锁范围:间隙锁:gap lock,或者next-key lock(记录锁+间隙锁)
lock_table: `test`.`trx_fee`
lock_index: PRIMARY --加载在哪个索引上的锁
lock_space: 82
lock_page: 3
lock_rec: 6
lock_data: 4
*************************** 2. row ***************************
lock_id: 17773:82:3:6
lock_trx_id: 17773
lock_mode: X
lock_type: RECORD
lock_table: `test`.`trx_fee`
lock_index: PRIMARY
lock_space: 82
lock_page: 3
lock_rec: 6
lock_data: 4

 

3)查看当前正在等待锁的事务

命令:SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS; 

mysql> select * from information_schema.INNODB_LOCK_WAITS\G
*************************** 1. row ***************************
requesting_trx_id: 17778 --请求锁的事务ID(等待方)
requested_lock_id: 17778:82:3:6 -- 请求锁ID
blocking_trx_id: 17773 -- 阻塞该锁的事务ID(当前持有方,待释放)
blocking_lock_id: 17773:82:3:6 -- 持有的锁ID

  

2、查询锁争用情况

1)查询表级锁争用情况

可以通过检查table_locks_waited和table_locks_immediate状态变量来分析表锁争夺情况。

mysql> show status like 'table%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| Table_locks_immediate | 6165 |
| Table_locks_waited | 0 |
| Table_open_cache_hits | 0 |
| Table_open_cache_misses | 0 |
| Table_open_cache_overflows | 0 |
+----------------------------+-------+

变量解释:

table_lock_waited 表示不能马上获得锁的数量

table_lock_immediate表示马上获得锁的数量

 

2)查看记录锁使用情况

mysql> show status like 'innodb_row_lock_%';
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| Innodb_row_lock_current_waits | 0 |
| Innodb_row_lock_time | 1896 |
| Innodb_row_lock_time_avg | 6 |
| Innodb_row_lock_time_max | 193 |
| Innodb_row_lock_waits | 274 |
+-------------------------------+-------+

变量解释:

Innodb_row_lock_current_waits:当前等待锁的数量  
Innodb_row_lock_time:系统启动到现在,锁定的总时间长度  
Innodb_row_lock_time_avg:每次平均锁定的时间  
Innodb_row_lock_time_max:最长一次锁定时间
Innodb_row_lock_waits:系统启动到现在总共锁定的次数

 

上一篇:mysql执行truncate drop 时卡死问题解决


下一篇:Mysql一分钟定位 Next-Key Lock,你需要几分钟