今天早上一上班,有同事就反映公司好幾個(gè)網(wǎng)站都打不開(kāi),登陸數(shù)據(jù)庫(kù)
專注于為中小企業(yè)提供網(wǎng)站制作、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)寧安免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千余家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
服務(wù)器(windows),發(fā)現(xiàn)很卡,于是重啟了下服務(wù)器,進(jìn)入系統(tǒng)后,沒(méi)過(guò)一會(huì)問(wèn)題依舊,查看了下系統(tǒng)進(jìn)程,發(fā)現(xiàn)MySQL占用率達(dá)到99%,可以肯定的是mysql連接出現(xiàn)問(wèn)題:
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
... ...
根據(jù)TCP協(xié)議定義的3次握手?jǐn)嚅_(kāi)連接規(guī)定,發(fā)起socket主動(dòng)關(guān)閉的一方 socket將進(jìn)入TIME_WAIT狀態(tài),TIME_WAIT狀態(tài)將持續(xù)2個(gè)MSL(Max Segment Lifetime),在Windows下默認(rèn)為4分鐘,即240秒,TIME_WAIT狀態(tài)下的socket不能被回收使用. 具體現(xiàn)象是對(duì)于一個(gè)處理大量短連接的服務(wù)器,如果是由服務(wù)器主動(dòng)關(guān)閉客戶端的連接,將導(dǎo)致服務(wù)器端存在大量的處于TIME_WAIT狀態(tài)的socket, 甚至比處于Established狀態(tài)下的socket多的多,嚴(yán)重影響服務(wù)器的處理能力,甚至耗盡可用的socket,停止服務(wù). TIME_WAIT是TCP協(xié)議用以保證被重新分配的socket不會(huì)受到之前殘留的延遲重發(fā)報(bào)文影響的機(jī)制,是必要的邏輯保證.
在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters,添加名為TcpTimedWaitDelay的
DWORD鍵,設(shè)置為60,以縮短TIME_WAIT的等待時(shí)間
登陸到web服務(wù)器(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
發(fā)現(xiàn)系統(tǒng)存在大量TIME_WAIT狀態(tài)的連接,通過(guò)調(diào)整內(nèi)核參數(shù)解決,
vi /etc/sysctl.conf
編輯文件,加入以下內(nèi)容:net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
然后執(zhí)行
/sbin/sysctl -p
讓參數(shù)生效。
net.ipv4.tcp_syncookies = 1 表示開(kāi)啟SYN Cookies。當(dāng)出現(xiàn)SYN等待隊(duì)列溢出時(shí),啟用cookies來(lái)處理,可防范少量SYN***,默認(rèn)為0,表示關(guān)閉;
net.ipv4.tcp_tw_reuse = 1 表示開(kāi)啟重用。允許將TIME-WAIT sockets重新用于新的TCP連接,默認(rèn)為0,表示關(guān)閉;
net.ipv4.tcp_tw_recycle = 1 表示開(kāi)啟TCP連接中TIME-WAIT sockets的快速回收,默認(rèn)為0,表示關(guān)閉。
net.ipv4.tcp_fin_timeout 修改系統(tǒng)默認(rèn)的 TIMEOUT 時(shí)間
修改之后,再用
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
發(fā)現(xiàn)大量的TIME_WAIT 已不存在,mysql進(jìn)程的占用率很快就降下來(lái)的,各網(wǎng)站訪問(wèn)正常?。?/p>
以上只是暫時(shí)的解決方法,最后仔細(xì)巡查發(fā)現(xiàn)是前天新上線的一個(gè)系統(tǒng),程序代碼中沒(méi)有使用mysql.colse(),才導(dǎo)致大量的mysql TIME_WAIT