mysql连接的空闲时间超过8小时后 MySQL自动断开该连接解决方案
时间:2024-01-15 21:35:26
解决这个问题的办法有三种:
1. 增加 MySQL 的 wait_timeout 属性的值。
修改 /etc/mysql/my.cnf文件,在 [mysqld] 节中设置:
# Set a connection to wait 8hours in idle status.
wait_timeout =86400
相关参数,红色部分
mysql> show variables like '%timeout%';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| connect_timeout | 5 |
| delayed_insert_timeout | 300 |
| innodb_lock_wait_timeout | 50 |
| interactive_timeout | 28800 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| slave_net_timeout | 3600 |
| wait_timeout | 28800 |
+--------------------------+-------+
同一时间,这两个参数只有一个起作用。到底是哪个参数起作用,和用户连接时指定的连接参数相关,缺省情况下是使用wait_timeout。我建议是将这两个参数都修改,以免引起不必要的麻烦。
这两个参数的默认值是8小时(60*60*8=28800)。我测试过将这两个参数改为0,结果出人意料,系统自动将这个值设置为。换句话说,不能将该值设置为永久。
将这2个参数设置为24小时(60*60*24=604800)即可。
set interactive_timeout=604800;
set wait_timeout=604800;
2. 减少连接池内连接的生存周期,使之小于上一项中所设置的 wait_timeout 的值。
修改 c3p0 的配置文件,设置:
# How long to keep unused connections around(in seconds)
# Note: MySQL times out idle connections after 8hours(28,800seconds)
# so ensure this value is below MySQL idle timeout
cpool.maxIdleTime=25200
在 Spring 的配置文件中:
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="maxIdleTime"value="${cpool.maxIdleTime}"/>
<!--other properties -->
</bean>
3. 定期使用连接池内的连接,使得它们不会因为闲置超时而被 MySQL 断开。
修改 c3p0 的配置文件,设置:
# Prevent MySQL raise exception after a long idle timecpool.preferredTestQuery='SELECT 1'cpool.idleConnectionTestPeriod=18000cpool.testConnectionOnCheckout=true
修改 Spring 的配置文件:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="preferredTestQuery" value="${cpool.preferredTestQuery}"/>
<property name="idleConnectionTestPeriod" value="${cpool.idleConnectionTestPeriod}"/>
<property name="testConnectionOnCheckout" value="${cpool.testConnectionOnCheckout}"/>
<!--other properties --></bean>
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中django学习心得
![](https://img.aspxhome.com/file/2023/2/89322_0s.png)
MySQL数据库的授权原则
python中可以发生异常自动重试库retrying
![](https://img.aspxhome.com/file/2023/0/110020_0s.png)
解决SQL Server的“此数据库没有有效所有者”问题
Python calendar模块详情
![](https://img.aspxhome.com/file/2023/9/67799_0s.png)
python使用正则表达式的search()函数实现指定位置搜索功能
asp.net 字符串、二进制、编码数组转换函数
php支持中文字符串分割的函数
也谈谈DIV+CSS的牛角尖
探讨如何配置SQL2008,让其允许C#远程外部连接的方法详解
Python统计文件中去重后uuid个数的方法
了解CSS的查找匹配原理,让CSS更简洁、高效
Python中整数的缓存机制讲解
python程序中调用其他程序的实现
Golang操作Kafka的实现示例
python将人民币转换大写的脚本代码
python调用matlab的m自定义函数方法
解读Opencv中Filter2D函数的补全方式
![](https://img.aspxhome.com/file/2023/5/98885_0s.png)
Python使用多进程运行含有任意个参数的函数
![](https://img.aspxhome.com/file/2023/1/123831_0s.jpg)