MySQL wait_timeout参数设置与网上常见错误小纠

原创 2013年12月02日 18:27:13

应用遇到异常报警:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 23,579 milliseconds ago.  The last packet sent successfully to the server was 0 milliseconds ago.
        at sun.reflect.GeneratedConstructorAccessor40.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1118)
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3055)
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2941)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3489)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113)
        at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1364)
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.execute(NewProxyPreparedStatement.java:989)
        at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQuery(SqlExecutor.java:185)
        at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteQuery(MappedStatement.java:221)
        at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:189)
        ... 36 more
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
        at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2502)
        at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2952)
        ... 47 more


问题比较明显,应用连接已经失效。

23.579s之前创建了一个连接,之后再次使用此数据库连接就超时了。


查看数据库配置:

mysql> show  global  variables like  'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 20    |
+---------------+-------+
1 row in set (0.00 sec)

服务器设置的时间为20s.从而说明上面超时的原因。


解决这个方法:


一:从应用层解决问题:修改连接池参数  缩短连接池内的连接超时时间。


二:从DB层解决问题:增大MySQL的连接失效时间。

mysql> show  global  variables like  'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 20    |
+---------------+-------+
1 row in set (0.00 sec)


动态修改global参数,wait_timeout;

set global wait_timeout=30;


再次查看参数:

mysql> show  global  variables like  'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 30    |
+---------------+-------+
1 row in set (0.00 sec)

设置完毕。


时间的大小设置不能随便,时间过长,导致过多的connection Sleep,占用较多系统资源。时间设置过小也不行。从业务出发,结合查询的耗时与业务吞吐量来进行设置比较合适吧。





常见误解:


网上有很多文章 没有清晰的认识 会话变量与全局变量的不同,查看了会话变量的超时时间,最终可能会导致将全局变量 wait_timeout设置成了31536000s!!


[mysqld]

wait_timeout=31536000


my.cnf里设置的参数是global级别。不是会话级别。


然而:

mysql> show variables like  'wait_timeout';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wait_timeout  | 28800 |
+---------------+-------+
1 row in set (0.00 sec)

是会话级别。有些人会把会话参数误认为全局参数。


正确的参数初始化设置应该为:

[mysqld]

wait_timeout=30 




建议大家今后查询变量的时候不要使用缺省。

查询会话变量:

mysql> show session variables like  '%wait%'; 
+--------------------------+----------+
| Variable_name            | Value    |
+--------------------------+----------+
| innodb_lock_wait_timeout | 50       |
| innodb_spin_wait_delay   | 6        |
| lock_wait_timeout        | 31536000 |
| wait_timeout             | 28800    |
+--------------------------+----------+
4 rows in set (0.00 sec)


查询全局变量:

mysql> show global variables like  '%wait%';        
+--------------------------+----------+
| Variable_name            | Value    |
+--------------------------+----------+
| innodb_lock_wait_timeout | 50       |
| innodb_spin_wait_delay   | 6        |
| lock_wait_timeout        | 31536000 |
| wait_timeout             | 30       |
+--------------------------+----------+
4 rows in set (0.00 sec)


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

mysql Can not read response from server. Expected to read 4 bytes 异常

http://stackoverflow.com/questions/13950496/what-is-java-io-eofexception-message-can-not-read-respon...

数据库报错

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure Last packet sen...

web服务连接mysql报错 解析:Can not read response from server. Expected to read 4 bytes, read 0 bytes before c

报错内容: The last packet successfully received from the server was 3,087,649 milliseconds ago.  The la...

tomcat链接mysql时超时报错java.io.EOFException: Can not read response from server. Expected to read 4 bytes,

需要在配置文件里加上下面就ok了
  • zhq426
  • zhq426
  • 2013年08月06日 10:06
  • 10208

Caused by: java.io.EOFException: Can not read response from server.

1、错误描述The last packet successfully received from the server was 76,997 milliseconds ago. The last p...

关于解决连接mysql数据库因wait_timeout而造成问题的办法

一、出现的问题      我的web工程在隔夜连接mysql数据库,会出现错误如下(截取了关键字),需要重启服务器才能ok。 Communications link failure; The la...
  • lyy396
  • lyy396
  • 2016年01月21日 17:58
  • 3431

Mysql 更改 wait_timeout 配置

现在想更改里面的一个属性wait_timeout,这个变量原来默认值是28800,想改成20秒,但是在my.cnf里面加上之后,show variables还是28800。 注意...

MySQL wait_timeout参数设置与网上常见错误小纠

转自:http://www.th7.cn/db/mysql/201312/37594.shtml

在mysql中connection设置和wait-timeout的设置

1>MYSQL中遇到too   many  connections的解决方案 com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConne...

c3p0数据库连接池死锁问题和mysql重连,连接丢失

最近项目中用的C3P0连接池出现各种bug,现在记录一下。 1、经常报连接池死锁 2016-08-31 15:24:00 [ WARN] - [com.mchange.v2.async.ThreadP...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:MySQL wait_timeout参数设置与网上常见错误小纠
举报原因:
原因补充:

(最多只允许输入30个字)