查看Mysql正在执行的事务、锁、等待

  一、关于锁的三张表(MEMORY引擎)
  
  ## 当前运行的所有事务
  
  mysql> select * from information_schema.innodb_trx\G;
  
  *************************** 1. row ***************************
  
  trx_id: 613963
  
  trx_state: LOCK WAIT #事务状态
  
  trx_started: 2019-02-22 10:48:48
  
  trx_requested_lock_id: 613963:460:3:4
  
  trx_wait_started: 2019-02-22 11:08:41
  
  trx_weight: 2
  
  trx_mysql_thread_id: 140
  
  trx_query: update tx1 set c1=‘heyfffff‘,c2=‘heyffffff‘ where id =3
  
  trx_operation_state: starting index read
  
  trx_tables_in_use: 1
  
  trx_tables_locked: 1
  
  trx_lock_structs: 2
  
  trx_lock_memory_bytes: 1136
  
  trx_rows_locked: 7 #事务锁住的行数
  
  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
  
  *************************** 2. row ***************************
  
  trx_id: 613962
  
  trx_state: RUNNING
  
  trx_started: 2019-02-22 10:46:29
  
  trx_requested_lock_id: NULL
  
  trx_wait_started: NULL
  
  trx_weight: 3
  
  trx_mysql_thread_id: 138
  
  trx_query: select * from information_schema.innodb_trx
  
  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: 1
  
  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
  
  2 rows in set (0.00 sec)
  
  ## 当前出现的锁
  
  mysql> select * from information_schema.innodb_locks\G;
  
  *************************** 1. row ***************************
  
  lock_id: 613963:460:3:4
  
  lock_trx_id: 613963
  
  lock_mode: X
  
  lock_type: RECORD
  
  lock_table: `test`.`tx1`
  
  lock_index: PRIMARY
  
  lock_space: 460
  
  lock_page: 3
  
  lock_rec: 4
  
  lock_data: 3
  
  *************************** 2. row ***************************
  
  lock_id: 613962:460:3:4
  
  lock_trx_id: 613962
  
  lock_mode: X
  
  lock_type: RECORD
  
  lock_table: `test`.`tx1`
  
  lock_index: PRIMARY
  
  lock_space: 460
  
  lock_page: 3
  
  lock_rec: 4
  
  lock_data: 3
  
  2 rows in set, 1 warning (0.00 sec)
  
  ## 锁等待的对应关系
  
  mysql> select * from information_schema.innodb_lock_waits\G;
  
  *************************** 1. row ***************************
  
  requesting_trx_id: 613963
  
  requested_lock_id: 613963:460:3:4 #请求锁的锁ID
  
  blocking_trx_id: 613962 #当前拥有锁的事务ID
  
  blocking_lock_id: 613962:460:3:4
  
  1 row in set, 1 warning (0.00 sec)
  
  二、查看锁的情况
  
  mysql> show status like ‘innodb_row_lock_%‘;
  
  +-------------------------------+--------+
  
  | Variable_name | Value |
  
  +-------------------------------+--------+
  
  | Innodb_row_lock_current_waits | 1 |
  
  | Innodb_row_lock_time | 479764 |
  
  | Innodb_row_lock_time_avg | 39980 |
  
  | Innodb_row_lock_time_max | 51021 |
  
  | Innodb_row_lock_waits | 12 |
  
  +-------------------------------+--------+
  
  5 rows in set (0.00 sec)
  
  解释如下:
  
  Innodb_row_lock_current_waits : 当前等待锁的数量
  
  Innodb_row_lock_time : 系统启动到现在,锁定的总时间长度
  
  Innodb_row_lock_time_avg : 每次平均锁定的时间
  
  Innodb_row_lock_time_max : 最长一次锁定时间
  
  Innodb_row_lock_waits : 系统启动到现在总共锁定的次数
  
  # 查询是否锁表
  
  mysql> show OPEN TABLES where In_use > 0;
  
  +----------+-------+--------+-------------+
  
  | Database | Table | In_use | Name_locked |
  
  +----------+-------+--------+-------------+
  
  | test | tx1 | 1 | 0 |
  
  +----------+-------+--------+-------------+
  
  1 row in set (0.00 sec)
  
  三、杀死进程
  
  # 1.查询进程
  
  mysql> show processlist\G # \G 结构旋转90度变成纵向
  
  *************************** 1. row ***************************
  
  Id: 138
  
  User: root
  
  Host: localhost:55106
  
  db: test
  
  Command: Query
  
  Time: 0
  
  State: starting
  
  Info: show processlist
  
  *************************** 2. row ***************************
  
  Id: 140
  
  User: root
  
  Host: localhost:56158
  
  db: test
  
  Command: Sleep
  
  Time: 145
  
  State:
  
  Info: NULL
  
  2 rows in set (0.00 sec)
  
  2 rows in set (0.00 sec)
  
  # 2.杀死对应进程ID
  
  mysql> kill 140;
  
  Query OK, 0 rows affected (0.00 sec)
  
  mysql> show processlist\G
  
  *************************** 1. row ***************************
  
  <script src="https://www.zhenghongyule.com cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
  
  <link href="https://dasheng178.com maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  
  <script src="https://www.yongshiyule178.com cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
  
  <style>
  
  div.item img{
  
  width:100%;
  
  }
  
  div#carousel-example-generic{
  
  width:80%;
  
  margin:0 auto;
  
  }
  
  </style>
  
  <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
  
  <!-- 轮播指标 -->
  
  <ol class="carousel-indicators">
  
  <li data-target="#carousel-example-generic"www.yinxionghui1.com data-slide-to="0" class="active"></li>
  
  <li data-target="#carousel-example-generic" www.yigouylpt2.cn/ data-slide-to="1"></li>
  
  <li data-target="#carousel-example-generic"www.zhongxinyul2.com data-slide-to="2"></li>
  
  <li data-target="#carousel-example-generic"www.xtd912.com data-slide-to="3"></li>
  
  </ol>
  
  <!-- 轮播的幻灯片项目 -->
  
  <div class="carousel-inner" role="listbox">
  
  <div class="item active">
  
  <img src="http://pic1.win4000.com/wallpaper/2019-02-21/5c6e434e8d2fb.jpg" >
  
  </div>
  
  <div class="item">
  
  <img src="http://pic1.win4000.com/wallpaper/2019-02-11/5c6137ee00320.jpg" >
  
  </div>
  
  <div class="item">
  
  <img src="http://pic1.win4000.com/wallpaper/2019-01-27/5c4d7aa191adb.jpg" >
  
  </div>
  
  <div class="item">
  
  <img src="http://pic1.win4000.com/wallpaper/2019-01-27/5c4d7a9f6030c.jpg" >
  
  </div>
  
  </div>
  
  Id: 138
  
  User: root
  
  Host: localhost:55106
  
  db: test
  
  Command: Query
  
  Time: 0
  
  State: starting
  
  Info: show processlist
  
  1 row in set (0.00 sec)
  
  四、SQL分析
  
  描述MySQL如何执行查询操作、以及MySQL成功返回结果集需要执行的行数。
  
  0. 1.EXPLAIN tbl_name 或 EXPLAIN [EXTENDED] SELECT select_options
  
  2.
  
  五、数据库的一些默认设置
  
  # 查看数据库默认存储引擎
  
  mysql> show engines;
  
  +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
  
  | Engine | Support | Comment | Transactions | XA | Savepoints |
  
  +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
  
  | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
  
  | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
  
  | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
  
  | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
  
  | MyISAM | YES | MyISAM storage engine | NO | NO | NO |
  
  | CSV | YES | CSV storage engine | NO | NO | NO |
  
  | ARCHIVE | YES | Archive storage engine | NO | NO | NO |
  
  | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
  
  | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
  
  +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
  
  9 rows in set (0.00 sec)
  
  # 查看当前数据库默认隔离级别
  
  mysql> select @@global.tx_isolation;
  
  +-----------------------+
  
  | @@global.tx_isolation |
  
  +-----------------------+
  
  | REPEATABLE-READ |
  
  +-----------------------+
  
  1 row in set, 1 warning (0.00 sec)
  
  # 默认自动提交事务
  
  mysql> show global variables like ‘autocommit‘;
  
  +---------------+-------+
  
  | Variable_name | Value |
  
  +---------------+-------+
  
  | autocommit | ON |
  
  +---------------+-------+
  
  1 row in set (0.00 sec)
  
  mysql> select @@global.autocommit;
  
  +---------------------+
  
  | @@global.autocommit |
  
  +---------------------+
  
  | 1 |
  
  +---------------------+
  
  1 row in set (0.00 sec)

