前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL8.0锁情况排查

MySQL8.0锁情况排查

作者头像
GreatSQL社区
发布2023-02-22 10:40:19
1.6K0
发布2023-02-22 10:40:19
举报
文章被收录于专栏:GreatSQL出品技术文章

* GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。

在数据库中,除传统的计算资源(CPU、RAM、IO)的争用以外,数据也是一种供许多用户共享的资源。如何保证数据并发访问的一致性、有效性是所有数据库必须解决的一个问题,锁冲突也是影响数据库并发访问性能的一个重要因素。从这个角度来说,锁对教据库而言显得尤其重要,也更加复杂。

本文将通过实验介绍MySQL8.0版锁该如何排查,以及找到阻塞的sql语句,实验的MySQL版本为8.0.26,隔离级别为RR。

1.MySQL8.0版本锁情况排查核心表

代码语言:javascript
复制
information_schema.innodb_trx  ##正在运行的事务信息。
sys.innodb_lock_waits          ##处于锁等待的关联事务信息。
performance_schema.threads     ##SQL线程及线程号、进程号、OS线程号等信息

2.行锁监控语句及排查步骤

代码语言:javascript
复制
# 确认有没有锁等待:
show status like 'innodb_row_lock%';
select * from information_schema.innodb_trx;

# 查询锁等待详细信息
select * from sys.innodb_lock_waits; ----> blocking_pid(锁源的连接线程)

# 通过连接线程ID找SQL线程语句
select * from performance_schema.threads;

# 通过SQL线程找到SQL语句
select * from performance_schema.events_statements_history;

3.测试验证

代码语言:javascript
复制
mysql> use world;
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city            |
| country         |
| countrylanguage |
+-----------------+
3 rows in set (0.00 sec)
3.1 分别开启两个窗口(session1,session2)
代码语言:javascript
复制
s1:
# 加排他锁
mysql> begin;
mysql> select * from world.city where id=1 for update;

s2:
# 加排他锁
mysql> begin;
mysql> update city  set name='girl' where id=1;
执行完处于夯住状态,默认50秒会超时回滚。
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

mysql> set innodb_lock_wait_timeout=5000;    ##锁等待超时参数,这里设置为5000便于测试.
mysql> update city  set name='girl' where id=1;
3.2 再开一个窗口s3,查看锁状态
代码语言:javascript
复制
mysql> use information_schema;
mysql> select trx_id,trx_state,trx_started,trx_tables_locked,trx_rows_locked from innodb_trx\G
*************************** 1. row ***************************
           trx_id: 8995        ##刚刚运行的第二个语句事务ID
        trx_state: LOCK WAIT   ##处于锁等待状态
      trx_started: 2022-12-23 16:00:42
trx_tables_locked: 1          ##锁了1张表
  trx_rows_locked: 2     ##锁了2行
*************************** 2. row ***************************
           trx_id: 8994       ##刚刚运行的第一个语句事务ID
        trx_state: RUNNING    ##获得锁的状态
      trx_started: 2022-12-23 15:59:41
trx_tables_locked: 1
  trx_rows_locked: 1
2 rows in set (0.00 sec)

代码语言:javascript
复制

mysql> select * from sys.innodb_lock_waits\G
*************************** 1. row ***************************
                wait_started: 2022-12-23 16:01:57
                    wait_age: 00:00:52
               wait_age_secs: 52
                locked_table: `world`.`city`
         locked_table_schema: world
           locked_table_name: city
      locked_table_partition: NULL
   locked_table_subpartition: NULL
                locked_index: PRIMARY
                 locked_type: RECORD
              waiting_trx_id: 8995
         waiting_trx_started: 2022-12-23 16:00:42
             waiting_trx_age: 00:02:07
     waiting_trx_rows_locked: 2
   waiting_trx_rows_modified: 0
                 waiting_pid: 33
               waiting_query: update city  set name='girl' where id=1
             waiting_lock_id: 140190433225944:16:6:2:140190349859736
           waiting_lock_mode: X,REC_NOT_GAP
             blocking_trx_id: 8994   ##阻塞者事务ID
                blocking_pid: 32     ##阻塞者进程ID, show processlist可查;
              blocking_query: NULL
            blocking_lock_id: 140190433226752:16:6:2:140190349865536
          blocking_lock_mode: X,REC_NOT_GAP
        blocking_trx_started: 2022-12-23 15:59:41
            blocking_trx_age: 00:03:08
    blocking_trx_rows_locked: 1
  blocking_trx_rows_modified: 0
     sql_kill_blocking_query: KILL QUERY 32
