Too Many Connections: How to Increase the MySQL Connection Count To Avoid This Problem

时间:2024-04-25 03:52:40

1.问题描述

  在启动使用mysql数据库的项目时,遇到一个报错,如下:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)

2.问题分析

  字面分析为:mysql数据库中的连接过多,数据库拒绝连接,导致报错;

3.解决

  3.1 减少现有数据库已经连接数量;

  3.2 增大现有数据库可以连接的数量;

4.方法<windows 版>

  减少现有连接肯能导致其它在运行的项目受到影响;

  需增加可以连接的数量;

  4.1 cmd进入shell,登录mysql

  Too Many Connections: How to Increase the MySQL Connection Count To Avoid This Problem

  4.2 mysql默认的链接数为151,mysql 5.7之前的版本为100;下为官方文档解释:

Too Many Connections: How to Increase the MySQL Connection Count To Avoid This Problem

  4.3 查看mysql现有的可以连接数(show variables like "max_connections") 并 设置最大链接数(set global max_connections = 500);

  Too Many Connections: How to Increase the MySQL Connection Count To Avoid This Problem

 4.4 重新启动要启动的项目即可,不会出现too many connections 的报错;

5. 总结

  mysql有连接数量的限制;

  

    

参考资料:

  1. https://www.morpheusdata.com/blog/2014-12-11-too-many-connections-how-to-increase-the-mysql-connection-count-to-avoid-this-problem

  2. https://dev.mysql.com/doc/refman/5.5/en/too-many-connections.html