0

当我尝试使用查询更新表时

 UPDATE person.heroes
   SET passwd = 'f662da7b9bd166ded69337a7f618da89'
 WHERE heroes_id = 283703

这个特定的线程(3086C6)被设置为等待锁定,因为另一个线程(3086C0)具有相同的锁定。该表在 hero_id 上的别名和主索引上是唯一的。我没有运行任何其他进程和“显示进程列表;” 确认它。

这两个线程显示它们正在使用 X 锁锁定相同的数据项“283703”(这是 account_id 并且是主索引)。3086C0是如何产生的?

person.heroes 上有一个触发器可以更新另一个表。

    sql>show engine innodb status;
    SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 388, signal count 394
Mutex spin waits 128, rounds 1354, OS waits 39
RW-shared spins 365, rounds 10714, OS waits 345
RW-excl spins 0, rounds 144, OS waits 4
Spin rounds per wait: 10.58 mutex, 29.35 RW-shared, 144.00 RW-excl
------------
TRANSACTIONS
------------
Trx id counter 3086C7
Purge done for trx's n:o < 3086B4 undo n:o < 0
History list length 1690
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 3086C3, not started
MySQL thread id 191, OS thread handle 0x7ff590948700, query id 48008 192.168.104.19 game
---TRANSACTION 3086C4, not started
MySQL thread id 192, OS thread handle 0x7ff58fd18700, query id 48012 192.168.104.19 game
---TRANSACTION 3079C4, not started
MySQL thread id 164, OS thread handle 0x7ff590885700, query id 44155 192.168.0.12 game
---TRANSACTION 308597, not started
MySQL thread id 167, OS thread handle 0x7ff5908c6700, query id 47224 192.168.0.12 game
---TRANSACTION 307FCF, not started
MySQL thread id 166, OS thread handle 0x7ff624054700, query id 45727 192.168.0.12 game
---TRANSACTION 307FC9, not started
MySQL thread id 163, OS thread handle 0x7ff590b91700, query id 45717 192.168.0.12 game
---TRANSACTION 307A1C, not started
MySQL thread id 155, OS thread handle 0x7ff62692d700, query id 45722 192.168.0.11 game
---TRANSACTION 3085ED, not started
MySQL thread id 146, OS thread handle 0x7ff590b50700, query id 47411 192.168.0.11 game
---TRANSACTION 308677, not started
MySQL thread id 130, OS thread handle 0x7ff590b0f700, query id 48016 localhost root
show engine innodb status
---TRANSACTION 303B8E, not started
MySQL thread id 125, OS thread handle 0x7ff590a4c700, query id 13780 localhost root
---TRANSACTION 308615, not started
MySQL thread id 122, OS thread handle 0x7ff590a8d700, query id 47458 192.168.0.11 game
---TRANSACTION 30865E, not started
MySQL thread id 120, OS thread handle 0x7ff590a0b700, query id 47545 192.168.0.11 game
---TRANSACTION 3086C6, ACTIVE 3 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 376, 1 row lock(s)
MySQL thread id 193, OS thread handle 0x7ff6268ec700, query id 48015 192.168.104.19 game Updating
UPDATE person.heroes
   SET passwd = 'f662da7b9bd166ded69337a7f618da89'
 WHERE heroes_id = 283703
