標籤:and connected nod row err com strong key line
很多開發人員都會遇見”MySQL: ERROR 1040: Too many connections”的異常情況,造成這種情況的一種原因是訪問量過高,MySQL伺服器抗不住,這個時候就要考慮增加從伺服器分散讀壓力;另一種原因就是MySQL設定檔中max_connections值過小。
首先,我們來查看mysql的最大串連數:
?
| 1234567 |
mysql> show variables like ‘%max_connections%‘;+-----------------+-------+| Variable_name | Value |+-----------------+-------+| max_connections | 151 |+-----------------+-------+1 row in set (0.00 sec) |
其次,查看伺服器響應的最大串連數:
?
| 1234567 |
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)的結果值:
?
| 1234567891011121314 |
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:
?
| 123456789 |
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服務即可。
查看當前串連的數量
show status;
Threads_connected 當前串連數
Threads_created 建立的串連數
mysql最佳化串連數