不少开发人员都会碰见”MySQL: ERROR 1040: Too many connections”的异常状况,形成这种状况的一种缘由是访问量太高,MySQL服务器抗不住,这个时候就要考虑增长从服务器分散读压力;另外一种缘由就是MySQL配置文件中max_connections值太小。
首先,咱们来查看mysql的最大链接数:
mysql
mysql>show variables like '%max_connections%'; + -----------------+-------+ | Variable_name | Value | + -----------------+-------+ | max_connections | 151 | + -----------------+-------+ 1 row in set (0.00 sec)
其次,查看服务器响应的最大链接数:
sql
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服务器最大链接上限值设置太高。
Max_used_connections / max_connections * 100% = 2/151 *100% ≈ 1%
上面咱们知道怎么查看mysql服务器的最大链接数值,而且知道了如何判断该值是否合理,下面咱们就来介绍一下如何设置这个最大链接数值。
方法1:
服务器
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服务便可。
code
mysql>show variables like
'max_connections'
;(查能够看当前的最大链接数)
开发
msyql>set
global
max_connections=1000;(设置最大链接数为1000,能够再次查看是否设置成功)