有同事就反映公司好几个网站都打不开,登陆MySQL数据库。服务器(Windows),发现很卡,于是重启了下服务器,进入系统后,没过一会问题依旧,查看了下系统进程,发现MySQL占用率达到99%,可以肯定的是MySQL连接出现问题:
netstat -an
192.168.12.13:3306 192.168.12.12:30443 TIME_WAIT
192.168.12.13:3306 192.168.12.12:30444 TIME_WAIT
192.168.12.13:3306 192.168.12.12:30445 TIME_WAIT
192.168.12.13:3306 192.168.12.12:30446 TIME_WAIT
192.168.12.13:3306 192.168.12.12:30447 TIME_WAIT
192.168.12.13:3306 192.168.12.12:30448 TIME_WAIT
192.168.12.13:3306 192.168.12.12:30449 TIME_WAIT
192.168.12.13:3306 192.168.12.12:30450 TIME_WAIT
192.168.12.13:3306 192.168.12.12:30451 TIME_WAIT
192.168.12.13:3306 192.168.12.12:30452 TIME_WAIT
... ...
根据TCP协议定义的3次握手断开连接规定,发起socket主动关闭的一方 socket将进入TIME_WAIT状态,TIME_WAIT状态将持续2个MSL(Max Segment Lifetime),在Windows下默认为4分钟,即240秒,TIME_WAIT状态下的socket不能被回收使用. 具体现象是对于一个处理大量短连接的服务器,如果是由服务器主动关闭客户端的连接,将导致服务器端存在大量的处于TIME_WAIT状态的socket, 甚至比处于Established状态下的socket多的多,严重影响服务器的处理能力,甚至耗尽可用的socket,停止服务. TIME_WAIT是TCP协议用以保证被重新分配的socket不会受到之前残留的延迟重发报文影响的机制,是必要的逻辑保证.
在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters,添加名为TcpTimedWaitDelay的
DWORD键,设置为60,以缩短TIME_WAIT的等待时间
登陆到Web服务器(Linux):
netstat -ae |grep mysql
tcp 0 0 aaaa:53045 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53044 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53051 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53050 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53049 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53048 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53055 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53054 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53053 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53052 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53059 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53058 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53057 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53056 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53063 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53062 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53061 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53060 192.168.12.3:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53067 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53066 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53065 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53064 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa53071 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53070 192.168.12.13:mysql TIME_WAIT root 0
tcp 0 0 aaaa:53069 192.168.12.13:mysql TIME_WAIT root 0
发现系统存在大量TIME_WAIT状态的连接,通过调整内核参数解决,
vi /etc/sysctl.conf