image.png

监控锁等待

  1. show status like 'innodb_row_lock%';
  2. --关注点:
  3. Innodb_row_lock_current_waits 当前有多少锁等待
  4. Innodb_row_lock_waits 系统启动以来一共发生过多少锁等待

查找被阻塞的事务

  1. select * from information_schema.innodb_trx where trx_state='lock wait';

查看锁源

  1. select * from sys.innodb_lock_waits;
  2. select locked_table,locked_type,waiting_trx_id,waiting_pid,waiting_query,waiting_lock_mode,blocking_trx_id,blocking_pid,sql_kill_blocking_connection from sys.innodb_lock_waits;
  3. locked_table 产生锁等待的表
  4. locked_type 锁的类型(recordlock,gaplock,nextlock
  5. waiting_trx_id 等待的事务ID
  6. waiting_pid 等待事务的连接线程ID
  7. waiting_query 等待事务语句
  8. waiting_lock_mode 等待锁的类型(X,S)
  9. blocking_trx_id 锁源的事务ID
  10. blocking_pid 锁源的事务连接线程ID
  11. sql_kill_blocking_connection 处理建议

根据锁源的pid,找到锁源SQL的线程id

  1. select * from performance_schema.threads where processlist_id=1691;

根据锁源sql线程id,找到锁源的sql语句

  1. select * from performance_schema.`events_statements_current` where thread_id=17176;

死锁的监控

  1. show variables like '%deadlock%';
  2. vi /etc/my.cnf
  3. innodb_print_all_deadlocks = 1

主从优化开启从库多线程MTS

  1. 必须是5.7以上的版本
  2. 必须开启GTID模式
  3. binlog必须时row模式
  4. my.cnf中加入:
  5. gtid_mode=ON
  6. enforce_gtid_consistency=ON
  7. log_slave_updates=ON
  8. slave-parallel-type=LOGICAL_CLOCK (默认时database库级别,改成时钟,事务级别)
  9. slave-parallel-workers=8 (cpu核心数/2)
  10. master_info_repository=TABLE
  11. relay_log_info_repository=TABLE
  12. relay_log_recovery=ON