PHP 出現下述錯誤訊息:
PHP Warning: mysql_connect(): Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (99)
但是追蹤起來, 程式執行時, 機器、DB 都沒有什麼 Loading, 於是從設定追, 程式大概都跑約 60秒就掛掉, 於是把設定於60秒的都抓出來看, 再測試看看, 也沒有任何差異, 一樣會掛掉.
Can't connect to MySQL server on (99) 解法
追查到的主因在於 Connection 太多, 於是 TCP (Port) 不夠用, 於是就會出現此錯誤訊息, 解法如下:
先查看系統預設值
- $ sudo sysctl -a | grep tcp_tw
net.ipv4.tcp_tw_recycle = 0
net.ipv4.tcp_tw_reuse = 0 - $ sudo sysctl -a | grep tcp_syncookies
net.ipv4.tcp_syncookies = 1 - $ sudo sysctl -a | grep tcp_fin_timeout
net.ipv4.tcp_fin_timeout = 60
解法: (於 Server 設定即可)
- vim /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= 5 - sudo sysctl -p # 讓設定能動, -p, --load[=<file>] read values from file
相關網頁
- MySQL Lists: mysql: Re: error 99(?) : can't connect to MySQL server
- Are PHP persistent connections evil ? - 下述摘錄自此文:
On Linux you can adjust “/proc/sys/net/ipv4/ip_local_port_range” to get more local ports available and “/proc/sys/net/ipv4/tcp_fin_timeout” to reduce recycle delay.
- 關於流量升高導致TIME_WAIT增加,MySQL連接大量失敗的問題 « 搜索技術博客-淘寶