session 1:更新记录
Query OK, 0 rows affected (0.01 sec)
mysql> update t1 set email='test@test.com' where id=0;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4
session 2也更新相同的记录,出现等待
Query OK, 0 rows affected (0.00 sec)
mysql> update t1 set email='abc' where id=0;
session 3:查看系统等待事件:
+-------------------------------+---------+
| Variable_name
+-------------------------------+---------+
| Com_lock_tables
| Com_unlock_tables
| Innodb_row_lock_current_waits | 1
| Innodb_row_lock_time
| Innodb_row_lock_time_avg
| Innodb_row_lock_time_max
| Innodb_row_lock_waits
| Key_blocks_not_flushed
| Key_blocks_unused
| Key_blocks_used
| Qcache_free_blocks
| Qcache_total_blocks
| Table_locks_immediate
| Table_locks_waited
+-------------------------------+---------+
14 rows in set (0.01 sec)
session 1:提交记录
Query OK, 0 rows affected (0.01 sec)
session 2:update立刻完成
Query OK, 4 rows affected (2 min 43.44 sec)
Rows matched: 4
session 3:再次查看系统等待事件
+-------------------------------+---------+
| Variable_name
+-------------------------------+---------+
| Com_lock_tables
| Com_unlock_tables
| Innodb_row_lock_current_waits | 0
| Innodb_row_lock_time
| Innodb_row_lock_time_avg
| Innodb_row_lock_time_max
| Innodb_row_lock_waits
| Key_blocks_not_flushed
| Key_blocks_unused
| Key_blocks_used
| Qcache_free_blocks
| Qcache_total_blocks
| Table_locks_immediate
| Table_locks_waited
+-------------------------------+---------+
14 rows in set (0.01 sec)
查询会话session 1,session 2的连接ID
mysql> status;
--------------
mysql
Connection id:
session 2:
mysql> status;
--------------
mysql
Connection id:
在上面的session 1尚没有提交的时候,可以执行下列命令,查看一些事务阻塞信息
------------
TRANSACTIONS
------------
Trx id counter 0 3852351
Purge done for trx's n:o < 0 3852350 undo n:o < 0 0
History list length 11
Total number of lock structs in row lock hash table 7
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 0, not started, OS thread id 15
MySQL thread id 18, query id 2071119 localhost root
show innodb status
---TRANSACTION 0 3852350, ACTIVE 6 sec, OS thread id 14 starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 320
MySQL thread id 13, query id 2071118 localhost test Updating
update t1 set email='abc' where id=0
------- TRX HAS BEEN WAITING 6 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 32782 n bits 1056 index `idx_t1_id` of table `dc_test/t1` trx id 0 3852350 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
------------------
---TRANSACTION 0 3852348, ACTIVE 391 sec, OS thread id 12
7 lock struct(s), heap size 1024, undo log entries 4
MySQL thread id 15, query id 2071117 localhost test
当出现行锁时,谁等待谁?在哪里得到此信息,由于mysql采用的是线程机制,如何kill阻塞者,不知道在Mysql数据库里怎么操作?