mysql死锁

mysql死锁:


1、查询是否锁表
show OPEN TABLES where In_use > 0;
 
2、查询进程
    show processlist

  查询到相对应的进程===然后 kill    id


#查看正在锁的事务# 
SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX


#查看等待锁的事务#
SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;


如何查看MySQL数据库的死锁信息
 mysql> show engine innodb status;


找到LATEST DETECTED DEADLOCK这一行



mysql> show engine innodb status;

LATEST DETECTED DEADLOCK
------------------------
160323 16:01:19
*** (1) TRANSACTION:
TRANSACTION 26CD, ACTIVE 61 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 3 lock struct(s), heap size 1248, 2 row lock(s), undo log entries 1
MySQL thread id 10218, OS thread handle 0x7f0ae41fa700, query id 4002030 123.56.118.135 root Updating
update diaocha_user set sex='b' where id=74
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 414 n bits 168 index `PRIMARY` of table `app`.`diaocha_user` trx id 26CD lock_mode X locks rec but not gap waiting
Record lock, heap no 100 PHYSICAL RECORD: n_fields 18; compact format; info bits 0
 0: len 4; hex 8000004a; asc    J;;
 1: len 6; hex 0000000026ce; asc     & ;;
 2: len 7; hex 35000002b20f74; asc 5     t;;
 3: len 1; hex 62; asc b;;
 4: SQL NULL;
 5: SQL NULL;
 6: SQL NULL;
 7: SQL NULL;
 8: SQL NULL;
 9: SQL NULL;
 10: SQL NULL;
 11: SQL NULL;
 12: len 0; hex ; asc ;;
 13: len 0; hex ; asc ;;
 14: len 0; hex ; asc ;;
 15: SQL NULL;
 16: SQL NULL;
 17: SQL NULL;

*** (2) TRANSACTION:
TRANSACTION 26CE, ACTIVE 51 sec starting index read
mysql tables in use 1, locked 1
3 lock struct(s), heap size 1248, 2 row lock(s), undo log entries 1
MySQL thread id 10219, OS thread handle 0x7f0ae412a700, query id 4002052 123.56.118.135 root Updating
update diaocha_user set sex='b' where id=73
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 0 page no 414 n bits 168 index `PRIMARY` of table `app`.`diaocha_user` trx id 26CE lock_mode X locks rec but not gap
Record lock, heap no 100 PHYSICAL RECORD: n_fields 18; compact format; info bits 0
 0: len 4; hex 8000004a; asc    J;;
 1: len 6; hex 0000000026ce; asc     & ;;
 2: len 7; hex 35000002b20f74; asc 5     t;;
 3: len 1; hex 62; asc b;;
 4: SQL NULL;
 5: SQL NULL;
 6: SQL NULL;
 7: SQL NULL;
 8: SQL NULL;
 9: SQL NULL;
 10: SQL NULL;
 11: SQL NULL;
 12: len 0; hex ; asc ;;
 13: len 0; hex ; asc ;;
 14: len 0; hex ; asc ;;
 15: SQL NULL;
 16: SQL NULL;
 17: SQL NULL;

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 414 n bits 168 index `PRIMARY` of table `app`.`diaocha_user` trx id 26CE lock_mode X locks rec but not gap waiting
Record lock, heap no 99 PHYSICAL RECORD: n_fields 18; compact format; info bits 0
 0: len 4; hex 80000049; asc    I;;
 1: len 6; hex 0000000026cd; asc     & ;;
 2: len 7; hex 340000023e06bc; asc 4   >  ;;
 3: len 1; hex 62; asc b;;
 4: SQL NULL;
 5: SQL NULL;
 6: SQL NULL;
 7: SQL NULL;
 8: SQL NULL;
 9: SQL NULL;
 10: SQL NULL;
 11: SQL NULL;
 12: len 0; hex ; asc ;;
 13: len 0; hex ; asc ;;
 14: len 0; hex ; asc ;;
 15: SQL NULL;
 16: SQL NULL;
 17: SQL NULL;

*** WE ROLL BACK TRANSACTION (2)





相同表,

a命令行

mysql> set autocommit=off
;
Query OK, 0 rows affected
mysql> update diaocha_user set sex='b' where id=74;
Database changed
Rows matched: 1  Changed: 1  Warnings: 0
mysql> update diaocha_user set sex='b' where id=73;
Database changed
Rows matched: 1  Changed: 0  Warnings: 0
mysql> 


b命令行

mysql>  set autocommit=off;
Query OK, 0 rows affected

mysql> update diaocha_user set sex='c' where id=73;
Database changed
Rows matched: 1  Changed: 1  Warnings: 0
mysql> update diaocha_user set sex='c' where id=74;
1213 - Deadlock found when trying to get lock; try restarting transaction
mysql> 

a命令行执行更新id=74 

b命令行执行更新id=73


然后

b命令行执行更新id=74 

a命令行执行更新id=73

死锁出来了。。。。。



  
  死锁生成原因:
 http://www.cnblogs.com/LBSer/p/5183300.htm

你可能感兴趣的:(数据库)