不少开发人员都会碰见”MySQL: ERROR 1040: Too many connections”的异常状况,形成这种状况的一种缘由是访问量太高,MySQL服务器抗不住,这个时候就要考虑增长从服务器分散读压力;另一种缘由就是MySQL配置文件中max_connections值太小。node
首先,咱们来查看mysql的最大链接数:mysql
?sql
1服务器 2测试 3spa 4.net 5code 6htm 7ci |
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服务便可。