MySQL报Too many connections

错误信息

 1 Exception in thread "main" java.sql.SQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
 2     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
 3     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
 4     at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
 5     at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
 6     at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:455)
 7     at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
 8     at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
 9     at java.sql.DriverManager.getConnection(DriverManager.java:664)
10     at java.sql.DriverManager.getConnection(DriverManager.java:247)

错误原因

mysql默认的连接为100个,如果连接数超过该值,则会报Too many connections

另外一个原因是没有及时关闭数据库连接

解决办法

1.使用数据库连接之后,一定要及时关闭数据库连接

2.如果确实需要一定数量的数据库连接,可修改配置文件修改数据库连接数量

修改/etc/my.cnf文件

[mysqld]新添加一行如下参数:

max_connections=4096

你可能感兴趣的:(MySQL报Too many connections)