MySQL metadata鎖實驗
在對已經存在的表執行DDL語句(例如更改表結構、DROP表等操作)的時候,如果有與這個表相關聯的事務沒有提交,就會產生metadata鎖等待。
這個時候,需要提交與這張表相關聯的事務,才會將metadata鎖釋放。
建立一張表
mysql> CREATE TABLE `travelrecord` (
-> `id` bigint(20) NOT NULL,
-> `user_id` varchar(100) DEFAULT NULL,
-> `traveldate` date DEFAULT NULL,
-> `fee` decimal(10,0) DEFAULT NULL,
-> `days` int(11) DEFAULT NULL,
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=gbk;
Query OK, 0 rows affected (0.09 sec)
會話一
開啟一個事務,查詢新建立的表
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from travelrecord;
Empty set (0.00 sec)
會話二
drop剛剛建立的表,會發生鎖等待
mysql> drop table travelrecord;
會話一
查詢會話
mysql> show processlist;
+----+------+-----------------+------+---------+------+---------------------------------+-------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+---------------------------------+-------------------------+
| 3 | root | 127.0.0.1:34875 | sale | Query | 0 | init | show processlist |
| 24 | root | 127.0.0.1:34876 | sale | Query | 1058 | Waiting for table metadata lock | drop table travelrecord |
+----+------+-----------------+------+---------+------+---------------------------------+-------------------------+
2 rows in set (0.00 sec)
提交事務
mysql> commit;
Query OK, 0 rows affected (0.03 sec)
會話二
鎖釋放,DROP語句順利執行
mysql> drop table travelrecord;
Query OK, 0 rows affected (58 min 40.02 sec)
資料庫中沒有了鎖
mysql> show processlist;
+----+------+-----------------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+-------+------------------+
| 3 | root | 127.0.0.1:34875 | sale | Sleep | 76 | | NULL |
| 24 | root | 127.0.0.1:34876 | sale | Query | 0 | init | show processlist |
+----+------+-----------------+------+---------+------+-------+------------------+
2 rows in set (0.00 sec)
這個時候,需要提交與這張表相關聯的事務,才會將metadata鎖釋放。
建立一張表
mysql> CREATE TABLE `travelrecord` (
-> `id` bigint(20) NOT NULL,
-> `user_id` varchar(100) DEFAULT NULL,
-> `traveldate` date DEFAULT NULL,
-> `fee` decimal(10,0) DEFAULT NULL,
-> `days` int(11) DEFAULT NULL,
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=gbk;
Query OK, 0 rows affected (0.09 sec)
會話一
開啟一個事務,查詢新建立的表
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from travelrecord;
Empty set (0.00 sec)
drop剛剛建立的表,會發生鎖等待
mysql> drop table travelrecord;
會話一
查詢會話
mysql> show processlist;
+----+------+-----------------+------+---------+------+---------------------------------+-------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+---------------------------------+-------------------------+
| 3 | root | 127.0.0.1:34875 | sale | Query | 0 | init | show processlist |
| 24 | root | 127.0.0.1:34876 | sale | Query | 1058 | Waiting for table metadata lock | drop table travelrecord |
+----+------+-----------------+------+---------+------+---------------------------------+-------------------------+
2 rows in set (0.00 sec)
mysql> commit;
Query OK, 0 rows affected (0.03 sec)
鎖釋放,DROP語句順利執行
mysql> drop table travelrecord;
Query OK, 0 rows affected (58 min 40.02 sec)
資料庫中沒有了鎖
mysql> show processlist;
+----+------+-----------------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------------+------+---------+------+-------+------------------+
| 3 | root | 127.0.0.1:34875 | sale | Sleep | 76 | | NULL |
| 24 | root | 127.0.0.1:34876 | sale | Query | 0 | init | show processlist |
+----+------+-----------------+------+---------+------+-------+------------------+
2 rows in set (0.00 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2145655/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql觀測METADATA LOCK(MDL)鎖MySql
- mysql metadata lock後設資料鎖之鎖狀態lock_status流轉圖MySql
- Mysql學習筆記-臨鍵鎖實驗MySql筆記
- MySQL8.0 binlog_row_metadataMySql
- MySQL新增索引偶遇waiting for table metadata lockMySql索引AI
- MySQL DDL Waiting for table metadata lock 解決MySqlAI
- Mysql加鎖與實踐MySql
- mysql鎖之死鎖MySql
- mysql 鎖MySql
- [Mysql]鎖MySql
- MySQL鎖MySql
- 利用MySQL中的樂觀鎖和悲觀鎖實現分散式鎖MySql分散式
- mysql load 相關實驗MySql
- Qt MetadataQT
- 線上BUG:MySQL死鎖分析實戰MySql
- 【MySQL】MySQL中的鎖MySql
- MySQL 死鎖和鎖等待MySql
- MySQL實戰 | 06/07 簡單說說MySQL中的鎖MySql
- MySQL -- 表鎖MySql
- MySQL -- 行鎖MySql
- MySQL表鎖MySql
- MySQL 四 鎖MySql
- MySQL 行鎖MySql
- MySQL鎖分析MySql
- MySQL 鎖bug?MySql
- MySQL的鎖MySql
- MySQL內部實現讀鎖和寫鎖的具體鎖定型別介紹MySql型別
- MYSQL學習與實驗(八)——儲存過程實驗MySql儲存過程
- MySQL全域性鎖、表鎖以及行鎖MySql
- MySQL鎖(樂觀鎖、悲觀鎖、多粒度鎖)MySql
- MySQL 全域性鎖和表鎖MySql
- MySQL鎖:03.InnoDB行鎖MySql
- 2.3.2.2.1 Metadata Links
- 解析arxiv Metadata
- 8.11.4 Metadata Locking
- 【問答分享第一彈】MySQL鎖總結:MySQL行鎖、表鎖、排他鎖、共享鎖的特點MySql
- Mysql鎖之行級鎖和表級意向鎖MySql
- mysql load 相關實驗記錄MySql
- MySQL鎖相關MySql