Home  >  Article  >  Database  >  mysql 8小时解决方案_MySQL

mysql 8小时解决方案_MySQL

WBOY
WBOYOriginal
2016-06-01 13:36:351016browse

bitsCN.com

mysql 8小时解决方案

 

解决这个问题的办法有三种: 

 

1. 增加 MySQL 的 wait_timeout 属性的值。 

修改 /etc/mysql/my.cnf文件,在 [mysqld] 节中设置: 

 

Java代码 


mysql 8小时解决方案_MySQL

 

同一时间,这两个参数只有一个起作用。到底是哪个参数起作用,和用户连接时指定的连接参数相关,缺省情况下是使用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 的配置文件中:

代码如下:

Java代码  

class="com.mchange.v2.c3p0.ComboPooledDataSource">  

 

 

  

 

3. 定期使用连接池内的连接,使得它们不会因为闲置超时而被 MySQL 断开。 

修改 c3p0 的配置文件,设置:

 

# Prevent MySQL raise exception after a long idle timecpool.preferredTestQuery='SELECT 1'cpool.idleConnectionTestPeriod=18000cpool.testConnectionOnCheckout=true 

修改 Spring 的配置文件:

代码如下:

Java代码  

 

 

 

 

  

 

bitsCN.com
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn