mysql连接异常

报错信息:
1,The last packet successfully received from the server was 59,002 milliseconds ago. 
	 The last packet sent successfully to the server was 1 milliseconds ago.
2,Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes,
	 read 0 bytes before connection was unexpectedly lost.	
3,ERROR [com.alibaba.druid.pool.DruidDataSource] - discard connection com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
数据库连接池配置:
	<bean id="emp_dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="url" value="jdbc:mysql://192.168.2.111:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&autoReconnect=true" />
		<property name="username" value="root" />
		<property name="password" value="root" />
		<property name="initialSize" value="1" />
		<property name="minIdle" value="1" />
		<property name="maxActive" value="20" />
		<property name="maxWait" value="60000" />
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<property name="testWhileIdle" value="true" />
		<property name="testOnBorrow" value="true" />
		<property name="testOnReturn" value="false" />
		<property name="poolPreparedStatements" value="true" />
		<property name="maxPoolPreparedStatementPerConnectionSize"
			value="20" />
	</bean>

报错原因:
mysql 连接闲置一定时间后,mysql数据库就会自动断开连接,spring 的 jdcp 连接池中的connection是无效的,导致报错(使用数据库连接工具同样也会报错)
解决方法:

修改连接等待时间,默认是28800,8小时,

1,不重启mysql数据库:
SET GLOBAL interactive_timeout = 31536000;
FLUSH PRIVILEGES;

SET GLOBAL wait_timeout = 31536000;
FLUSH PRIVILEGES;
2,修改配置文件my.cnf,添加下面两个参数,重启mysql数据库:
[mysqld]
interactive_timeout = 31536000
wait_timeout = 31536000
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值