MySQL: ERROR 1040: Too many connections”的异常情况1
2019獨角獸企業重金招聘Python工程師標準>>>
很多開發人員都會遇見”MySQL: ERROR 1040: Too many connections”的異常情況,造成這種情況的一種原因是訪問量過高,MySQL服務器抗不住,這個時候就要考慮增加從服務器分散讀壓力;另一種原因就是MySQL配置文件中max_connections值過小。
首先,我們來查看mysql的最大連接數:
?
| 1 2 3 4 5 6 7 | mysql> show variables like '%max_connections%'; +-----------------+-------+ | Variable_name? | Value | +-----------------+-------+ | max_connections | 151? | +-----------------+-------+ 1 row in set (0.00 sec) |
其次,查看服務器響應的最大連接數:
?
| 1 2 3 4 5 6 7 | mysql> show global status like 'Max_used_connections'; +----------------------+-------+ | Variable_name??? | Value | +----------------------+-------+ | Max_used_connections | 2?? | +----------------------+-------+ 1 row in set (0.00 sec) |
可以看到服務器響應的最大連接數為2,遠遠低于mysql服務器允許的最大連接數值。
對于mysql服務器最大連接數值的設置范圍比較理想的是:服務器響應的最大連接數值占服務器上限連接數值的比例值在10%以上,如果在10%以下,說明mysql服務器最大連接上限值設置過高。
?
| 1 | Max_used_connections / max_connections * 100% = 2/151 *100% ≈ 1% |
我們可以看到占比遠低于10%(因為這是本地測試服務器,結果值沒有太大的參考意義,大家可以根據實際情況設置連接數的上限值)。
再來看一下自己 linode VPS 現在(時間:2013-11-13 23:40:11)的結果值:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | mysql> show variables like '%max_connections%'; +-----------------+-------+ | Variable_name? | Value | +-----------------+-------+ | max_connections | 151? | +-----------------+-------+ 1 row in set (0.19 sec) mysql> show global status like 'Max_used_connections'; +----------------------+-------+ | Variable_name??? | Value | +----------------------+-------+ | Max_used_connections | 44? | +----------------------+-------+ 1 row in set (0.17 sec) |
這里的最大連接數占上限連接數的30%左右。
上面我們知道怎么查看mysql服務器的最大連接數值,并且知道了如何判斷該值是否合理,下面我們就來介紹一下如何設置這個最大連接數值。
方法1:
?
| 1 2 3 4 5 6 7 8 9 | mysql> set GLOBAL max_connections=256; Query OK, 0 rows affected (0.00 sec) mysql> show variables like '%max_connections%'; +-----------------+-------+ | Variable_name? | Value | +-----------------+-------+ | max_connections | 256? | +-----------------+-------+ 1 row in set (0.00 sec) |
方法2:
修改mysql配置文件my.cnf,在[mysqld]段中添加或修改max_connections值:
max_connections=128
重啟mysql服務即可。
轉載于:https://my.oschina.net/u/2357525/blog/737620
總結
以上是生活随笔為你收集整理的MySQL: ERROR 1040: Too many connections”的异常情况1的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设计模式——建造者模式
- 下一篇: (翻译) MongoDB(2) 数据库和