mysql超时:The last packet successfully received from the server was 172,848,658 milliseconds ago.

本文介绍了MySQL连接超时的问题及解决方案,包括调整服务器配置、使用JDBC参数和配置连接池验证策略等方法。

今天查询接口报错了。报错内容如下:

The last packet successfully received from the server was 172,848,658 milliseconds ago. The last packet sent successfully to the server was 172,848,673 milliseconds ago. 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.

明明上周是好好的,其实以前我出过这个错误,这次决定要记录下来。
查看一下报错的内容大致是:上次服务器成功接收信息包是172,848,658 毫秒以前,上次服务器成功发送数据是172,848,673毫秒以前,已经超出服务器配置的wait_timeout’超时时间。如果想避免这种异常就需要增加wait_timeout’的超时时间。或者在xml中设置autoReconnect=true

第一种方式

在my.ini中mysqld下面增加
wait_timeout=2147483
interactive_timeout=2147483

最大只能是2147483 解析为24.85天。
大家可以根据自己的需求设置。如果连接人数很多,时间可以适当的减少,连接人数少可以适当增加。

第二种方式

有的同学可能没有my.ini 此时可以输入sql命令
这里写图片描述

第三种方式(转)

  • 如果使用的是JDBC,在JDBC URL上添加?autoReconnect=true,如:

jdbc:mysql://10.10.10.10:3306/mydb?autoReconnect=true
- 如果是在Spring中使用DBCP连接池,在定义datasource增加属性validationQuery和testOnBorrow,如:

<bean id="vrsRankDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${countNew.jdbc.url}" />
    <property name="username" value="${countNew.jdbc.user}" />
    <property name="password" value="${countNew.jdbc.pwd}" />
    <property name="validationQuery" value="SELECT 1" />
    <property name="testOnBorrow" value="true"/>
</bean>
  • 如果是在Spring中使用c3p0连接池,则在定义datasource的时候,添加属性testConnectionOnCheckin和testConnectionOnCheckout,如:
<bean name="cacheCloudDB" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"/>
    <property name="jdbcUrl" value="${cache.url}"/>
    <property name="user" value="${cache.user}"/>
    <property name="password" value="${cache.password}"/>
    <property name="initialPoolSize" value="10"/>
    <property name="maxPoolSize" value="${cache.maxPoolSize}"/>
    <property name="testConnectionOnCheckin" value="false"/>
    <property name="testConnectionOnCheckout" value="true"/>
    <property name="preferredTestQuery" value="SELECT 1"/>
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值