sql_kill_blocking_connection: KILL 32   ##解锁方法
1 row in set (0.00 sec)
3.3 查看进程ID为32的进程,无法显示当前执行的SQL语句
代码语言:javascript
复制
mysql> show processlist;
+----+-----------------+-----------------+--------------------+---------+-------+------------------------+-----------------------------------------+
| Id | User            | Host            | db                 | Command | Time  | State                  | Info                                    |
+----+-----------------+-----------------+--------------------+---------+-------+------------------------+-----------------------------------------+
|  5 | event_scheduler | localhost       | NULL               | Daemon  | 27235 | Waiting on empty queue | NULL                                    |
| 29 | root            | localhost:43074 | information_schema | Query   |     0 | init                   | show processlist                        |
| 32 | root            | localhost:43080 | world              | Sleep   |   248 |                        | NULL                                    |
| 33 | root            | localhost:43082 | world              | Query   |   112 | updating               | update city  set name='girl' where id=1 |
+----+-----------------+-----------------+--------------------+---------+-------+------------------------+-----------------------------------------+
4 rows in set (0.00 sec)
3.4 查看进程ID为32的进程对应的SQL线程ID
代码语言:javascript
复制
mysql> select thread_id,processlist_id from performance_schema.threads where processlist_id=32;
+-----------+----------------+
| thread_id | processlist_id |
+-----------+----------------+
|        75 |             32 |
+-----------+----------------+
1 row in set (0.00 sec)

找出SQL线程ID为75

3.5 根据线程ID 75,找到真正执行的SQL语句
代码语言:javascript
复制
mysql> select thread_id,sql_text from performance_schema.events_statements_history where thread_id=75\G
*************************** 1. row ***************************
thread_id: 75
 sql_text: NULL
*************************** 2. row ***************************
thread_id: 75
 sql_text: NULL
*************************** 3. row ***************************
thread_id: 75
 sql_text: NULL
*************************** 4. row ***************************
thread_id: 75
 sql_text: show tables
*************************** 5. row ***************************
thread_id: 75
 sql_text: set autocommit=0
*************************** 6. row ***************************
thread_id: 75
 sql_text: begin
*************************** 7. row ***************************
thread_id: 75
 sql_text: select * from world.city where id=1 for update
*************************** 8. row ***************************
thread_id: 75
 sql_text: NULL
*************************** 9. row ***************************
thread_id: 75
 sql_text: show databases
*************************** 10. row ***************************
thread_id: 75
 sql_text: show tables
10 rows in set (0.00 sec)

找到select * from world.city where id=1 for update语句,确认后如果没问题可以kill掉。

3.6 处理锁源SQL对应的连接线程。
代码语言:javascript
复制
kill  32;
3.7 通过设置回滚申请锁的事务的时间,让处于等待的事务回滚,解决锁冲突。
代码语言:javascript
复制
set innodb_lock_wait_timeout=500;  #设置回滚申请锁的事务的时间。

4.innodb_lock_wait_timeout参数

innodb_lock_wait_timeout指的是事务等待获取资源等待的最长时间,超过这个时间还未分配到资源则会返回应用失败;

参数的时间单位是,最小可设置为1s,最大可设置1073741824秒(34年),默认安装时这个值是50s.

当锁等待超过设置时间的时候,就会报如下的错误;

代码语言:javascript
复制
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

参数支持范围为session和global,且支持动态修改,所以可以通过两种方法修改;

1.通过语句修改

代码语言:javascript
复制
set innodb_lock_wait_timeout=50;
set global innodb_lock_wait_timeout=50;
注意global的修改对当前线程是不生效的,只有建立新的连接才生效

2.修改参数文件/etc/my.cnf

代码语言:javascript
复制
innodb_lock_wait_timeout = 50

Enjoy GreatSQL :)


《深入浅出MGR》视频课程

https://www.bilibili.com/video/BV1Da411W7Va?spm_id_from=333.999.0.0&vd_source=ae1951b64ea7b9e6ba11f1d0bbcff0e4


文章推荐:


关于 GreatSQL

GreatSQL是由万里数据库维护的MySQL分支,专注于提升MGR可靠性及性能,支持InnoDB并行查询特性,是适用于金融级应用的MySQL分支版本。

GreatSQL社区官网: https://greatsql.cn/

Gitee: https://gitee.com/GreatSQL/GreatSQL

GitHub: https://github.com/GreatSQL/GreatSQL

Bilibili:

https://space.bilibili.com/1363850082/video

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2023-01-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 GreatSQL社区 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.MySQL8.0版本锁情况排查核心表
  • 2.行锁监控语句及排查步骤
  • 3.测试验证
    • 3.1 分别开启两个窗口(session1,session2)
      • 3.2 再开一个窗口s3,查看锁状态
        • 3.3 查看进程ID为32的进程,无法显示当前执行的SQL语句
          • 3.4 查看进程ID为32的进程对应的SQL线程ID
            • 3.5 根据线程ID 75,找到真正执行的SQL语句
              • 3.6 处理锁源SQL对应的连接线程。
                • 3.7 通过设置回滚申请锁的事务的时间,让处于等待的事务回滚,解决锁冲突。
                • 4.innodb_lock_wait_timeout参数
                相关产品与服务
                数据库
                云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档