mysql> show variables like 'transaction_isolation'; +-----------------------+----------------+ | Variable_name | Value | +-----------------------+----------------+ | transaction_isolation | READ-COMMITTED | +-----------------------+----------------+ 1 row in set (0.00 sec)
# 设置参数innodb_status_output_locks=on,否则看不到IX锁 mysql> show variables like 'innodb_status_output_locks'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | innodb_status_output_locks | ON | +----------------------------+-------+ 1 row in set (0.00 sec)
mysql> select * from t where id > 3 and id < 6 for update; +------+------+ | id | sal | +------+------+ | 4 | 400 | | 5 | 500 | +------+------+ 2 rows in set (0.00 sec)
mysql> show engine innodb status; ... ------------ TRANSACTIONS ------------ Trx id counter 6699539 Purge done for trx's n:o < 6699526 undo n:o < 0 state: running but idle History list length 3 LIST OF TRANSACTIONS FOR EACH SESSION: ---TRANSACTION 421163334236736, not started 0 lock struct(s), heap size 1136, 0 row lock(s) ---TRANSACTION 421163334235880, not started 0 lock struct(s), heap size 1136, 0 row lock(s) ---TRANSACTION 6699538, ACTIVE 3 sec 2 lock struct(s), heap size 1136, 2 row lock(s) MySQL thread id 9, OS thread handle 139688312157952, query id 36 localhost root starting show engine innodb status TABLE LOCK table `ds0`.`t` trx id 6699538 lock mode IX RECORD LOCKS space id 8692 page no 4 n bits 80 index GEN_CLUST_INDEX of table `ds0`.`t` trx id 6699538 lock_mode X locks rec but not gap Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b903; asc H ;; 1: len 6; hex 000000663621; asc f6!;; 2: len 7; hex 82000001170110; asc ;; 3: len 4; hex 80000004; asc ;; 4: len 4; hex 80000190; asc ;;
Record lock, heap no 6 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b904; asc H ;; 1: len 6; hex 000000663622; asc f6";; 2: len 7; hex 810000010e0110; asc ;; 3: len 4; hex 80000005; asc ;; 4: len 4; hex 800001f4; asc ;;
mysql> select * from t where id = 7 for update; # 等待锁
mysql> show engine innodb status; ...
------------ TRANSACTIONS ------------ Trx id counter 6699540 Purge done for trx's n:o < 6699526 undo n:o < 0 state: running but idle History list length 0 LIST OF TRANSACTIONS FOR EACH SESSION: ---TRANSACTION 421163334236736, not started 0 lock struct(s), heap size 1136, 0 row lock(s) ---TRANSACTION 421163334235880, not started 0 lock struct(s), heap size 1136, 0 row lock(s) ---TRANSACTION 6699539, ACTIVE 10 sec fetching rows mysql tables in use 1, locked 1 LOCK WAIT 3 lock struct(s), heap size 1136, 1 row lock(s) MySQL thread id 11, OS thread handle 139688309319424, query id 78 localhost root executing select * from t where id = 7 for update ------- TRX HAS BEEN WAITING 10 SEC FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 8692 page no 4 n bits 80 index GEN_CLUST_INDEX of table `ds0`.`t` trx id 6699539 lock_mode X locks rec but not gap waiting Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b903; asc H ;; 1: len 6; hex 000000663621; asc f6!;; 2: len 7; hex 82000001170110; asc ;; 3: len 4; hex 80000004; asc ;; 4: len 4; hex 80000190; asc ;;
------------------ TABLE LOCK table `ds0`.`t` trx id 6699539 lock mode IX RECORD LOCKS space id 8692 page no 4 n bits 80 index GEN_CLUST_INDEX of table `ds0`.`t` trx id 6699539 lock_mode X locks rec but not gap RECORD LOCKS space id 8692 page no 4 n bits 80 index GEN_CLUST_INDEX of table `ds0`.`t` trx id 6699539 lock_mode X locks rec but not gap waiting Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b903; asc H ;; 1: len 6; hex 000000663621; asc f6!;; 2: len 7; hex 82000001170110; asc ;; 3: len 4; hex 80000004; asc ;; 4: len 4; hex 80000190; asc ;;
---TRANSACTION 6699538, ACTIVE 523 sec 2 lock struct(s), heap size 1136, 2 row lock(s) MySQL thread id 9, OS thread handle 139688312157952, query id 79 localhost root starting show engine innodb status TABLE LOCK table `ds0`.`t` trx id 6699538 lock mode IX RECORD LOCKS space id 8692 page no 4 n bits 80 index GEN_CLUST_INDEX of table `ds0`.`t` trx id 6699538 lock_mode X locks rec but not gap Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b903; asc H ;; 1: len 6; hex 000000663621; asc f6!;; 2: len 7; hex 82000001170110; asc ;; 3: len 4; hex 80000004; asc ;; 4: len 4; hex 80000190; asc ;;
Record lock, heap no 6 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b904; asc H ;; 1: len 6; hex 000000663622; asc f6";; 2: len 7; hex 810000010e0110; asc ;; 3: len 4; hex 80000005; asc ;; 4: len 4; hex 800001f4; asc ;; ...
mysql> show engine innodb status; ------------ TRANSACTIONS ------------ Trx id counter 6699544 Purge done for trx's n:o < 6699543 undo n:o < 0 state: running but idle History list length 0 LIST OF TRANSACTIONS FOR EACH SESSION: ---TRANSACTION 421163334239304, not started 0 lock struct(s), heap size 1136, 0 row lock(s) ---TRANSACTION 421163334238448, not started 0 lock struct(s), heap size 1136, 0 row lock(s) ---TRANSACTION 421163334236736, not started 0 lock struct(s), heap size 1136, 0 row lock(s) ---TRANSACTION 421163334235880, not started 0 lock struct(s), heap size 1136, 0 row lock(s) ---TRANSACTION 6699543, ACTIVE 14 sec 2 lock struct(s), heap size 1136, 10 row lock(s) MySQL thread id 9, OS thread handle 139688312157952, query id 350 localhost root starting show engine innodb status TABLE LOCK table `ds0`.`t` trx id 6699543 lock mode IX RECORD LOCKS space id 8692 page no 4 n bits 80 index GEN_CLUST_INDEX of table `ds0`.`t` trx id 6699543 lock_mode X locks rec but not gap Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b900; asc H ;; 1: len 6; hex 00000066361a; asc f6 ;; 2: len 7; hex 810000011a0110; asc ;; 3: len 4; hex 80000001; asc ;; 4: len 4; hex 80000064; asc d;;
Record lock, heap no 3 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b901; asc H ;; 1: len 6; hex 00000066361b; asc f6 ;; 2: len 7; hex 820000010b0110; asc ;; 3: len 4; hex 80000002; asc ;; 4: len 4; hex 800000c8; asc ;;
Record lock, heap no 4 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b902; asc H ;; 1: len 6; hex 000000663620; asc f6 ;; 2: len 7; hex 810000010c0110; asc ;; 3: len 4; hex 80000003; asc ;; 4: len 4; hex 8000012c; asc ,;;
Record lock, heap no 5 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b903; asc H ;; 1: len 6; hex 000000663621; asc f6!;; 2: len 7; hex 82000001170110; asc ;; 3: len 4; hex 80000004; asc ;; 4: len 4; hex 80000190; asc ;;
Record lock, heap no 6 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b904; asc H ;; 1: len 6; hex 000000663622; asc f6";; 2: len 7; hex 810000010e0110; asc ;; 3: len 4; hex 80000005; asc ;; 4: len 4; hex 800001f4; asc ;;
Record lock, heap no 7 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b905; asc H ;; 1: len 6; hex 000000663623; asc f6#;; 2: len 7; hex 820000010d0110; asc ;; 3: len 4; hex 80000006; asc ;; 4: len 4; hex 80000258; asc X;;
Record lock, heap no 8 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b906; asc H ;; 1: len 6; hex 000000663624; asc f6$;; 2: len 7; hex 810000010f0110; asc ;; 3: len 4; hex 80000007; asc ;; 4: len 4; hex 800002bc; asc ;;
Record lock, heap no 9 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b907; asc H ;; 1: len 6; hex 000000663625; asc f6%;; 2: len 7; hex 820000010e0110; asc ;; 3: len 4; hex 80000008; asc ;; 4: len 4; hex 80000320; asc ;;
Record lock, heap no 10 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b908; asc H ;; 1: len 6; hex 000000663626; asc f6&;; 2: len 7; hex 81000001100110; asc ;; 3: len 4; hex 80000009; asc ;; 4: len 4; hex 80000384; asc ;;
Record lock, heap no 11 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b909; asc H ;; 1: len 6; hex 000000663627; asc f6';; 2: len 7; hex 820000010f0110; asc ;; 3: len 4; hex 8000000a; asc ;; 4: len 4; hex 800003e8; asc ;;
---TRANSACTION 6699544, ACTIVE 41 sec starting index read mysql tables in use 1, locked 1 LOCK WAIT 2 lock struct(s), heap size 1136, 1 row lock(s) MySQL thread id 11, OS thread handle 139688309319424, query id 368 localhost root executing select * from t where id = 7 for update ------- TRX HAS BEEN WAITING 41 SEC FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 8692 page no 4 n bits 80 index GEN_CLUST_INDEX of table `ds0`.`t` trx id 6699544 lock_mode X locks rec but not gap waiting Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b900; asc H ;; 1: len 6; hex 00000066361a; asc f6 ;; 2: len 7; hex 810000011a0110; asc ;; 3: len 4; hex 80000001; asc ;; 4: len 4; hex 80000064; asc d;;
------------------ TABLE LOCK table `ds0`.`t` trx id 6699544 lock mode IX RECORD LOCKS space id 8692 page no 4 n bits 80 index GEN_CLUST_INDEX of table `ds0`.`t` trx id 6699544 lock_mode X locks rec but not gap waiting Record lock, heap no 2 PHYSICAL RECORD: n_fields 5; compact format; info bits 0 0: len 6; hex 00000048b900; asc H ;; 1: len 6; hex 00000066361a; asc f6 ;; 2: len 7; hex 810000011a0110; asc ;; 3: len 4; hex 80000001; asc ;; 4: len 4; hex 80000064; asc d;;