云计算、AI、云原生、大数据等一站式技术学习平台

网站首页 > 教程文章 正文

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

jxf315 2025-04-07 15:16:27 教程文章 35 ℃

数据库连接不稳示意图

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

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


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

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


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

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

2.2 超时参数对照表

参数名

作用阶段

类比场景

默认值

危险临界点

connect_timeout

连接建立

打电话等待接通

10秒

>30秒

wait_timeout

连接空闲

通话后忘记挂机

28800秒

<600秒

net_read_timeout

数据接收

等待对方说话

30秒

>60秒

net_write_timeout

数据发送

等待对方听清

60秒

>120秒


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

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

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

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

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

3.2 连接字符串关键配置

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

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

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

initialTimeout:30                             //重试等待时间

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

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

3.3 连接池关键配置

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

3.4 突发故障应急方案

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

4.1"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

4.2"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


4.3"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,736,635 milliseconds ago. The last packet sent successfully to the server was 6,736,637 milliseconds ago.

修复组合拳:

解决办法:
     1. 增加net_read_timeout和net_write_timeout时间
     2. 也可以尝试增加MySQL连接字符串中socketTimeout时间
最近发表
标签列表