mysql连接超时怎么处理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql连接超时怎么处理相关的知识,希望对你有一定的参考价值。

mysql_OPT_READ_TIMEOUT 是 MySQL c api 客户端中用来设置读取超时时间的参数。在 MySQL 的官方文档中,该参数的描述是这样的:

    MYSQL_OPT_READ_TIMEOUT (argument type: unsigned int *)The timeout in seconds for each attempt to read from the server. There are retries if necessary, so the total effective timeout value is three times the option value. You can set the value so that a lost connection can be detected earlier than the TCP/IPClose_Wait_Timeout value of 10 minutes.

    也就是说在需要的时候,实际的超时时间会是设定值的 3 倍。但是实际测试后发现实际的超时时间和设置的超时时间一致。

    而具体什么时候发生三倍超时,在文档中没有找到。所以对 MySQL 5.7.20 的源码进行了一些分析。

    使用 GDB 调试代码找了实际与 mysql server 通信的代码,如下:

    请点击输入图片描述

    其中 vio_read() 函数中,使用 recv 和 poll 来读取报文和做读取超时。net_should_retry() 函数只有在发生 EINTR 时才会返回 true。从这段代码来看是符合测试结果的,并没有对读取进行三次重试。只有在读取操作被系统中断打断时才会重试,但是这个重试并没有次数限制。

    从上面代码的分析可以看出,代码的逻辑和文档的描述不符。于是在一顿搜索后,找到了一个 MySQL 的 BUG(Bug #31163)。该 BUG 报告了在 MySQL 5.0 中,MySQL c api 读取的实际超时时间是设置的三倍,与现有文档描述相符。于是对 MySQL 5.0.96 的代码又进行分析。

    同样使用 GDB 找到了通信部分的代码。这次找到了重试三次的代码,如下:

    请点击输入图片描述

    这个版本的 MySQL api 的读写超时是直接使用的 setsockopt 设置的。第一次循环,在 A 点发生了第一次超时(虽然注释写的非阻塞,但是客户端的连接始终是阻塞模式的)。然后在 B 点将该 socket 设置为阻塞模式,C 点这里重置 retry 次数。由于设置了 alarm 第二次以后的循环会直接进入 D 点的这个分支,并且判断循环次数。作为客户端时net->retry_count 始终是 1,所以重试了两次,共计进行了 3 次 vioread 后从 E 点退出函数。

    由上面的分析可知,MySQL 文档对于该参数的描述已经过时,现在的 MYSQL_OPT_READ_TIMEOUT 并不会出现三倍超时的问题。而 Bug #31163 中的处理结果也是将文档中该参数的描述更新为实际读取超时时间是设定时间的三倍。也许是 MySQL 的维护者们在后续版本更新时忘记更新文档吧。

参考技术A 查看mysql server超时时间:
msyql> show global variables like '%timeout%';
设置mysql server超时时间(以秒为单位):
msyql> set global wait_timeout=10;
msyql> set global interactive_timeout=10;
参考技术B 你好,
查看mysql server超时时间:

msyql> show global variables like '%timeout%';

设置mysql server超时时间(以秒为单位):

msyql> set global wait_timeout=10;

msyql> set global interactive_timeout=10;本回答被提问者采纳

MySQL连接超时处理

1.由于MySQL默认是8小时的wait_timeout,当超过8小时的连接时间后,在JAVA中调用将出现如下报错

SEVERE EXCEPTION com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was175588 seconds ago.The last packet sent successfully to the server was 175588 seconds ago, which  is longer than the server configured value of wait_timeout. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property autoReconnect=true to avoid this problem.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3246)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1917)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
    at cn.sm.ApplePack.ApplePack.handle(ApplePack.java:35)
    at cn.sm.DataProcessNSQConsumerApp.handleMessage(DataProcessNSQConsumerApp.java:108)
    at com.sproutsocial.nsq.SubConnection$3.run(SubConnection.java:178)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.SocketException: 断开的管道 (Write failed)
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
    at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3227)
    ... 14 more

 

解决方法有两个:修改MySQL的配置或者设置c3p0的属性

2.可以通过如下语句查看wait_timeout的值:

①查看

show GLOBAL VARIABLES like %timeout%; #全局,和mysql中的my.cnf中的配置一致
show  VARIABLES like %timeout%; #会话

修改(也可以修改my.cnf配置文件并重启达到相同效果)

set GLOBAL interactive_timeout=604800; #24小时

②里面还有一个interactive_timeout,具体区别可以参看《MySQL - wait_timeout与interactive_timeout详解

 

3.配置c3p0

①修改的配置

       <property name="breakAfterAcquireFailure">false</property>
        <property name="testConnectionOnCheckout">false</property>
        <property name="testConnectionOnCheckin">false</property>
        <property name="idleConnectionTestPeriod">3600</property> <!--多长时间检查一次,单位秒-->
        <property name="acquireRetryAttempts">10</property>
        <property name="acquireRetryDelay">1000</property>

需要在每次使用的时候getConnection()从连接池中获取Connection,并在使用完成之后进行close归还到连接池中。

②关于c3p0的每个配置属性的详细信息可参看官网《c3p0官网

关于c3p0的重连配置以及介绍可参看《关于c3p0的重连机制

 

以上。

以上是关于mysql连接超时怎么处理的主要内容,如果未能解决你的问题,请参考以下文章

用C#连接操作MYSQL时,老是会超时,怎么处理SQL语句执超时啊,连接字符串里好像设置不了。

C#连接mysql数据库,怎么设置超时时间

mysql长时间不操作会不会连接超时?怎么改?

mysql连接超时的问题处理

mysql怎么设置超时时间

MySQL连接超时处理