临时解决系统中大量的TIME_WAIT连接
今天,偶然间发现后台服务与数据库之间有大量的TIME_WAIT的连接:
[root@localhost logs]# netstat -an | grep TIME_WAIT tcp 0 0 a.a.a.a:54709 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:80 c.c.c.c:37811 TIME_WAIT tcp 0 0 a.a.a.a:54700 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54702 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54693 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54699 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54711 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54701 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54712 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54694 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54692 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54707 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54703 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54708 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54697 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54696 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54705 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54698 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54704 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54695 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54706 b.b.b.b:3306 TIME_WAIT tcp 0 0 a.a.a.a:54713 b.b.b.b:3306 TIME_WAIT
对于这种情况,临时解决办法就是修改内核参数:
vi /etc/sysctl.conf
编辑文件,加入以下内容:
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
然后执行 /sbin/sysctl -p
让参数生效。net.ipv4.tcp_syncookies = 1 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
net.ipv4.tcp_fin_timeout 修改系統默认的 TIMEOUT 时间
修改后,再次查看:
[root@rj-01 logs]# netstat -an | grep 3306 tcp 0 0 a.a.a.a:44396 b.b.b.b:3306 ESTABLISHED tcp 0 0 a.a.a.a:44667 b.b.b.b:3306 ESTABLISHED tcp 0 0 a.a.a.a:44665 b.b.b.b:3306 ESTABLISHED tcp 0 0 a.a.a.a:44666 b.b.b.b:3306 ESTABLISHED tcp 0 0 a.a.a.a:44668 b.b.b.b:3306 ESTABLISHED tcp 0 0 a.a.a.a:44664 b.b.b.b:3306 ESTABLISHED tcp 0 0 a.a.a.a:44397 b.b.b.b:3306 ESTABLISHED
发现之间到数据库的TIME_WAIT连接不存在了。
PS:尽管可以解决TIME_WAIT问题,但是这里建议还是需要找到产生TIME_WAIT的本质原因,比如应用系统中哪里忘记调用close方法等。
-------------------------
No pains, no gains
posted on 2016-05-26 17:01 CasonChan 阅读(1673) 评论(0) 编辑 收藏 举报