Data source rejected establishment of connection, message from server: "Too many connections&qu

Posted Faron

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Data source rejected establishment of connection, message from server: "Too many connections&qu相关的知识,希望对你有一定的参考价值。

详细错误信息:

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:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)..............................

问题分析:

查看MySQL的当前最大连接数,登录MySQL:mysql -uroot -p,回车;输入密码,回车;

输入命令:select VARIABLE_VALUE from information_schema.GLOBAL_VARIABLES where VARIABLE_NAME=‘MAX_CONNECTIONS‘; 回车

显示当前最大连接数为:151,之前通过文章“CentOS下mysql最大连接数设置 1040 too many connection”中的方法设置为3600了,现在怎么又变成151了?原来这个方法只是临时的修改了最大连接数,重新启动MySQL服务后就还原了。

解决问题:

要彻底解决问题还是要修改my.cnf配置文件,这里使用VI来修改,输入命令:vi /usr/my.cnf 回车;打开文件后按“i”键进入编辑状态;

在“[mysqld]”下面添加“max_connections=3600”,按Esc键进入命令模式,输入“:wq”回车(保存并退出)。

执行:service mysql restart 重新启动MySQL服务;启动服务的时间可能有点长,耐心等待……

 

注意:很多文章中提到在“[mysqld]”下面添加“set-variable=max_connections=1000”,根本不行,加了之后服务就启动不了了。

我这的环境是:CentOS 6.5  MySQL 5.6

 

 

错误原因:

    太多的连接数,登录用户过多,配置的mysql连接数过小,或者某些连接没有关闭,导致连接数过大。

问题的解决:

    修改mysql的my.ini配置文件,网上的说法:mysql安装目录下的my.ini中设定的并发连接数太少或者系统繁忙导致连接数被占满。

    而项目实际上部署在linux系统上,需要找到my.cnf的配置文件,一般在etc/my.cnf,找到这个文件,添加如下行:

    set-variable=max_connections=1000
    set-variable=max_user_connections=500

    set-variable=wait_timeout=200

 

之后重启mysql,生效。

    net stop mysql

    net start mysql

    max_connections: 为设置最大的连接数

    max_user_connections:设置每用户最大的 连接数500

    wait_timeout:表示200秒后将关闭空闲连接,但对正在工作的连接不受影响。

 

    //重新启动MySQL后使用下面的命令查看修改是否成功

    # mysqladmin -uroot -p variables

    Password:

    //可以看到以下项说明修改成功

    | max_connections                 | 1000

    | max_user_connections            | 500

    | wait_timeout                    | 200

解决方法二:

还有一个可能就是代码里打开了太多的连接,但是忘记了在finally块里面关闭,从而导致在处理大数据的时候,抛出异常。下面这样的代码就不会有异常了。

try{			
      conn=Good.getConnection();
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE
   				, ResultSet.CONCUR_UPDATABLE);
      String sql1="insert into cat_garbage values(‘"+rs.getInt("id")+"‘,‘"+rs.getInt("cid")+"‘,‘"+rs.getString("name")+"‘,‘"+rs.getString("keyword")+"‘)";
    stmt.executeUpdate(sql1);
    }
    catch(SQLException|ClassNotFoundException|IOException e)
    {
      e.printStackTrace();
    }
    finally
    {
      if(stmt!= null) 
        stmt.close(); 		
      if(conn!= null) 
        conn.close(); 
    }
要随时记住关闭rs,stmt,conn等资源,学会使用finally,在finally中,这些语句一定会执行


How to increase MySQL connections(max_connections)?

Q: Every socket of MySQL Database will have defaults connections as 100 but I am looking for any way to increase the number of possible connections > 100 to a socket connection of MySQL Database.??

A:

If you need to increase MySQL Connections without MySQL restart do like below

mysql> show variables like ‘max_connections‘;
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 100   |
+-----------------+-------+
1 row in set (0.00 sec)

mysql> SET GLOBAL max_connections = 150;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like ‘max_connections‘;
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 150   |
+-----------------+-------+
1 row in set (0.00 sec)

These settings will change at MySQL Restart.


For permanent changes add below line in my.cnf and restart MySQL

max_connections = 150



以上是关于Data source rejected establishment of connection, message from server: "Too many connections&qu的主要内容,如果未能解决你的问题,请参考以下文章

Data source rejected establishment of connection, message from server: "Too many connections&q

Data source rejected establishment of connection, message from server: "Too many connections&qu

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establish

Data source rejected establishment of connection, message from server: "Too many connections&qu

Data source rejected establishment of connection, message from server: "Too many connections&qu

今天碰到报错 Data source rejected establishment of connection, message from server: "Too many conne