“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.mysql
从以上解释能够看出,connect_timeout在获取链接阶段(authenticate)起做用,interactive_timeout和wait_timeout在链接空闲阶段(sleep)起做用,而net_read_timeout和net_write_timeout则是在链接繁忙阶段(query)起做用。sql
获取MySQL链接是屡次握手的结果,除了用户名和密码的匹配校验外,还有IP->HOST->DNS->IP验证,任何一步均可能由于网络问题致使线程阻塞。为了防止线程浪费在没必要要的校验等待上,超过connect_timeout的链接请求将会被拒绝。网络
即便没有网络问题,也不能容许客户端一直占用链接。对于保持sleep状态超过了wait_timeout(或interactive_timeout,取决于CLIENT_INTERACTIVE标志)的客户端,MySQL会主动断开链接。post
即便链接没有处于sleep状态,即客户端忙于计算或者存储数据,MySQL也选择了有条件的等待。在数据包的分发过程当中,客户端可能来不及响应(发送、接收、或者处理数据包太慢)。为了保证链接不被浪费在无尽的等待中,MySQL也会选择有条件(net_read_timeout和net_write_timeout)地主动断开链接。spa
这么多Timeout足以证实MySQL是多么乐于断开链接。而乐于断开链接的背后,主要是为了防止服务端共享资源被某客户端(mysql、mysqldump、页面程序等)一直占用。线程