Trx read view will not see trx with id >= 3086C7, sees < 3086C0
------- TRX HAS BEEN WAITING 3 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 783 page no 20 n bits 160 index `PRIMARY` of table `person`.`heroes` trx id 3086C6 lock_mode X locks rec but not gap waiting
Record lock, heap no 20 PHYSICAL RECORD: n_fields 29; compact format; info bits 0
 0: len 4; hex 80045437; asc   T7;;
 1: len 6; hex 0000003086c0; asc    0  ;;
 2: len 7; hex 25000002630e5d; asc %   c ];;
 3: len 3; hex 543120; asc T1 ;;
 4: SQL NULL;
 5: SQL NULL;
 6: SQL NULL;
 7: SQL NULL;
 8: SQL NULL;
 9: len 2; hex 494e; asc IN;;
 10: SQL NULL;
 11: SQL NULL;
 12: len 28; hex 746573746f6965776e31323334406d61696c696e61746f722e636f6d; asc testoiewn1234@mailinator.com;;
 13: len 13; hex 746573746f6965776e31323334; asc testoiewn1234;;
 14: len 30; hex 346464376565616238306135633231663033303138666166366462666363; asc 4dd7eeab80a5c21f03018faf6dbfcc; (total 32 bytes);
 15: SQL NULL;
 16: SQL NULL;
 17: SQL NULL;
 18: SQL NULL;
 19: SQL NULL;
 20: len 1; hex 80; asc  ;;
 21: len 4; hex 80000000; asc     ;;
 22: len 6; hex 353030303831; asc 500081;;
 23: SQL NULL;
 24: len 1; hex 80; asc  ;;
 25: len 4; hex 80000200; asc     ;;
 26: len 3; hex 800000; asc    ;;
 27: len 1; hex 80; asc  ;;
 28: len 3; hex 494e52; asc INR;;

------------------
TABLE LOCK table `person`.`heroes` trx id 3086C6 lock mode IX
RECORD LOCKS space id 783 page no 20 n bits 160 index `PRIMARY` of table `person`.`heroes` trx id 3086C6 lock_mode X locks rec but not gap waiting
Record lock, heap no 20 PHYSICAL RECORD: n_fields 29; compact format; info bits 0
 0: len 4; hex 80045437; asc   T7;;
 1: len 6; hex 0000003086c0; asc    0  ;;
 2: len 7; hex 25000002630e5d; asc %   c ];;
 3: len 3; hex 543120; asc T1 ;;
 4: SQL NULL;
 5: SQL NULL;
 6: SQL NULL;
 7: SQL NULL;
 8: SQL NULL;
 9: len 2; hex 494e; asc IN;;
 10: SQL NULL;
 11: SQL NULL;
 12: len 28; hex 746573746f6965776e31323334406d61696c696e61746f722e636f6d; asc testoiewn1234@mailinator.com;;
 13: len 13; hex 746573746f6965776e31323334; asc testoiewn1234;;
 14: len 30; hex 346464376565616238306135633231663033303138666166366462666363; asc 4dd7eeab80a5c21f03018faf6dbfcc; (total 32 bytes);
 15: SQL NULL;
 16: SQL NULL;
 17: SQL NULL;
 18: SQL NULL;
 19: SQL NULL;
 20: len 1; hex 80; asc  ;;
 21: len 4; hex 80000000; asc     ;;
 22: len 6; hex 353030303831; asc 500081;;
 23: SQL NULL;
 24: len 1; hex 80; asc  ;;
 25: len 4; hex 80000200; asc     ;;
 26: len 3; hex 800000; asc    ;;
 27: len 1; hex 80; asc  ;;
 28: len 3; hex 494e52; asc INR;;

---TRANSACTION 3086C0, ACTIVE 3 sec
3 lock struct(s), heap size 1248, 2 row lock(s), undo log entries 1
MySQL thread id 190, OS thread handle 0x7ff590ace700, query id 47999 192.168.104.19 game
TABLE LOCK table `person`.`heroes` trx id 3086C0 lock mode IX
RECORD LOCKS space id 783 page no 18 n bits 624 index `alias` of table `person`.`heroes` trx id 3086C0 lock_mode X locks rec but not gap
Record lock, heap no 508 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
 0: len 13; hex 746573746f6965776e31323334; asc testoiewn1234;;
 1: len 4; hex 80045437; asc   T7;;

