最近发现 mysql经常挂掉,后来看了netstat发现:
[admin@server5 ~]$ netstat -an | grep 3306 | grep 'ESTABLISHED'|wc -l
33
[admin@server5 ~]$ netstat -an | grep 3306 | grep 'TIME_WAIT'|wc -l
1677
登陆到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
编辑文件,加入以下内容:
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 时间
net.ipv4.tcp_max_tw_buckets = 1000
修改之后,再用
netstat -ae|grep mysql
tcp 0 0 aaaa:50408 192.168.12.13:mysql ESTABLISHED nobody 3224651
tcp 0 0 aaaa:50417 192.168.12.13:mysql ESTABLISHED nobody 3224673
tcp 0 0 aaaa:50419 192.168.12.13:mysql ESTABLISHED nobody 3224675
发现大量的TIME_WAIT 已不存在,mysql进程的占用率很快就降下来的,各网站访问正常!!
以上只是暂时的解决方法,最后仔细巡查发现是前天新上线的一个系统,程序代码中没有使用mysql.colse(),才导致大量的mysql TIME_WAIT
kernel.msgmnb = 65536
# Controls the default maxmimum size of a mesage queue
kernel.msgmax = 65536
# Controls the maximum shared segment size, in bytes
kernel.shmmax = 68719476736
# Controls the maximum number of shared memory segments, in pages
kernel.shmall = 4294967296
net.core.wmem_max=12582912
net.core.rmem_max=12582912
net.ipv4.tcp_rmem= 10240 87380 12582912
net.ipv4.tcp_wmem= 10240 87380 12582912
net.ipv4.tcp_mem= 10240 87380 12582912
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_sack = 0
#net.ipv4.tcp_sack = 1
net.ipv4.tcp_fack = 1
net.ipv4.tcp_no_metrics_save = 1
net.core.netdev_max_backlog = 30000
net.core.somaxconn = 30000
net.ipv4.tcp_fin_timeout = 8
net.ipv4.tcp_keepalive_intvl = 20
#net.ipv4.tcp_keepalive_intvl = 11
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_time = 20
#net.ipv4.tcp_keepalive_time = 8
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
net.ipv4.ip_local_port_range = 32768 65535
net.ipv4.tcp_max_syn_backlog = 30000
net.ipv4.tcp_max_tw_buckets = 1000