1 锁等待的例子
session 1: 执行查询但不提交
mysql> begin;
mysql> select * from test where c2 = ‘1‘;
session 2: 执行ddl发生等待
mysql> alter table test drop index idx5;
查看show processlist;
| 20487 | mytest | ****:35986 | test123 | Sleep | 3799 | | NULL |
| 20501 | mytest | ****:35990 | test123 | Sleep | 65 | | NULL |
| 20886 | mytest | ****:36076 | test123 | Query | 36 | Waiting for table metadata lock | alter table test drop index idx5 |
| 21463 | mytest | ****:36208 | NULL | Query | 0 | init | show processlist |
ddl发生锁等待"Waiting for table metadata lock"
从information_schema.innodb_trx可以查到未提交的事务
mysql> select * from information_schema.innodb_trx\G
*************************** 1. row ***************************
trx_id: 96386
trx_state: RUNNING
trx_started: 2015-01-29 16:34:38
trx_requested_lock_id: NULL
trx_wait_started: NULL
trx_weight: 0
trx_mysql_thread_id: 20501
trx_query: NULL
trx_operation_state: NULL
trx_tables_in_use: 0
trx_tables_locked: 0
trx_lock_structs: 0
trx_lock_memory_bytes: 360
trx_rows_locked: 0
trx_rows_modified: 0
trx_concurrency_tickets: 0
trx_isolation_level: READ COMMITTED
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
trx_is_read_only: 0
trx_autocommit_non_locking: 0
1 row in set (0.00 sec)
show engine innodb status也可看到未提交事务信息。
------------
TRANSACTIONS
------------
Trx id counter 96455
Purge done for trx‘s n:o < 96339 undo n:o < 0 state: running but idle
History list length 282
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0, not started
MySQL thread id 21463, OS thread handle 0x2b7e50081700, query id 155157 10.189.100.29 mytest init
show engine innodb status
---TRANSACTION 96330, not started
MySQL thread id 20886, OS thread handle 0x2b7e600c2700, query id 153866 10.189.100.30 mytest Waiting for table metadata lock
alter table test drop index idx5
---TRANSACTION 96094, not started
MySQL thread id 20487, OS thread handle 0x2b7e8c081700, query id 146859 10.189.100.29 mytest cleaning up
---TRANSACTION 96386, ACTIVE 744 sec
MySQL thread id 20501, OS thread handle 0x2b7e70081700, query id 153813 10.189.100.30 mytest cleaning up
session 1:执行commit;
commit;
session 2:ddl完成。
2 metadata lock
1)metadata lock是什么
顾名思义,metadata lock即元数据锁。在数据库中元数据即数据字典信息包括db,table,function,procedure,trigger,event等。metadata lock主要为了保证元数据的一致性。而实际上metadata lock不仅仅保护元数据,还可以保证set global read only, flush tables with read lock 操作的顺利执行。
metadata lock也是一种锁。每个metadata lock都会定义锁住的对象,锁的持有时间和锁的类型。
锁的作用对象
metadata lock按锁住的对象来分类,可以分为global, schema, table, function,procedure,trigger,event,commit.这些对象发生锁等待时,我们在show processlist可以分别看到如下等待信息。
Waiting for global read lock
Waiting for schema metadata lock
Waiting for table metadata lock
Waiting for stored function metadata lock
Waiting for stored procedure metadata lock
Waiting for trigger metadata lock
Waiting for event metadata lock
Waiting for commit lock
锁的持有时间
MDL_STATEMENT:语句结束时释放
MDL_TRANSACTION:事务结束时释放
MDL_EXPLICIT:显式释放
锁的类型
(1)MDL_INTENTION_EXCLUSIVE(IX):意向排他锁,用于global和commit的加锁。
例如:truncate table t1;insert into t1 values(3,‘abcde‘);会加如下锁:
(GLOBAL,MDL_STATEMENT,MDL_INTENTION_EXCLUSIVE)
(SCHEMA,MDL_TRANSACTION,MDL_INTENTION_EXCLUSIVE)
(2)MDL_SHARED(S):只访问元数据,不访问数据。
例如: set golbal_read_only =on;flush tables with read lock;会加如下锁:
(GLOBAL,MDL_EXPLICIT,MDL_SHARED)
(COMMIT,MDL_EXPLICIT,MDL_SHARED)
(3)MDL_SHARED_HIGH_PRIO(SH):用于访问information_scheam表,不涉及数据。
例如: select * from information_schema.tables;
show create table xx;
desc xxx;会加如下锁:
(TABLE,MDL_TRANSACTION,MDL_SHARED_HIGH_PRIO)
(4)MDL_SHARED_READ(SR): 用于读数据
例如:select * from t1; lock table t1 read;会加如下锁:
(TABLE,MDL_TRANSACTION,MDL_SHARE_READ)
(5)MDL_SHARED_WRITE(SW):用于写数据
例如:insert/update/delete/select .. for update会加如下锁:
(TABLE,MDL_TRANSACTION,MDL_SHARE_WRITE)
(6)MDL_SHARED_UPGRADABLE(SU):是mysql5.6引入的新的Metadata lock,在alter table /create index/drop index会加该锁;可以说是为了online ddl才引入的。特点是允许DML,防止DDL;
(TABLE,MDL_TRANSACTION,MDL_SHARED_UPGRADABLE)
MDL_SHARED_NO_WRITE(SNW):用于防止其他人读写数据,但可以访问元数据。
(7)MDL_SHARED_NO_WRITE(SNW):用于防止其他人读写数据,但可以访问元数据。
alter table t1 modify c bigint;(非onlineddl)
(TABLE,MDL_TRANSACTION,MDL_SHARED_NO_WRITE)
(8)MDL_SHARED_NO_READ_WRITE(SNRW):用于防止其他人读写数据,但可以访问元数据。
lock table t1 write;
(TABLE,MDL_TRANSACTION,MDL_SHARED_NO_READ_WRITE)
(9)MDL_EXCLUSIVE(X):防止访问元数据
例如:CREATE/DROP/RENAME TABLE,其他online DDL在rename阶段也持有X锁
(TABLE,MDL_TRANSACTION,MDL_EXCLUSIVE)
other
关于global对象
主要作用是防止DDL和写操作的过程中,执行set golbal_read_only =on或flush tables with read lock;
关于commit对象锁
主要作用是执行flush tables with read lock后,防止已经开始在执行的写事务提交。
insert/update/delete在提交时都会上(COMMIT,MDL_EXPLICIT,MDL_INTENTION_EXCLUSIVE)锁。
关于schema对象锁
主要作用是防止alter/drop/create db与 alter/drop/create table,procedut之间的并发执行
alter/drop/create db会上(SCHEMA,MDL_TRANSACTION,MDL_EXCLUSIVE)
而 alter/drop/create table,procedut会上(SCHEMA,MDL_TRANSACTION,MDL_INTENTION_EXCLUSIVE)
关于table对象锁
主要处理表DDL与DDL, 表DDL与表DML之间的并发
3 其他案例
根据前面介绍的metadata lock加锁规则和锁的兼容性,我们就可以解释出现metadata lock等待的原因了。
1)Waiting for global read lock
我们先够造一个Waiting for global read lock场景:
session1: alter table t1 add c3 bigint; //大表执行需较长时间
session2: set global read only=on; //等待
查看
mysql> show processlist;
+----+------+-----------------+------+---------+------+------------------------------+------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+------------------------------+------------------------------+
| 1 | root | localhost:5202 | test | Query | 12 | altering table | alter table t1 add c3 bigint |
| 2 | root | localhost:14699 | test | Query | 3 | Waiting for global read lock | set global read_only=on |
| 3 | root | localhost:17085 | NULL | Query | 0 | init | show processlist |
+----+------+-----------------+------+---------+------+------------------------------+------------------------------+
3 rows in set (0.00 sec)
分析:
alter table t1 add c3 bigint;会加(GLOBAL,MDL_STATEMENT,MDL_INTENTION_EXCLUSIVE) 语句结束后才释放
set global read only=on;会加(GLOBAL,MDL_EXPLICIT,MDL_SHARED)
由于session1执行时间比较长,一直持有MDL_INTENTION_EXCLUSIVE。从兼容性矩阵可以看出MDL_SHARED和MDL_INTENTION_EXCLUSIVE是不相容的,因此发生“Waiting for global read lock ”等待。直到session 1 alter操作完成释放MDL_INTENTION_EXCLUSIVE。set global read only=on;才可以继续执行。
2)Waiting for commit lock
session1: begin
insert into t1 vlaues(null, ‘ab‘);
session2:flush table with read lock;//成功
session1: commit //发生等待
查看
mysql> show processlist;
+----+------+-----------------+------+---------+------+-------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+-------------------------+------------------+
| 1 | root | localhost:5202 | test | Query | 7 | Waiting for commit lock | commit |
| 2 | root | localhost:14699 | test | Sleep | 13 | | NULL |
| 3 | root | localhost:17085 | NULL | Query | 0 | init | show processlist |
+----+------+-----------------+------+---------+------+-------------------------+------------------+
分析:
flush table with read lock;持有(COMMIT,MDL_EXPLICIT,MDL_SHARED)
commit时上(COMMIT,MDL_EXPLICIT,MDL_INTENTION_EXCLUSIVE)锁
MDL_SHARED和MDL_INTENTION_EXCLUSIVE是不相容的,因此发生等待
3)Waiting for table metadata lock
这里我们来看第一节给出的锁等待的例子。
查询表时上(TABLE,MDL_TRANSACTION,MDL_SHARE_READ)锁,这里需注意MDL_TRANSACTION表示事物提交时才释放锁。
alter表上(TABLE,MDL_TRANSACTION, MDL_EXCLUSIVE)锁,MDL_SHARE_READ和MDL_EXCLUSIVE是不相容的,因此需等待查询事物提交。