文/溫國兵
html
MySQL 5.5 中,information_schema 庫中新增了三個關於鎖的表,亦即 innodb_trx、innodb_locks 和 innodb_lock_waits。mysql
其中 innodb_trx
表記錄當前運行的全部事務,innodb_locks
表記錄當前出現的鎖,innodb_lock_waits
表記錄鎖等待的對應關係。sql
登陸 MySQL 5.5。併發
mysql -S /tmp/mysql_5540.sock -uroot -proot
這是個人 MySQL 版本信息。ide
mysql> SHOW VARIABLES LIKE '%version%'; +-------------------------+------------------------------+ | Variable_name | Value | +-------------------------+------------------------------+ | innodb_version | 5.5.40 | | protocol_version | 10 | | slave_type_conversions | | | version | 5.5.40 | | version_comment | MySQL Community Server (GPL) | | version_compile_machine | i386 | | version_compile_os | osx10.6 | +-------------------------+------------------------------+ 7 rows in set (0.00 sec)
查看 innodb_trx 表結構。測試
mysql> USE information_schema; mysql> DESC innodb_trx;
下面對 innodb_trx 表的每一個字段進行解釋:spa
trx_id:事務ID。
trx_state:事務狀態,有如下幾種狀態:RUNNING、LOCK WAIT、ROLLING BACK 和 COMMITTING。
trx_started:事務開始時間。
trx_requested_lock_id:事務當前正在等待鎖的標識,能夠和 INNODB_LOCKS 表 JOIN 以獲得更多詳細信息。
trx_wait_started:事務開始等待的時間。
trx_weight:事務的權重。
trx_mysql_thread_id:事務線程 ID,能夠和 PROCESSLIST 表 JOIN。
trx_query:事務正在執行的 SQL 語句。
trx_operation_state:事務當前操做狀態。
trx_tables_in_use:當前事務執行的 SQL 中使用的表的個數。
trx_tables_locked:當前執行 SQL 的行鎖數量。
trx_lock_structs:事務保留的鎖數量。
trx_lock_memory_bytes:事務鎖住的內存大小,單位爲 BYTES。
trx_rows_locked:事務鎖住的記錄數。包含標記爲 DELETED,而且已經保存到磁盤但對事務不可見的行。
trx_rows_modified:事務更改的行數。
trx_concurrency_tickets:事務併發票數。
trx_isolation_level:當前事務的隔離級別。
trx_unique_checks:是否打開惟一性檢查的標識。
trx_foreign_key_checks:是否打開外鍵檢查的標識。
trx_last_foreign_key_error:最後一次的外鍵錯誤信息。
trx_adaptive_hash_latched:自適應散列索引是否被當前事務鎖住的標識。
trx_adaptive_hash_timeout:是否馬上放棄爲自適應散列索引搜索 LATCH 的標識。線程
查看 innodb_locks 表結構。debug
mysql> DESC innodb_locks;
下面對 innodb_locks 表的每一個字段進行解釋:rest
lock_id:鎖 ID。
lock_trx_id:擁有鎖的事務 ID。能夠和 INNODB_TRX 表 JOIN 獲得事務的詳細信息。
lock_mode:鎖的模式。有以下鎖類型:行級鎖包括:S、X、IS、IX,分別表明:共享鎖、排它鎖、意向共享鎖、意向排它鎖。表級鎖包括:S_GAP、X_GAP、IS_GAP、IX_GAP 和 AUTO_INC,分別表明共享間隙鎖、排它間隙鎖、意向共享間隙鎖、意向排它間隙鎖和自動遞增鎖。
lock_type:鎖的類型。RECORD 表明行級鎖,TABLE 表明表級鎖。
lock_table:被鎖定的或者包含鎖定記錄的表的名稱。
lock_index:當 LOCK_TYPE=’RECORD’ 時,表示索引的名稱;不然爲 NULL。
lock_space:當 LOCK_TYPE=’RECORD’ 時,表示鎖定行的表空間 ID;不然爲 NULL。
lock_page:當 LOCK_TYPE=’RECORD’ 時,表示鎖定行的頁號;不然爲 NULL。
lock_rec:當 LOCK_TYPE=’RECORD’ 時,表示一堆頁面中鎖定行的數量,亦即被鎖定的記錄號;不然爲 NULL。
lock_data:當 LOCK_TYPE=’RECORD’ 時,表示鎖定行的主鍵;不然爲NULL。
查看 innodb_lock_waits 表結構。
mysql> DESC innodb_lock_waits;
下面對 innodb_lock_waits 表的每一個字段進行解釋:
requesting_trx_id:請求事務的 ID。
requested_lock_id:事務所等待的鎖定的 ID。能夠和 INNODB_LOCKS 表 JOIN。
blocking_trx_id:阻塞事務的 ID。
blocking_lock_id:某一事務的鎖的 ID,該事務阻塞了另外一事務的運行。能夠和 INNODB_LOCKS 表 JOIN。
建立測試表,錄入測試數據。
mysql> USE test; mysql> CREATE TABLE user -> (id INT PRIMARY KEY, -> name VARCHAR(20), -> age INT, -> sex CHAR(2), -> city VARCHAR(20), -> job VARCHAR(10) -> ) DEFAULT CHARSET utf8 ENGINE = INNODB; Query OK, 0 rows affected (0.15 sec) mysql> INSERT INTO user(id, name, age, sex, city, job) \ -> VALUES(1, 'robin', 19, 'M', 'GZ', 'DBA'); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO user(id, name, age, sex, city, job) \ -> VALUES(2, 'Wentasy', 19, 'M', 'GZ', 'DBA'); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO user(id, name, age, sex, city, job) \ -> VALUES(3, 'dbarobin', 19, 'M', 'GZ', 'DBA'); Query OK, 1 row affected (0.00 sec) mysql> COMMIT; Query OK, 0 rows affected (0.00 sec)
Session 1 開始事務。
mysql> START TRANSACTION; Query OK, 0 rows affected (0.00 sec) mysql> UPDATE user SET name='wentasy' WHERE id = 2; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 -- 此時已經開始事務,因此 innodb_trx 表會有記錄。 mysql> SELECT * FROM information_schema.innodb_trx \G *************************** 1. row *************************** trx_id: 360E trx_state: RUNNING trx_started: 2015-01-27 15:23:49 trx_requested_lock_id: NULL trx_wait_started: NULL trx_weight: 3 trx_mysql_thread_id: 1 trx_query: SELECT * FROM information_schema.innodb_trx trx_operation_state: NULL trx_tables_in_use: 0 trx_tables_locked: 0 trx_lock_structs: 2 trx_lock_memory_bytes: 376 trx_rows_locked: 1 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 1 row in set (0.00 sec) -- 此時沒有發生鎖等待,故 innodb_locks表 和 innodb_lock_waits 表都沒有數據。 mysql> SELECT * FROM information_schema.innodb_locks \G Empty set (0.00 sec) mysql> SELECT * FROM information_schema.innodb_lock_waits \G Empty set (0.00 sec)
Session 2 更新數據。
mysql -S /tmp/mysql_5540.sock -uroot -proot
mysql> USE test; mysql> UPDATE user SET name="lock_waits" WHERE ID = 2;
Session 1 查看 innodb_trx 表、innodb_locks 表和 innodb_lock_waits 表,能夠查看到數據。
在 innodb_trx 表的第一行,trx_id 爲 360F 表示第二個事務,狀態爲等待狀態,請求的鎖 ID 爲 360F:243:3:3,線程 ID 爲 2,事務用到的表爲 1,有 1 個表被鎖。第二行中,trx_id 爲 360E 表示第一個事務。
mysql> SELECT * FROM information_schema.innodb_trx \G *************************** 1. row *************************** trx_id: 360F trx_state: LOCK WAIT trx_started: 2015-01-27 15:28:48 trx_requested_lock_id: 360F:243:3:3 trx_wait_started: 2015-01-27 15:28:48 trx_weight: 2 trx_mysql_thread_id: 2 trx_query: UPDATE user SET name="lock_waits" WHERE ID = 2 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: 10000 *************************** 2. row *************************** trx_id: 360E trx_state: RUNNING trx_started: 2015-01-27 15:23:49 trx_requested_lock_id: NULL trx_wait_started: NULL trx_weight: 3 trx_mysql_thread_id: 1 trx_query: SELECT * FROM information_schema.innodb_trx trx_operation_state: NULL trx_tables_in_use: 0 trx_tables_locked: 0 trx_lock_structs: 2 trx_lock_memory_bytes: 376 trx_rows_locked: 1 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_locks \G *************************** 1. row *************************** lock_id: 360F:243:3:3 lock_trx_id: 360F lock_mode: X lock_type: RECORD lock_table: `test`.`user` lock_index: `PRIMARY` lock_space: 243 lock_page: 3 lock_rec: 3 lock_data: 2 *************************** 2. row *************************** lock_id: 360E:243:3:3 lock_trx_id: 360E lock_mode: X lock_type: RECORD lock_table: `test`.`user` lock_index: `PRIMARY` lock_space: 243 lock_page: 3 lock_rec: 3 lock_data: 2 2 rows in set (0.00 sec) mysql> SELECT * FROM information_schema.innodb_lock_waits \G *************************** 1. row *************************** requesting_trx_id: 360F requested_lock_id: 360F:243:3:3 blocking_trx_id: 360E blocking_lock_id: 360E:243:3:3 1 row in set (0.00 sec)
因爲默認的 innodb_lock_wait_timeout
是 50 秒,因此 50 秒事後,Session 2 出現以下提示:
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
「>
再次模擬鎖等待以前,把 innodb_lock_wait_timeout
設置爲 10 分鐘,方便後面的演示。
mysql> SHOW VARIABLES LIKE '%innodb_lock_wait%'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | innodb_lock_wait_timeout | 50 | +--------------------------+-------+ 1 row in set (0.00 sec) mysql> SET innodb_lock_wait_timeout=600; Query OK, 0 rows affected (0.00 sec) mysql> SET GLOBAL innodb_lock_wait_timeout=600; Query OK, 0 rows affected (0.00 sec) mysql> SHOW VARIABLES LIKE '%innodb_lock_wait%'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | innodb_lock_wait_timeout | 600 | +--------------------------+-------+ 1 row in set (0.00 sec)
再次開啓一個 Session,此時的 Session 姑且命名爲 Session 3。而後再次更新數據,因爲 Session 1 未提交,因此會發生鎖等待。
mysql> USE test; mysql> UPDATE user SET name="lock_waits" WHERE ID = 2;
此時再次發生鎖等待。咱們在 Session 1 中使用不一樣的查詢方法查看鎖等待。
mysql> SELECT * FROM innodb_lock_waits \G *************************** 1. row *************************** requesting_trx_id: 3612 requested_lock_id: 3612:243:3:3 blocking_trx_id: 360E blocking_lock_id: 360E:243:3:3 1 row in set (0.00 sec)
mysql> SELECT * \ > FROM innodb_locks \ > WHERE lock_trx_id \ > IN (SELECT blocking_trx_id FROM innodb_lock_waits) \G *************************** 1. row *************************** lock_id: 360E:243:3:3 lock_trx_id: 360E lock_mode: X lock_type: RECORD lock_table: `test`.`user` lock_index: `PRIMARY` lock_space: 243 lock_page: 3 lock_rec: 3 lock_data: 2 1 row in set (0.00 sec)
mysql> SELECT innodb_locks.* \ > FROM innodb_locks \ > JOIN innodb_lock_waits \ > ON (innodb_locks.lock_trx_id = innodb_lock_waits.blocking_trx_id) \G *************************** 1. row *************************** lock_id: 360E:243:3:3 lock_trx_id: 360E lock_mode: X lock_type: RECORD lock_table: `test`.`user` lock_index: `PRIMARY` lock_space: 243 lock_page: 3 lock_rec: 3 lock_data: 2 1 row in set (0.01 sec)
須要注意 lock_table
值的寫法。
mysql> SELECT * FROM innodb_locks \ > WHERE lock_table = '`test`.`user`' \G *************************** 1. row *************************** lock_id: 3612:243:3:3 lock_trx_id: 3612 lock_mode: X lock_type: RECORD lock_table: `test`.`user` lock_index: `PRIMARY` lock_space: 243 lock_page: 3 lock_rec: 3 lock_data: 2 *************************** 2. row *************************** lock_id: 360E:243:3:3 lock_trx_id: 360E lock_mode: X lock_type: RECORD lock_table: `test`.`user` lock_index: `PRIMARY` lock_space: 243 lock_page: 3 lock_rec: 3 lock_data: 2 2 rows in set (0.00 sec)
mysql> SELECT trx_id, trx_requested_lock_id, trx_mysql_thread_id, trx_query \ > FROM innodb_trx \ > WHERE trx_state = 'LOCK WAIT' \G *************************** 1. row *************************** trx_id: 3612 trx_requested_lock_id: 3612:243:3:3 trx_mysql_thread_id: 9 trx_query: UPDATE user SET name="lock_waits" WHERE ID = 2 1 row in set (0.00 sec)
mysql> SHOW ENGINE INNODB STATUS \G
在輸出結果的最後,咱們看到以下信息:
-------------- ROW OPERATIONS -------------- 0 queries inside InnoDB, 0 queries in queue 1 read views open inside InnoDB Main thread id 4525080576, state: waiting for server activity Number of rows inserted 6, updated 1, deleted 0, read 2 0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s ---------------------------- END OF INNODB MONITOR OUTPUT ============================ 1 row in set (0.00 sec)
mysql> SHOW PROCESSLIST \G *************************** 1. row *************************** Id: 1 User: root Host: localhost db: information_schema Command: Query Time: 0 State: NULL Info: SHOW PROCESSLIST *************************** 2. row *************************** Id: 9 User: root Host: localhost db: test Command: Query Time: 116 State: Updating Info: UPDATE user SET name="lock_waits" WHERE ID = 2 2 rows in set (0.00 sec)
既然咱們從上述方法中獲得了相關信息,咱們能夠獲得發生鎖等待的線程 ID,而後將其 KILL 掉。
Session 1 中 KILL 掉髮生鎖等待的線程。
mysql> kill 9;
Query OK, 0 rows affected (0.00 sec)
Session 3 中能夠看到鎖等待消除。
mysql> UPDATE user SET name="lock_waits" WHERE ID = 2;
有以下輸出:
ERROR 2013 (HY000): Lost connection to MySQL server during query
Session 1 中再次查看 PROCESSLIST,能夠看到沒有相關的信息了。
mysql> SHOW PROCESSLIST \G *************************** 1. row *************************** Id: 1 User: root Host: localhost db: information_schema Command: Query Time: 0 State: NULL Info: SHOW PROCESSLIST 1 row in set (0.00 sec)
模擬完成後,咱們提交,此時 innodb_trx 表、innodb_locks 表和 innodb_lock_waits 表中都沒有數據。
mysql> COMMIT; Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM information_schema.innodb_trx \G Empty set (0.00 sec) mysql> SELECT * FROM information_schema.innodb_locks \G Empty set (0.00 sec) mysql> SELECT * FROM information_schema.innodb_lock_waits \G Empty set (0.00 sec)
把 innodb_lock_wait
還原爲默認值。
mysql> SHOW VARIABLES LIKE '%innodb_lock_wait%'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | innodb_lock_wait_timeout | 600 | +--------------------------+-------+ 1 row in set (0.00 sec) mysql> SET GLOBAL innodb_lock_wait_timeout=50; Query OK, 0 rows affected (0.00 sec) mysql> SET innodb_lock_wait_timeout=50; Query OK, 0 rows affected (0.00 sec) mysql> SHOW VARIABLES LIKE '%innodb_lock_wait%'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | innodb_lock_wait_timeout | 50 | +--------------------------+-------+ 1 row in set (0.00 sec)
–EOF–