MySQL的driverClassName、url

你的MySQL总掉线?三招设置让数据库连接稳如泰山!

一、你的数据库是否正在经历这些崩溃瞬间?

  • 凌晨三点告警轰炸:Communications link failure报错刷屏
  • 用户投诉暴增:页面突然弹出"数据库连接已断开"
  • 订单神秘消失:支付成功但订单未生成
  • 运维头发掉光:wait_timeout参数调了N次依然报错


真实案例:某电商大促期间因net_write_timeout设置不当,导致:

每秒丢失笔订单客服投诉量激增%DBA连夜抢救扣绩效


二、四大超时参数全解析(小白也能懂)

连接三阶段与参数对应关系

超时参数对照表

参数名

作用阶段

类比场景

默认值

危险临界点

connect_timeout

连接建立

打电话等待接通

>秒

wait_timeout

连接空闲

通话后忘记挂机

<秒

net_read_timeout

数据接收

等待对方说话

>秒

net_write_timeout

数据发送

等待对方听清

>秒


三、防掉线配置指南(附紧急情况处理)

全局参数设置(生产环境推荐)

-- 连接阶段防护
SET GLOBAL connect_timeout = ;

-- 传输阶段加固  
SET GLOBAL net_read_timeout = ;
SET GLOBAL net_write_timeout = ;

-- 空闲连接管理
SET GLOBAL wait_timeout = ;
SET GLOBAL interactive_timeout = ;

连接字符串关键配置

具体可以查看类:com.mysql.cj.conf.PropertyKey中

connectTimeout:                           //客户端链接超时时间

enableQueryTimeouts:true/false, 默认true        //是否启用sql执行超时线程检查

initialTimeout:                             //重试等待时间

queryTimeoutKillsConnection:true/false, 默认false  //超时后是否正常安全关闭连接

socketTimeout:               //设置数据库socket连接的超时时间

连接池关键配置

spring:
  datasource:
    hikari:
      connection-timeout:   # 等待连接超时
      idle-timeout:        # 空闲连接保留时间
      max-lifetime:       # 连接最大存活时间
      keepalive-time:       # 保活心跳间隔
spring.datasource:
  driverClassName: com.mysql.jdbc.Driver
  url: jdbc:mysql://.xxx.xxx:/database?enableQueryTimeouts=false
  username: 
  password: 
  validationQuery: SELECT 1        //用于检测连接是否有效的SQL语句
  connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=
  initialSize: 1
  minIdle: 
  maxActive: 
  maxWait: 
  timeBetweenEvictionRunsMillis:      //间隔多久检测一次空闲连接(毫秒)
  minEvictableIdleTimeMillis:        //连接池中连接最小空闲时间(毫秒)
  testWhileIdle: true     //是否开启空闲连接的检测
  testOnBorrow: false     //是否开启连接的检测功能,在获取连接时检测连接是否有效
  testOnReturn: false     //是否开启连接的检测功能,在归还连接时检测连接是否有效
  poolPreparedStatements: true
  maxOpenPreparedStatements: 
  removeAbandoned: true      //强制移除无效连接,防止链接泄露
  removeAbandonedTimeout:  //强制回收连接的时限,当程序从池中get到连接开始算起,大于业务时间
  logAbandoned: true   //记录移除日志

突发故障应急方案

四、三大经典报错解决方案

"Statement cancelled due to timeout or client request"

Cause: com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request

; Statement cancelled due to timeout or client request; nested exception is
com.mysql.cj.jdbc.exceptions.MySQLTimeoutException: Statement cancelled due to timeout or client request

? 修复组合拳:

解决办法:

1. 增加MyBatis的超时时间defaultStatementTimeout或者xml中设置

2. 连接字符串enableQueryTimeouts设置为false

"See wait_timeout and interactive_timeout for configuring this behavior"

org.springframework.dao.RecoverableDataAccessException:

### Error querying database. Cause:
com.mysql.cj.jdbc.exceptions.CommunicationsException: The client was disconnected by the server because of inactivity. See wait_timeout and interactive_timeout for configuring this behavior.

? 修复组合拳:

解决办法:
     1. 增加这个时间
     2. 连接池将testOnBorrow,testOnReturn设置为true


"Communications link failure"

Caused by:
org.springframework.dao.RecoverableDataAccessException: ### Error querying database. Cause:
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure The last packet successfully received from the server was 6,, milliseconds ago. The last packet sent successfully to the server was 6,, milliseconds ago.

? 修复组合拳:

解决办法:
     1. 增加net_read_timeout和net_write_timeout时间
     2. 也可以尝试增加MySQL连接字符串中socketTimeout时间
原文链接:,转发请注明来源!