1max_connectionsMySQL的最大连接数,增加该值增加mysqld 要求的文件描述符的数量。如果服务器的并发连接请求量比较大,建议调高此值,以增加并行连接数量,当然这建立在机器能支撑的情况下,因为如果连接数越多,介于MySQL会为每个连接提供连接缓冲区,就会开销越多的内存,所以要适当调整该值,不能盲目提高设值。
数值过小会经常出现ERROR 1040: Too many connections错误,可以过’conn%’通配符查看当前状态的连接数量,以定夺该值的大小。
1.1出现的异常
18:38:11.262 [main] ERROR com.alibaba.druid.pool.DruidDataSource - {dataSource-10} init error
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
1.2 查看max_used_connections和max_connections
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.00 sec)
mysql> show status like 'max%connections';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 152 |
+----------------------+-------+
1 row in set (0.00 sec)
max_used_connections / max_connections * 100% (理想值≈ 85%)
如果max_used_connections跟max_connections相同 那么就是max_connections设置过低或者超过服务器负载上限了,低于10%则设置过大。
这里可以选择把max_connections设置为200
1.3设置max_connections的值
1.在配置文件my.cnf中设置max_connections的值
找到max_connections一行,修改为(如果没有,则自己添加),
上面的1000即该参数的值。需要重启mysql
2、实时(临时)修改此参数的值
首先登陆mysql,执行如下命令:
mysql> set GLOBAL max_connections=200;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 200 |
+-----------------+-------+
1 row in set (0.00 sec)
修改后就不再报刚才的错了
mysql> show status like 'max%connections';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| Max_used_connections | 165 |
+----------------------+-------+
1 row in set (0.00 sec)