RECORD LOCKS space id 783 page no 20 n bits 160 index `PRIMARY` of table `person`.`heroes` trx id 3086C0 lock_mode X locks rec but not gap
Record lock, heap no 20 PHYSICAL RECORD: n_fields 29; compact format; info bits 0
 0: len 4; hex 80045437; asc   T7;;
 1: len 6; hex 0000003086c0; asc    0  ;;
 2: len 7; hex 25000002630e5d; asc %   c ];;
 3: len 3; hex 543120; asc T1 ;;
 4: SQL NULL;
 5: SQL NULL;
 6: SQL NULL;
 7: SQL NULL;
 8: SQL NULL;
 9: len 2; hex 494e; asc IN;;
 10: SQL NULL;
 11: SQL NULL;
 12: len 28; hex 746573746f6965776e31323334406d61696c696e61746f722e636f6d; asc testoiewn1234@mailinator.com;;
 13: len 13; hex 746573746f6965776e31323334; asc testoiewn1234;;
 14: len 30; hex 346464376565616238306135633231663033303138666166366462666363; asc 4dd7eeab80a5c21f03018faf6dbfcc; (total 32 bytes);
 15: SQL NULL;
 16: SQL NULL;
 17: SQL NULL;
 18: SQL NULL;
 19: SQL NULL;
 20: len 1; hex 80; asc  ;;
 21: len 4; hex 80000000; asc     ;;
 22: len 6; hex 353030303831; asc 500081;;
 23: SQL NULL;
 24: len 1; hex 80; asc  ;;
 25: len 4; hex 80000200; asc     ;;
 26: len 3; hex 800000; asc    ;;
 27: len 1; hex 80; asc  ;;
 28: len 3; hex 494e52; asc INR;;

--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (read thread)
I/O thread 4 state: waiting for i/o request (read thread)
I/O thread 5 state: waiting for i/o request (read thread)
I/O thread 6 state: waiting for i/o request (write thread)
I/O thread 7 state: waiting for i/o request (write thread)
I/O thread 8 state: waiting for i/o request (write thread)
I/O thread 9 state: waiting for i/o request (write thread)
Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
 ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
1343 OS file reads, 6867 OS file writes, 2731 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.45 writes/s, 0.09 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 30 merges
merged operations:
 insert 30, delete mark 0, delete 0
discarded operations:
 insert 0, delete mark 0, delete 0
Hash table size 4425293, node heap has 17 buffer(s)
29.09 hash searches/s, 3.91 non-hash searches/s
---
LOG
---
Log sequence number 682871332
Log flushed up to   682871332
Last checkpoint at  682869899
0 pending log writes, 0 pending chkp writes
2311 log i/o's done, 0.45 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 2197815296; in additional pool allocated 0
Dictionary memory allocated 714369
Buffer pool size   131071
Free buffers       129785
Database pages     1269
Old database pages 488
Modified db pages  14
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 2, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 1228, created 41, written 4623
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 1269, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
2 read views open inside InnoDB
Main thread process no. 1460, id 140692705335040, state: sleeping
Number of rows inserted 1366, updated 709, deleted 191, read 912575
0.09 inserts/s, 0.36 updates/s, 0.09 deletes/s, 33.72 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================
 |
+--------+------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

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 |
+------------------+-------------+-----------+-----------+--------------------+------------+------------+-----------+----------+-----------+
| 3086C6:783:20:20 | 3086C6      | X         | RECORD    | `person`.`heroes` | `PRIMARY`  |        783 |        20 |       20 | 283703    |
| 3086C0:783:20:20 | 3086C0      | X         | RECORD    | `person`.`heroes` | `PRIMARY`  |        783 |        20 |       20 | 283703    |
+------------------+-------------+-----------+-----------+--------------------+------------+------------+-----------+----------+-----------+
2 rows in set (0.00 sec)

mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX\G
*************************** 1. row ***************************
                trx_id: 3086C6
             trx_state: LOCK WAIT
           trx_started: 2013-06-27 15:28:46
     trx_requested_lock_id: 3086C6:783:20:20
      trx_wait_started: 2013-06-27 15:28:46
            trx_weight: 2
       trx_mysql_thread_id: 193
             trx_query: UPDATE person.heroes
   SET passwd = 'f662da7b9bd166ded69337a7f618da89'
 WHERE heroes_id = 283703
       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: 9999
