MySQL报Too many connections

错误信息

Exception in thread "main" java.sql.SQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)

错误原因

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

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

解决办法

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

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

修改/etc/my.cnf文件

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

max_connections=4096

相关推荐