a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 58|回复: 0

[综合辅导] Linux认证:MySQLtimeout分析

[复制链接]
发表于 2012-8-4 12:07:07 | 显示全部楼层 |阅读模式
“And God said, Let there be network: and there was timeout”   在使用MySQL的过程中,你是否遇到了众多让人百思不得其解的Timeout?* Z1 I$ C* a1 N! @: j  x
  那么这些Timeout之后,到底是代码问题,还是不为人知的匠心独具?
1 V& J: M. U% |/ F, k& k0 L  本期Out-man,讲述咱们MySQL DBA自己的Timeout。! A7 D6 N' \! x0 F$ R# Z' f
  先看一下比较常见的Timeout参数和相关解释:
' S4 H- _7 Z4 \# z' x  connect_timeout. p1 \, i, t6 o# v1 L* C% F9 v
  The number of seconds that the mysqld server waits for a connect packet before responding with Bad handshake.
* K0 J8 d' A: z% r) p  interactive_timeout# W- F: J. w' T
  The number of seconds the server waits for activity on an interactive connection before closing it.
; ~: L/ ]+ f% v+ o) M8 f' }  wait_timeout
  I4 e% `7 n& o4 `/ q/ k  The number of seconds the server waits for activity on a noninteractive connection before closing it.
0 |* h. W0 e1 e  net_read_timeout
* G# d  Q" s& f2 W3 C  The number of seconds to wait for more data from a connection before aborting the read.
8 q- J/ o1 f# d; v" T  net_write_timeout$ P/ x  G" X" S2 [) O( x9 J  i
  The number of seconds to wait for a block to be written to a connection before aborting the write.) ^( ]% j( G$ J5 c: I
  从以上解释可以看出,connect_timeout在获取连接阶段(authenticate)起作用,interactive_timeout和wait_timeout在连接空闲阶段(sleep)起作用,而net_read_timeout和net_write_timeout则是在连接繁忙阶段(query)起作用。
( _- L+ W& P" z8 ~  获取MySQL连接是多次握手的结果,除了用户名和密码的匹配校验外,还有IP->HOST->DNS->IP验证,任何一步都可能因为网络问题导致线程阻塞。为了防止线程浪费在不必要的校验等待上,超过connect_timeout的连接请求将会被拒绝。# t2 J. A: E: s/ K: p
  即使没有网络问题,也不能允许客户端一直占用连接。对于保持sleep状态超过了wait_timeout(或interactive_timeout,取决于CLIENT_INTERACTIVE标志)的客户端,MySQL会主动断开连接。
. v' c- z! _4 }) ]& U& I% E9 x  即使连接没有处于sleep状态,即客户端忙于计算或者存储数据,MySQL也选择了有条件的等待。在数据包的分发过程中,客户端可能来不及响应(发送、接收、或者处理数据包太慢)。为了保证连接不被浪费在无尽的等待中,MySQL也会选择有条件(net_read_timeout和net_write_timeout)地主动断开连接。" ~' A6 E0 W5 i' u  M7 v) {
  这么多Timeout足以证明MySQL是多么乐于断开连接。而乐于断开连接的背后,主要是为了防止服务端共享资源被某客户端(mysql、mysqldump、页面程序等)一直占用。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-17 09:04 , Processed in 0.494690 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表