(1) TRANSACTION: TRANSACTION 11500361, ACTIVE 1 sec fetching rows mysql tables in use 3, locked 3 LOCK WAIT 80 lock struct(s), heap size 13864, 801 row lock(s), undo log entries 1 MySQL thread id 159300, 〜 UPDATE xxxx SET xxx *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 443 page no 172 n bits 176 index〜 Record lock, heap no 49 PHYSICAL RECORD: n_fields 11; compact format; info bits 0 〜 *** (2) TRANSACTION: TRANSACTION 11547333, ACTIVE 1 sec starting index read mysql tables in use 3, locked 3 6 lock struct(s), heap size 1184, 6 row lock(s), undo log entries 5 MySQL thread id 1590063, OS thread handle 0x7fa60f28a700, 〜 UPDATE xxx SET xxx= '2', updated_at = '2017-XX-XX 20:18:12’ *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 443 page no 172 n bits 176 index 〜 Record lock, heap no 39 PHYSICAL RECORD: n_fields 11; compact format; info bits 0 〜 *** WE ROLL BACK TRANSACTION (2)
SET xxx *** (2) TRANSACTION: UPDATE xxx SET xxx= '2', updated_at = '2017-XX-XX 20:18:12’ *** (2) HOLDS THE LOCK(S): *** WE ROLL BACK TRANSACTION (2) SHOW ENGINE INNODB STATUS ぶつかったSQL 負けたほう。
TABLE test(col int) ENGINE=InnoDB; mysql> INSERT INTO test VALUES(1); mysql> INSERT INTO test VALUES(2); mysql> ALTER TABLE `test` ADD UNIQUE (`col`); mysql1> begin; mysql1> select * from test where col = 1 for update; mysql2> begin; mysql2> select * from test where col = 2 for update; mysql1> select * from test where col = 2 for update; mysql2> select * from deadlock_test where col = 1 for update; ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction - 準備 - コンソール2台で実行