MySQL Timeout解析
為什么80%的碼農都做不了架構師?>>> ??
“And God said, Let there be network: and there was timeout”
在使用MySQL的過程中,你是否遇到了眾多讓人百思不得其解的Timeout?
那么這些Timeout之后,到底是代碼問題,還是不為人知的匠心獨具?
本期Out-man,講述咱們MySQL DBA自己的Timeout。
先看一下比較常見的Timeout參數和相關解釋:
connect_timeout
The number of seconds that the mysqld server waits for a connect packet before responding with Bad handshake.
interactive_timeout
The number of seconds the server waits for activity on an interactive connection before closing it.
wait_timeout
The number of seconds the server waits for activity on a noninteractive connection before closing it.
net_read_timeout
The number of seconds to wait for more data from a connection before aborting the read.
net_write_timeout
The number of seconds to wait for a block to be written to a connection before aborting the write.
從以上解釋可以看出,connect_timeout在獲取連接階段(authenticate)起作用,interactive_timeout和wait_timeout在連接空閑階段(sleep)起作用,而net_read_timeout和net_write_timeout則是在連接繁忙階段(query)起作用。
獲取MySQL連接是多次握手的結果,除了用戶名和密碼的匹配校驗外,還有IP->HOST->DNS->IP驗證,任何一步都可能因為網絡問題導致線程阻塞。為了防止線程浪費在不必要的校驗等待上,超過connect_timeout的連接請求將會被拒絕。
即使沒有網絡問題,也不能允許客戶端一直占用連接。對于保持sleep狀態超過了wait_timeout(或interactive_timeout,取決于CLIENT_INTERACTIVE標志)的客戶端,MySQL會主動斷開連接。
即使連接沒有處于sleep狀態,即客戶端忙于計算或者存儲數據,MySQL也選擇了有條件的等待。在數據包的分發過程中,客戶端可能來不及響應(發送、接收、或者處理數據包太慢)。為了保證連接不被浪費在無盡的等待中,MySQL也會選擇有條件(net_read_timeout和net_write_timeout)地主動斷開連接。
這么多Timeout足以證明MySQL是多么樂于斷開連接。而樂于斷開連接的背后,主要是為了防止服務端共享資源被某客戶端(mysql、mysqldump、頁面程序等)一直占用。
轉載于:https://my.oschina.net/opleo/blog/28704
總結
以上是生活随笔為你收集整理的MySQL Timeout解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: seer2雷伊(seer2)
- 下一篇: MVC小技巧