zoukankan      html  css  js  c++  java
  • mysql锁产生和判断

    一、环境模拟

    mysql> show create table t1;
    +-------+-----------------------------------------------------------------------------------------------------------------------+
    | Table | Create Table |
    +-------+-----------------------------------------------------------------------------------------------------------------------+
    | t1 | CREATE TABLE `t1` (
    `id` int(11) NOT NULL,
    `name` varchar(50) DEFAULT NULL
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

    设置sql提交方式为手动commit,where条件没有索引

    二、锁出现模拟

    创建一下测试数据如下:

    root@localhost : mirror:43: >select * from t1;

    +------+--------+
    | id | name |
    +------+--------+
    | 1 | mirror |
    | 2 | mirror |
    | 3 | mirror |
    | 4 | mirror |
    +------+--------+
    4 rows in set (0.00 sec)

    session 1:更新id为1的数据不提交

    root@localhost : mirror:26: >update t1 set name='wang' where id=1;
    Query OK, 1 row affected (0.01 sec)
    Rows matched: 1 Changed: 1 Warnings: 0

     session 2:

    root@localhost : mirror:45: >update t1 set name='zhang' where id=1;

    session 3: 处理

    1.查看是否有表被锁

    root@localhost : (none):54: >show OPEN TABLES where In_use > 0;
    +----------+-------+--------+-------------+
    | Database | Table | In_use | Name_locked |
    +----------+-------+--------+-------------+
    | mirror | t1 | 1 | 0 |
    +----------+-------+--------+-------------+

    2.

    show engine innodb status \G

    LOCK WAIT 2 lock struct(s), heap size 1136, 13 row lock(s)
    MySQL thread id 12, OS thread handle 140687159236352, query id 686 localhost root updating
    update t1 set name='zhang' where id=1
    ------- TRX HAS BEEN WAITING 2 SEC FOR THIS LOCK TO BE GRANTED:
    RECORD LOCKS space id 26 page no 3 n bits 72 index GEN_CLUST_INDEX of table `mirror`.`t1` trx id 1327 lock_mode X waiting
    Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0
    0: len 6; hex 000000000208; asc ;;
    1: len 6; hex 000000000526; asc &;;
    2: len 7; hex 3a0000002b03d1; asc : + ;;
    3: len 4; hex 80000001; asc ;;
    4: len 4; hex 77616e67; asc wang;;------------------

    ---TRANSACTION 1318, ACTIVE 2856 sec
    3 lock struct(s), heap size 1136, 9 row lock(s), undo log entries 5
    MySQL thread id 11, OS thread handle 140687158425344, query id 154 localhost root   //也可以看到锁阻塞的sql 的id信息
    Trx read view will not see trx with id >= 1318, sees < 1318

    3.查看行锁的信息

    root@localhost : (none):38: >select * from sys.innodb_lock_waits\G
    *************************** 1. row ***************************
    wait_started: 2021-11-18 14:37:53
    wait_age: 00:00:46
    wait_age_secs: 46  //锁等待时间,等待锁的事务
    locked_table: `mirror`.`t1`
    locked_index: GEN_CLUST_INDEX
    locked_type: RECORD
    waiting_trx_id: 1327  
    waiting_trx_started: 2021-11-18 14:27:48
    waiting_trx_age: 00:10:51
    waiting_trx_rows_locked: 8
    waiting_trx_rows_modified: 0
    waiting_pid: 12  //等待的线程
    waiting_query: update t1 set name='zhang' where id=1
    waiting_lock_id: 1327:26:3:2
    waiting_lock_mode: X
    blocking_trx_id: 1318
    blocking_pid: 11
    blocking_query: NULL
    blocking_lock_id: 1318:26:3:2
    blocking_lock_mode: X
    blocking_trx_started: 2021-11-18 14:24:47
    blocking_trx_age: 00:13:52
    blocking_trx_rows_locked: 9
    blocking_trx_rows_modified: 5
    sql_kill_blocking_query: KILL QUERY 11   //阻塞的sql id
    sql_kill_blocking_connection: KILL 11   杀掉锁的事务(杀掉阻塞上面语句的sql)
    2 rows in set, 3 warnings (0.00 sec)

    可以通过 KILL 11 直接杀掉阻塞的线程。

    如果想知道哪条sql阻塞的,可以拿着sql_kill_blocking_connection的值11去threads查询

    4、确认哪条sql阻塞的别的sql

    root@localhost : (none):44: >select * from performance_schema.threads where processlist_id=11\G
    *************************** 1. row ***************************
    THREAD_ID: 36
    NAME: thread/sql/one_connection
    TYPE: FOREGROUND
    PROCESSLIST_ID: 11
    PROCESSLIST_USER: root    //哪个用户发出的
    PROCESSLIST_HOST: localhost  //哪个客户端发出的
    PROCESSLIST_DB: mirror  //哪个数据库
    PROCESSLIST_COMMAND: Sleep
    PROCESSLIST_TIME: 1031
    PROCESSLIST_STATE: NULL
    PROCESSLIST_INFO: update t1 set name='wang' where id=1  //可以确认是这条sql阻塞了其他sql
    PARENT_THREAD_ID: NULL
    ROLE: NULL
    INSTRUMENTED: YES
    HISTORY: YES
    CONNECTION_TYPE: Socket
    THREAD_OS_ID: 22349
    1 row in set (0.00 sec)

    ps:select * from performance_schema.metadata_locks;

    该条语句查看元数据锁,比如ddl锁

  • 相关阅读:
    JQ 鼠标滑过按钮改变背景图片
    TreeView 用法(有代码)
    MVC框架 IE浏览时IIS配置
    table 边框 不显示好td内为空串时,边框不显示
    DIV 内滚动条 样式的写法
    用 Grid 数据绑定
    架设支持Silverlight的Web服务器
    c# sql like @参数
    IE8 的margintop兼容问题
    弱符号与强符号
  • 原文地址:https://www.cnblogs.com/liuxiuxiu/p/15572509.html
Copyright © 2011-2022 走看看