命令

5.7

1、查看正在进行中的事务

mysql> select * from information_schema.innodb_trx\G
*************************** 1. row ***************************
                    trx_id: 139926
                 trx_state: RUNNING
               trx_started: 2023-05-27 20:45:08
     trx_requested_lock_id: NULL
          trx_wait_started: NULL
                trx_weight: 2
       trx_mysql_thread_id: 347
                 trx_query: NULL
       trx_operation_state: NULL
         trx_tables_in_use: 0
         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
1 row in set (0.00 sec)

2、查看正在锁的事务

mysql> select * from information_schema.innodb_locks\G
*************************** 1. row ***************************
    lock_id: 139929:340:4:1
lock_trx_id: 139929
  lock_mode: X
  lock_type: RECORD
 lock_table: `230510`.`t_order`
 lock_index: index_order
 lock_space: 340
  lock_page: 4
   lock_rec: 1
  lock_data: supremum pseudo-record
*************************** 2. row ***************************
    lock_id: 139926:340:4:1
lock_trx_id: 139926
  lock_mode: X
  lock_type: RECORD
 lock_table: `230510`.`t_order`
 lock_index: index_order
 lock_space: 340
  lock_page: 4
   lock_rec: 1
  lock_data: supremum pseudo-record
2 rows in set, 1 warning (0.00 sec)

3、查看等待锁的事务

mysql> select * from information_schema.innodb_lock_waits\G
*************************** 1. row ***************************
requesting_trx_id: 139929
requested_lock_id: 139929:340:4:1
  blocking_trx_id: 139926
 blocking_lock_id: 139926:340:4:1
1 row in set, 1 warning (0.00 sec)

4、查询是否锁表

mysql> show open tables where in_use > 0;
+----------+---------+--------+-------------+
| Database | Table   | In_use | Name_locked |
+----------+---------+--------+-------------+
| 230510   | t_order |      1 |           0 |
+----------+---------+--------+-------------+
1 row in set (0.00 sec)

在发生死锁时,这几种方式都可以查询到和当前死锁相关的信息。

5、查看最近死锁的日志

show engine innodb status;

6、解除死锁

如果需要解除死锁,有一种最简单粗暴的方式,那就是找到进程id之后,直接干掉。

查看当前正在进行中的进程

show processlist;

-- 也可以使用
select * from information_schema.innodb_trx\G

杀掉进程对应的进程 id(进程id对应上面第一条命令的id和第二条命令的trx_mysql_thread_id):

kill <id|trx_mysql_thread_id>;

验证(kill后再看是否还有锁):

show open tables where in_use > 0;

8.0

  • 查看死锁
    select * from performance_schema.data_locks;

  • 查看死锁等待时间
    select * from performance_schema.data_lock_waits;

拓展

\G的作用

在MySQL中,使用\G语句终止符可以使查询结果以列的方式显示,而不是默认的行的方式。例如,普通的查询结果显示为:

mysql> SELECT * FROM test;
+----+-------+
| id | name  |
+----+-------+ 
|  1 | a     |
|  2 | b     |
|  3 | c     |
+----+-------+
3 rows in set (0.00 sec)

使用 \G 语句终止符后,查询结果显示为:

mysql> SELECT * FROM test\G;
*************************** 1. row ***************************
      id: 1
     name: a     
*************************** 2. row ***************************
      id: 2
     name: b  
*************************** 3. row ***************************
       id: 3  
     name: c
3 rows in set (0.00 sec)

使用 \G 后就不能再加分号 ;,因为 \G 在功能上等同于 ;,如果加了分号,那么就是 ;;(2 个分号),会报SQL语法错误 No query specified

mysql> select * from t_order where id=1\G;
*************************** 1. row ***************************
         id: 1
   order_no: 1001
create_date: NULL
1 row in set (0.00 sec)

ERROR:
No query specified

YOLO