root@leyou:~# mysql -uroot -pxxx log
mysql> show variables like '%innodb_lock_wait_timeout%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| innodb_lock_wait_timeout | 50 |
+--------------------------+-------+
1 row in set (0.00 sec)
mysql>
mysql> set global innodb_lock_wait_timeout=300;
Query OK, 0 rows affected (0.00 sec)
session1:
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> update a set name='x' where> Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
然后什么也不动,去session2:
session2:
mysql> update a set name='z' where> 会一直等待
session3:
mysql> show full processlist ;
+---------+------+---------------------+--------------------+---------+--------+----------+----------------------------------+
|> +---------+------+---------------------+--------------------+---------+--------+----------+----------------------------------+ |
| 1720902 | root | localhost | log | Sleep | 977 | | NULL |
| 1720906 | root | localhost | log | Query | 294 | Updating | update a set name='z' where> | 1720907 | root | localhost | information_schema | Query | 0 | NULL | show full processlist |
+---------+------+---------------------+--------------------+---------+--------+----------+----------------------------------+
8 rows in set (0.00 sec)
mysql> show OPEN TABLES where In_use > 0;
+----------+-------+--------+-------------+
| Database | Table | In_use | Name_locked |
+----------+-------+--------+-------------+
| log | a | 1 | 0 |
+----------+-------+--------+-------------+
1 row in set (0.00 sec)
可以看到是log库的表a现在被占用。
继续查看:
mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;
+--------------------+-------------+-----------+-----------+------------+-------------------+------------+-----------+----------+----------------+
| lock_id | lock_trx_id | lock_mode | lock_type | lock_table | lock_index | lock_space | lock_page | lock_rec | lock_data |
+--------------------+-------------+-----------+-----------+------------+-------------------+------------+-----------+----------+----------------+
| EB9B45A:116492:3:2 | EB9B45B | X | RECORD | `log`.`a` | `GEN_CLUST_INDEX` | 116492 | 3 | 2 | 0x0000B83D5770 |
| EB9B458:116492:3:2 | EB9B458 | X | RECORD | `log`.`a` | `GEN_CLUST_INDEX` | 116492 | 3 | 2 | 0x0000B83D5770 |
+--------------------+-------------+-----------+-----------+------------+-------------------+------------+-----------+----------+----------------+
2 rows in set (0.00 sec)
有两个锁,事物号分别是EB9B45B 以及EB9B458 ,锁的模式都是X,及排它锁,都是锁住了表a,的lock_space 116492 第3页。
查看正在等待锁的表:
mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;
+-------------------+--------------------+-----------------+--------------------+
| requesting_trx_id | requested_lock_id | blocking_trx_id | blocking_lock_id |
+-------------------+--------------------+-----------------+--------------------+
| EB9B45B | EB9B45B:116492:3:2 | EB9B458 | EB9B458:116492:3:2 |
+-------------------+--------------------+-----------------+--------------------+
1 row in set (0.00 sec)
这个事物号:EB9B45B 还在等待行级排它锁被grant,所以就需要kill掉EB9B458的pid
再查看另一个视图:
mysql> SELECT * FROM information_schema.INNODB_TRX\G;
*************************** 1. row ***************************
trx_id: EB9B45B
trx_state: LOCK WAIT
trx_started: 2016-11-15 10:11:25
trx_requested_lock_id: EB9B45B:116492:3:2
trx_wait_started: 2016-11-15 10:11:25
trx_weight: 2
trx_mysql_thread_id: 1720906
trx_query: update a set name='z' where> trx_operation_state: starting index read
trx_tables_in_use: 1
trx_tables_locked: 1
trx_lock_structs: 2
trx_lock_memory_bytes: 376
trx_rows_locked: 1
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: 10000
*************************** 2. row ***************************
trx_id: EB9B458
trx_state: RUNNING
trx_started: 2016-11-15 10:00:02
trx_requested_lock_id: NULL
trx_wait_started: NULL
trx_weight: 3
trx_mysql_thread_id: 1720902
trx_query: NULL
trx_operation_state: NULL
trx_tables_in_use: 0
trx_tables_locked: 0
trx_lock_structs: 2
trx_lock_memory_bytes: 376
trx_rows_locked: 7
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: 10000
2 rows in set (0.00 sec)
可以看到更详细的锁表情况,
trx_id: EB9B458 pid为1720902 的,状态 trx_state: RUNNING,kill掉这个pid就释放了排它锁。
此外,用mysql> show engine innodb status\G; 也可以看到结束了但是未提交的事物:
关键信息如下:
在------------
TRANSACTIONS
------------
这一栏查看:
------------------
---TRANSACTION EB9B458, ACTIVE 1978 sec
2 lock struct(s), heap>
MySQL thread> --------
FILE I/O
--------
EB9B458这个事物号在等待1720902 这个pid结束进程。
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com