mysql 8小时问题的解决方法

Use Hibernate + MYSQL database development, link timeout problem:

com.mysql.jdbc.CommunicationsException: The last packet successfully received from the server was58129 seconds ago.The last packet sent successfully to the server was 58129 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.

Checked and the original problem is the mysql timeout if the connection idle for 8 hours (8 hours, no database operations), mysql will automatically disconnect to restart the tomcat.

The solution:

One. If you do not hibernate, then adding in the connection url parameter: autoReconnect = true

jdbc.url = jdbc: mysql: / / ipaddress: 3306/database? autoReconnect = true & autoReconnectForPools = true

Two. With hibernate, then add the following attributes: 
<property name="connection.autoReconnect"> true </ property> 
<property name="connection.autoReconnectForPools"> true </ property> 
<property name="connection.is-connection-validation-required"> true </ property>

III. If still using c3p0 connection pool: 
<property name="hibernate.c3p0.acquire_increment"> 1 </ property> 
<property name="hibernate.c3p0.idle_test_period"> 0 </ property> 
<property name="hibernate.c3p0.timeout"> 0 </ property> 
<property name="hibernate.c3p0.validate"> true </ property>

IV. The worst solution

Using the Connector / J MySQL database connection, the program will run a long time after the reports the following error:

Communications link failure, The last packet successfully received from the server was *** millisecond ago.The last packet successfully sent to the server was *** millisecond ago.

One error will prompt you to change wait_timeout or use the Connector / J‘s autoReconnect property to avoid the error.

Then check out some information, only to find people who experience this problem quite a lot, mostly when using connection pooling will appear this way, short connection should be very difficult to have this problem. The cause of the problem:

MySQL Server default "wait_timeout" is 28,800 seconds or 8 hours, means that if a connection idle time exceed 8 hours, MySQL will automatically disconnect the connection, and connection pool is that the connection is still valid (because the school did not test the effectiveness of the connection), when the application when applying for the connection, it will lead to the above error.

1. In accordance with the error prompt, you can use the autoReconnect JDBC URL attribute, the actual test used autoReconnect = true & failOverReadOnly = false, but did not work, use the 5.1 version, you may really like the Internet before talking about only 4 version of the force.

2. No way, can only modify the parameters of the MySQL, wait_timeout a maximum of 31536000 that is 1 year, add in my.cnf:

[Mysqld]

wait_timeout = 31536000

interactive_timeout = 31536000

Restart effect, you need to modify these two parameters simultaneously.

时间: 2024-10-24 15:18:30

mysql 8小时问题的解决方法的相关文章

连接Mysql提示Can’t connect to local MySQL server through socket的解决方法

mysql,Mysqldump,Mysqladmin,php连接mysql服务常会提示下面错误: 1 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 这是由于修改mysql服务的了socket文件mysql.sock位置,而导致无法通过mysql socket文件连接到mysql服务引起的,具体解决办法如下:1.查看mysql服务的sock

Centos下忘记mysql的root密码的解决方法

Centos下忘记mysql的root密码的解决方法 一:(停掉正在运行的mysql) [[email protected] ~]# service mysql stop 二:使用 “--skip-grant-tables”参数重新启动mysql [[email protected] ~]# mysqld_safe --skip-grant-tables & [1] 23810 Starting mysqld daemon with databases from /var/lib/mysql 三

启动Mysql服务提示Can’t connect to local MySQL server through socket的解决方法

启动Mysql服务常会提示下面错误: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' 这是由于修改mysql服务的了socket文件mysql.sock位置,而导致无法通过mysql socket文件连接到mysql服务引起的,具体解决办法如下: 1.查看mysql服务的socket文件位置:mysql socket文件的位置是在/etc/my.c

PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法

这篇文章主要介绍了PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法,是在进行PHP数据库程序开发中常会遇到的错误,需要的朋友可以参考下 本文实例讲述了PHP提示 Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法,在PHP程序开发中常会遇到这类问题.分享给大家供大家参考,具体的解决方法如下: 将下面代码改为mysqli

关于MySQL的异常处理 Can&#39;t connect to MySQL server on localhost (10061)解决方法

首先检查MySQL 服务没有启动>如果没有启动,则要启动这个服务. 昨天,重起服务器后出现MySQL 'localhost' (10061)错误,开始以为是因为数据库链接打开过多,数据库资源耗尽的缘故,但是重启服务器以后,仍旧出现问题,于是在网上查找解决方法.大体如下: 解决办法: 第一步 删除c:\windows\下面的my.ini 第二步 打开c:\mysql\bin\winmysqladmin.exe 输入用户名 和密码 第三步 在dos下 输入 mysqld-nt -remove 删除服

MySQL连接数超过限制的解决方法

最近网站出现 User 数据库名称 has already more than 'max_user_connections' active connections 的报错,网站瘫痪.有必要研究下这个问题. max_user_connections 是 MySQL 用户连接数的最大值设置,整段语句的意思是:服务器的 MySQL 的最大连接数参数设置不足.解决方法:修改 MySQL 安装目录下 my.ini 或者 my.cnf 文件内的 max_user_connections 参数的数值,重启 M

启动mysql时显示:/tmp/mysql.sock 不存在的解决方法

Can't connect to local MySQL server through socket '/tmp/mysql.sock' 上述提示可能在启动mysql时遇到,即在/tmp/mysql.sock位置找不到所需要的mysql.sock文件,主要是由于my.cnf文件里对mysql.sock的位置设定导致. mysql.sock默认的是在/var/lib/mysql, 如果发现确实是在该目录下,可以在[mysqld]下面加入mysql.sock的pathvi /etc/my.cnf(m

mysql密码更改不了解决方法

mysql密码更改不了解决方法 ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. 今天在MySql5.7操作时报错:You must SET PASSWORD before executing this statement解决方法,需要的朋友可以参考下 ERROR 1820 (HY000): You must SET PASSWO

老男孩教育每日一题-2017年4月28日- MySQL主从复制常见故障及解决方法?

MySQL主从复制常见故障及解决方法? 1.1.1故障1:从库数据与主库冲突 show slave status; 报错:且show slave status\G Slave_I/O_Running:Yes Slave_SQL_Running:No Seconds_Behind_Master:NULL Last_error:Error 'Can't create database 'xiaoliu'; database exists' on query. Default   database:'