想必大家在用MySQL时都会遇到连接超时的问题,如下图所示:
就是这个异常(com.mysql.jdbc.exceptions.jdbc4.Communication***ception:Communications link failure Last packet sent to the server was X ms ago),是由于MySQL服务在长时间不连接之后断开了,断开之后的首次请求会抛出这个异常。那么既然是连接超时的问题,就要去MySQL中探究一下连接时间是怎么控制的。打开MySQL的控制台,运行:show variables like ‘%timeout%’,查看和连接时间有关的MySQL系统变量,得到如下结果:
其中wait_timeout就是负责超时控制的变量,其时间为长度为28800s,就是8个小时,那么就是说MySQL的服务会在操作间隔8小时后断开,需要再次重连。也有用户在URL中使用jdbc.url=jdbc:mysql://localhost:3306/nd?autoReconnect=true来使得连接自动恢复,当然了,这是可以的,不过是MySQL4及其以下版本适用。MySQL5中已经无效了,必须调整系统变量来控制了。MySQL5手册中对两个变量有如下的说明:
interactive_timeout:服务器关闭交互式连接前等待活动的秒数。交互式客户端定义为在mysql_real_connect()中使用CLIENT_INTERACTIVE选项的客户端。又见wait_timeout
wait_timeout:服务器关闭非交互连接之前等待活动的秒数。在线程启动时,根据全局wait_timeout值或全局interactive_timeout值初始化会话wait_timeout值,取决于客户端类型(由mysql_real_connect()的连接选项CLIENT_INTERACTIVE定义),又见interactive_timeout
如此看来,两个变量是共同控制的,那么都必须对他们进行修改了。继续深入这两个变量wait_timeout的取值范围是1-2147483(Windows),1-31536000(linux),interactive_time取值随wait_timeout变动,它们的默认值都是28800。
MySQL的系统变量由配置文件控制,当配置文件中不配置时,系统使用默认值,这个28800就是默认值。要修改就只能在配置文件里修改。Windows下在%MySQL HOME%/bin下有mysql.ini配置文件,打开后在如下位置添加两个变量,赋值。(这里修改为388000)
保存退出,重启mysql服务,一定是重启系统服务。便可看到修改结果:
Linux系统下的配置文件为/etc/my.cnf。需要多说一点的是:windows下的配置文件具体是哪个需要从windows系统服务中找到mysql,打开属性,看“可执行文件路径”里面的参数值,因为它可能是my.cnf而不是my.ini,这是由于安装时的设置,我们可能会忽略它。
至此,修改完成,那么连接超时的问题从数据库上就解决了。当然在编程时也可以使用连接池设置超时时间,配置相对简单。但修改数据库更为本质一些。
http://blog.chinaunix.net/uid-116213-id-3439368.html
相关推荐
@[Android studio通过jdbc连接mysql基本步骤 以及 遇到的坑“The last packet sent successfully to the server was 0 milliseconds ago”哈哈] 小白第一次发博客哈哈,记录一下这三个晚上来我的悲惨经历以及我成功...
Cause com.mysql.jdbc.exceptions.jdbc4.CommunicationsException The last packet successfully received from the server was 47,795,922 ... The last packet sent successfully to the server was 47,795,922
代码如下:Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0 很明显这是连接初始化阶段就丢失了连接的错误。 google半天大多是说的注释掉配置文件中 bind-address = ...
The packet switch uses the destination IP address in the packet to determine the outgoing link. Asking which road to take is analogous to a packet asking which outgoing link it should be forwarded on...
主要介绍了Lost connection to MySQL server at 'reading authorization packet', system error: 0错误解决方法,需要的朋友可以参考下
There are different queuing strategies, and these determine not only the performance of the interconnection network, but also the set of requirements to make them work effectively and their cost....
2.查看数据库表创建完成后,我们可以查看数据库表show tables 2.修改表记录2.1 不带条件的update 表名 set 字段名=值, 字段名=值,
Carve out data in a packet to retrieve the actual files sent across the network Graph traffic patterns to visualize the data flowing across your network Use advanced Wireshark features to understand ...
使用Navicat远程服务器mysql数据库时报错误:2013-Lost connection to MYSQL server at ‘waitting for initial communication packet’,system error:0 操作流程一、检验Mysql数据库是否安装成功二、对Mysql的配置...
mysql5.7 修改max_allowed_packet方法
如果已经设置Mysql/Bin环境变量,直接在CMD里输入命令,如果没有设置Mysql环境变量,去Mysql安装目录的Bin文件夹里运行 F:\AppServ\MySQL\bin\mysql.exe -u root -p 其中root是用户名,然后就可以进入Mysql命令行...
1、Caused by: ... The last packet sent successfully to the server was 56,201,339 milliseconds ago. is longer than the server configured value of ‘wait_timeout’
Lost connection to MySQL server at ‘reading initial communication packet’ 错误解决 上次解决了这个问题,今天又碰到,突然失忆,又做了一番无用功后终于搞定,这次一定要记录下来,免得下次又浪费时间 1、...
Packet Guide to Routing and Switching
compose up 请注意,第一次运行时,您可能会看到: mvw_flyway | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. (Caused by...
The "ptsaplayer.dll" in the .zip package is the patch to fix the issue that Packet Tracer 7.2.2's PTSAPlayer plugin does not work on Windows 7. Apply the patch will fix the issue. Only users using...
融合多个面上光强信息的混合迭代相位恢复算法
MySQL max_allowed_packet过小引起的问题报错如下com.mysql.jdbc.PacketTooBigException: Packet
PacketSender,TCPUDP,64BIT