在数据库中,情况除传统的排查计算资源(CPU、RAM、情况IO)的排查争用以外,数据也是情况一种供许多用户共享的资源。如何保证数据并发访问的排查一致性、有效性是情况所有数据库必须解决的b2b供应网一个问题,锁冲突也是排查影响数据库并发访问性能的一个重要因素。从这个角度来说,情况锁对教据库而言显得尤其重要,排查也更加复杂。情况 本文将通过实验介绍MySQL8.0版锁该如何排查,排查以及找到阻塞的情况sql语句,实验的排查MySQL版本为8.0.26,香港云服务器隔离级别为RR。情况  1.MySQL8.0版本锁情况排查核心表 复制information_schema.innodb_trx ##正在运行的事务信息。 sys.innodb_lock_waits ##处于锁等待的关联事务信息。 performance_schema.threads ##SQL线程及线程号、进程号、OS线程号等信息1.2.3. 2.行锁监控语句及排查步骤 复制# 确认有没有锁等待: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;1.2.3.4.5.6.7.8.9.10.11.12. 3.测试验证 复制mysql> use world;mysql> show tables;+-----------------+| Tables_in_world |+-----------------+| city || country || countrylanguage |+-----------------+3 rows in set (0.00 sec)1.2.3.4.5.6.7.8.9.10. 3.1 分别开启两个窗口(session1,session2) 复制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;1.2.3.4.5.6.7.8.9.10.11.12.13.14. 3.2 再开一个窗口s3,查看锁状态 复制mysql> use information_schema;mysql> select trx_id,trx_state,trx_started,trx_tables_locked,trx_rows_locked from innodb_trx\G 亿华云计算 |