共計 4124 個字符,預計需要花費 11 分鐘才能閱讀完成。
本篇文章給大家分享的是有關 Mysql 從庫大量 select 堵塞處于 Waiting for table flush 狀態該怎么辦,丸趣 TV 小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著丸趣 TV 小編一起來看看吧。
背景:
mycat 讀寫分離,應用大量 select 超時
1. 檢查
通過檢查發現大量 select 處于 Waiting for table flush 狀態,仔細看了一下 processlist 以及時間段,可以斷定是備份加 select 慢查詢引起的!
2. 重現環境
session1
session2
查看此時的 processlist 狀態
mysql show full processlist;
+——+——+———————+——–+————-+———+———————————————————————–+—————————————————+
| Id | User | Host | db | Command | Time | State | Info |
+——+——+———————+——–+————-+———+———————————————————————–+—————————————————+
| 2 | repl | 47.93.243.162:43700 | NULL | Binlog Dump | 1527333 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL |
| 9140 | root | localhost | devops | Query | 564 | User sleep | select *,sleep(1000) from operation_log limit 100 |
| 9141 | root | localhost | NULL | Query | 0 | init | show full processlist |
| 9143 | root | localhost:56880 | NULL | Query | 509 | Waiting for table flush | FLUSH NO_WRITE_TO_BINLOG TABLES |
終端二執行 xtracebackup 備份
。。。。。
。。。。。
log scanned up to (768745274)
。。。。。備份堵塞
終端一執行一個慢查詢
mysql select *,sleep(1000) from operation_log limit 100
session3
終端 3 對慢查詢涉及到的表進行查詢操作
[root@iZ2ze66bhrbxkc31nljgjnZ ~]# mysql -uroot -p***** -e select * from operation_log limit 10 devops
Warning: Using a password on the command line interface can be insecure.
… 堵塞狀態
此時的 processlist 狀態
mysql show full processlist;
+——+——+———————+——–+————-+———+———————————————————————–+—————————————————+
| Id | User | Host | db | Command | Time | State | Info |
+——+——+———————+——–+————-+———+———————————————————————–+—————————————————+
| 2 | repl | 47.93.243.162:43700 | NULL | Binlog Dump | 1527460 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL |
| 9140 | root | localhost | devops | Query | 691 | User sleep | select *,sleep(1000) from operation_log limit 100 |
| 9141 | root | localhost | NULL | Query | 0 | init | show full processlist |
| 9143 | root | localhost:56880 | NULL | Query | 636 | Waiting for table flush | FLUSH NO_WRITE_TO_BINLOG TABLES |
| 9150 | root | localhost | devops | Query | 454 | Waiting for table flush | select * from operation_log limit 10 | – 查詢被堵塞
+——+——+———————+——–+————-+———+———————————————————————–+—————————————————+
步驟 1 阻塞了步驟二, 步驟二導致步驟三需要等待步驟一。
session4
終端四對其它非慢查詢中的表進行查詢(不堵塞)
[root@iZ2ze66bhrbxkc31nljgjnZ ~]# mysql -uroot -pESBecs00 -e select * from role limit 10 devops
Warning: Using a password on the command line interface can be insecure.
+—-+—————–+——————————–+——–+
| id | role_name | description | status |
+—-+—————–+——————————–+——–+
| 1 | 超級管理員 | 所有權限 | 1 |
| 3 | 開發工程師 | 開發工程師開發工程師 | 1 |
| 4 | 運維工程師 | 運帷工程師運帷工程師 | 1 |
+—-+—————–+——————————–+——–+
[root@iZ2ze66bhrbxkc31nljgjnZ ~]# mysql -uroot -pESBecs00 -e select * from module limit 10 devops
Warning: Using a password on the command line interface can be insecure.
+—–+————–+——–+————+
| id | module_name | status | list_order |
+—–+————–+——–+————+
| 100 | 系統管理 | 1 | 2 |
| 600 | 環境管理 | 1 | 3 |
+—–+————–+——–+————+
解決辦法:
殺掉原始慢查詢 sql 即可!
xtrace 版本 2.2 可加參數 –lock-wait-query-type=all
xtrace 版本 2.4 可加參數 –ftwrl-wait-query-type
該選項表示獲得全局鎖之前允許那種查詢完成,默認是 ALL,可選 update。
原因:
在 flush tables with read lock 成功獲得鎖之前,必須等待所有語句執行完成(包括 SELECT)。所以如果有個慢查詢在執行,或者一個打開的事務,或者其他進程拿著表鎖,flush tables
with read lock 就會被阻塞,直到所有的鎖被釋放。
The thread got a notification that the underlying structure for a table has changed
and it needs to reopen the table to get the new structure.
However, to reopen the table,
it must wait until all other threads have closed the table in question.
This notification takes place if another thread has used FLUSH TABLES
or one of the following statements on the table in question:
FLUSH TABLES tbl_name, ALTER TABLE, RENAME TABLE, REPAIR TABLE, ANALYZE TABLE, orOPTIMIZE TABLE.
以上就是 Mysql 從庫大量 select 堵塞處于 Waiting for table flush 狀態該怎么辦,丸趣 TV 小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注丸趣 TV 行業資訊頻道。