原文地址:https://www.cnblogs.com/qwangxiao/p/10417401.html

时间: 2024-10-21 05:42:18

查看Mysql正在执行的事务、锁、等待的相关文章

查看MySQL记录执行过的SQL

第一种:查Slow query的SQL语法: log_slow_queries = /var/log/mysql/mysql-slow.log long_query_time = 2 (超过2秒的SQL语法记录起来,设短一点来记录除错也是一种方法.) 第二种:设MySQL Replication用binlog: log_bin = /var/log/mysql/mysql-bin.log (此档要用mysqlbinlog解来看, mysqlbinlog mysql-bin.000042| gre

mysql外键引发的锁等待

有这样两条sql: insert table_a (bId) value(1); -- sql-1  update table_b set b.xx=123 where b.id =1; -- sql-2 其中,table_a的字段bId是个外键:外键关联的正是table_b的id字段. 在mysql上执行这两条数据时,sql-1会锁住sql-2.我们的系统中,为这一个锁,发生了不知道多少的锁等待,更引发了不知道多少的死锁. 特此备忘.

查看Mysql实时执行的Sql语句

最近给客户开发了基于Asp.Net mvc5 +Mysql+EF的项目,但是在EF里无法看到Mysql执行的语句 之前也找到一些监控Mysql的软件但一直没有用起来,现在又遇到了问题即在EF里Mysal的查询没有结果而在Mysql里没有问题 因为不知道EF生成的Mysql语句所以不知道是不是Sql的问题于是决定必须解决此问题 通过半天时间的努力解决了此问题,其实很简单即开启Mysql的Log功能,那么Mysql的执行语句都会写到Log文件里 然后通过BareTail这个专门查看Log文件的工具即

查看mysql哪些表正在被锁状态

SHOW PROCESSLIST查看数据库中表的状态,是否被锁 select * from t1  where uid='xxxx' for update    //在有索引(例如uid)的情况下是行锁,否则是表锁

使用explain查看mysql查询执行计划

explain语句: 字段解释: type: all(全表扫描) ref() possible_keys: 预测使用什么列做为索引 key: 实际使用的key ref: 参考,引用 rows: 扫描的行数 id: 扫描的行数 extra: 使用什么来定位

mysql general log 查看mysql 执行历史

我们有时候需要查看mysql的执行历史,比如我们做sql优化的时候,起码要知道执行的sql是什么,框架一般会帮我们拼装sql,所以在程序中不一定能够打印出sql,这个时候就需要mysql的general log了. 查看设置mysql genneral log show VARIABLES like '%general_log%'; set GLOBAL general_log = off;// on-打开; off-关闭 general_log ON general_log_file /var

MySql命令行控制事务

新建表t1 CREATE TABLE `t1` (   `a` int(11) NOT NULL,   `b` int(11) DEFAULT NULL,   PRIMARY KEY (`a`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 查看mysql系统级别的事务隔离级别: mysql> SELECT @@global.tx_isolation; +-----------------------+ | @@global.tx_isolation | +--

mysql事务未提交导致锁等待如何解决

1.实验环境 Myql版本5.7.17-log 实验表结构 ([email protected])[apex]> show create table test; +-------+-----------------------------------------------------------------------------------------------------------------------------------+ | Table| Create Table      

mysql死锁,等待资源,事务锁,Lock wait timeout exceeded; try restarting transaction解决

前面已经了解了InnoDB关于在出现锁等待的时候,会根据参数innodb_lock_wait_timeout的配置,判断是否需要进行timeout的操作,本文档介绍在出现锁等待时候的查看及分析处理: 在InnoDB Plugin之前,一般通过show full processlist(很难发现被锁的行记录问题所在)和show engine innodb status命令查看当前的数据库请求,然后再判断当前事务中锁的情况.随着mysql的发展,已经提供更加便捷的方法来监控数据库中的锁等待现象了.