惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Know Your Adversary
Know Your Adversary
D
Docker
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
V
V2EX
V
Visual Studio Blog
J
Java Code Geeks
博客园 - 【当耐特】
罗磊的独立博客
量子位
W
WeLiveSecurity
博客园 - 聂微东
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
O
OpenAI News
PCI Perspectives
PCI Perspectives
The Last Watchdog
The Last Watchdog
S
Schneier on Security
博客园 - Franky
WordPress大学
WordPress大学
T
Tor Project blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Security Latest
Security Latest
Cisco Talos Blog
Cisco Talos Blog
腾讯CDC
美团技术团队
博客园 - 叶小钗
www.infosecurity-magazine.com
www.infosecurity-magazine.com
月光博客
月光博客
S
Secure Thoughts
Engineering at Meta
Engineering at Meta
T
Tailwind CSS Blog
TaoSecurity Blog
TaoSecurity Blog
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
D
DataBreaches.Net
Project Zero
Project Zero
S
SegmentFault 最新的问题
C
Cisco Blogs
H
Help Net Security
Google Online Security Blog
Google Online Security Blog
A
About on SuperTechFans
F
Fortinet All Blogs
博客园 - 司徒正美

崎径 其镜

Unity CVE-2025-59489 漏洞修复实践(Google Play 合规) 从零配置 VS Code C++ 环境 力扣笔记 一文详解Hexo 博客搭建 Unity 游戏的 Google Play 16 kb页面对齐处理 Unity 升级到 2022 踩坑记录(URP / 黑屏 / HTTP) Android Google Play 16 KB 页面对齐适配指南 iOS 应用开启包外存储访问(文件共享) xlua学习笔记 Lua新知 EFK日志分析系统的搭建 使用贝塞尔曲线实现道具随机飞动效果 震惊,JS不加分号会造成错误!? Linux升级Python Github图床工具 JS使用replace()函数全部替换 JS使用Splice()函数操作数组 安卓应用闪屏 安卓各渠道SDK接入体验 某微信爬虫工具多开方案 U3D问题总结(七) lua U3D问题总结(六) 优化 U3D问题总结(五) 渲染与光照 U3D问题总结(四) 物理相关 U3D问题总结(三) Unity基础
当你的程序连接Mysql然后崩溃时
Anqi Zhao · 2019-11-17 · via 崎径 其镜

之前写过一个监控mysql数据库更新状态的预警程序,总是莫名其妙的报一个连接错误的错,然后程序死掉。后来在系统趋于稳定之后,我就没再继续维护这个工具了。

但是最近我在写另一个工具时,遇到了一个奇怪的问题,就是:tick总在27000多左右的时候崩溃。

我进行了一系列的猜测,比如tick的代码,或者是逻辑有问题,最后我把思路放在了之前遇到的这个错误上。查阅资料后发现,MySQL数据库在连接之后,如果超过一个设定的时间戳之后,会断开。这个值叫WAIT_TIMEOUT,默认值是28800,也就是说如果连上MySQL数据库之后,8小时内没有进行操作,这个连接便会断开。

网上很多连接MySQL数据库的代码没有处理过超时连接的问题,就连JS的官方代码好像也是在17年之后才更新的。以往这个问题,大家都是通过修改这个值来进行规避的。比如改成300天。修改有两种方式,一种是修改配置文件,这样在启动时便会使用这个配置;另一种是修改这个值,或者全局,或者当次生效。

下面是我在使用JavaScript语言链接MySQL数据库时,处理超时重连问题的代码:

this.config = {
"host": "x.x.x.x",
"port": xxxx,
"user": "root",
"password": "pass",
"database": "name""
}

async connect() {
let self = this;
console.log("connect mysql success with", JSON.stringify(this.config))
// 创建连接
this.db_mysql = mysql.createConnection(
this.config
);
// 连接数据库
await this.db_mysql.connect();
// 错误处理
this.db_mysql.on('error', function(err) {
if (err) {
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
// 处理超时
console.warning("start reconnect mysql");
self.connect();
} else {
console.error(err.stack || err);
console.warning("start reconnect mysql");
self.connect();
}
}
});
}