*************************** 2. row ***************************
                trx_id: 3086C0
             trx_state: RUNNING
           trx_started: 2013-06-27 15:28:46
     trx_requested_lock_id: NULL
      trx_wait_started: NULL
            trx_weight: 4
       trx_mysql_thread_id: 190
             trx_query: NULL
       trx_operation_state: NULL
     trx_tables_in_use: 0
     trx_tables_locked: 0
      trx_lock_structs: 3
     trx_lock_memory_bytes: 1248
       trx_rows_locked: 2
     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)

mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;
+-------------------+-------------------+-----------------+------------------+
| requesting_trx_id | requested_lock_id | blocking_trx_id | blocking_lock_id |
+-------------------+-------------------+-----------------+------------------+
| 3086C6            | 3086C6:783:20:20  | 3086C0          | 3086C0:783:20:20 |
+-------------------+-------------------+-----------------+------------------+
1 row in set (0.00 sec)

mysql版本为:5.5.31

任何人都可以阐明吗?

4

1 回答 1

4

另一个 MySQL 显然在事务中访问了相同的记录,或者接近它的记录,并在其上打开了记录锁,防止其他线程修改记录,直到事务被提交或回滚。

要确定另一个 mysql 线程,您已经在正确的轨道上:

SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX\G

将显示您被阻止的 InnoDB 事务(上例中的 30250F)和另一个事务(302509)。检查mysql进程/线程列表:

show processlist;

每个线程都有一个线程 id:

mysql> show processlist;
+------------+---------+--------------------+------+-------------+---------+-----------------------------------------------------------------------+------------------+
| Id         | User    | Host               | db   | Command     | Time    | State                                                                 | Info             |
+------------+---------+--------------------+------+-------------+---------+-----------------------------------------------------------------------+------------------+
|        112 | myuser  | localhost          | mydb | Sleep       |      95 |                                                                       | NULL             |

并且阻塞事务的线程 id (112)trx_mysql_thread_idINNODB_TRX列表中可见。

如果您现在使用该kill 112;命令终止线程 112,它的事务应该被回滚(它在当前事务中所做的任何更改都将被还原),锁被释放,并且被阻塞的线程应该能够继续。

正如您在评论中所说,该线程现在可能正在休眠:它之前修改了一行(trx_rows_modified:1)导致两行被锁定(trx_rows_locked:2),其中一个是您尝试的行现在修改。它只是还没有提交事务,这就是锁仍然存在的原因。

至于真正的解决方法:您的应用程序应该COMMIT;在每次事务之后执行。如果您不使用事务并且不想现在学习它们,您可能希望SET autocommit=1;在连接到数据库之后和执行任何操作之前启用自动提交。MySQL 默认启用了自动提交,因此也可能是您当前在代码中禁用了它。请注意,您必须为每个连接单独执行此操作。

启用自动提交后,COMMIT如果您只是发出简单的语句,则不需要,但您仍然可以通过发出BEGINorSTART TRANSACTION命令显式启动事务,在这种情况下,您需要使用 aCOMMIT或终止它ROLLBACK。检查您的代码中的BEGINorSTART TRANSACTION语句,如果您不执行 COMMIT,请删除它们。

Autocommit 也可以作为全局选项进行调整(如何全局设置 autocommit false),但是您的应用程序将取决于具有该设置的数据库服务器配置。

使用事务时,您必须确保始终在操作结束时执行以下操作之一:

  1. 犯罪
  2. 回滚
  3. 关闭连接(如果未提交则导致回滚)

通过 Web 服务上的持久连接,HTTP 客户端连接意外关闭可能会在没有提交或回滚的情况下终止您的脚本执行,并使连接在事务中间保持打开状态。

相关问题,事务的基本介绍: 什么是数据库事务?

http://dev.mysql.com/doc/refman/5.6/en/commit.html

于 2013-06-27T07:17:00.387 回答