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

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

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命令
mysql超时:The last packet successfully received from the server was 172,848,658 milliseconds ago._第1张图片

第三种方式(转)

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

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

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"/>
  • 如果是在Spring中使用c3p0连接池,则在定义datasource的时候,添加属性testConnectionOnCheckin和testConnectionOnCheckout,如:
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"/>

你可能感兴趣的:(【java】,java)