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

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
L
LINUX DO - 最新话题
P
Privacy & Cybersecurity Law Blog
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
PCI Perspectives
PCI Perspectives
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
TaoSecurity Blog
TaoSecurity Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Attack and Defense Labs
Attack and Defense Labs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
小众软件
小众软件
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
F
Fortinet All Blogs
T
Threatpost
Security Latest
Security Latest
V
Vulnerabilities – Threatpost
B
Blog RSS Feed
MongoDB | Blog
MongoDB | Blog
WordPress大学
WordPress大学
Forbes - Security
Forbes - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
C
CERT Recently Published Vulnerability Notes
AI
AI
博客园 - 三生石上(FineUI控件)
T
Threat Research - Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
Schneier on Security
I
Intezer
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
V2EX
aimingoo的专栏
aimingoo的专栏
量子位
NISL@THU
NISL@THU
N
News and Events Feed by Topic
O
OpenAI News
Microsoft Azure Blog
Microsoft Azure Blog
C
Check Point Blog
Schneier on Security
Schneier on Security
S
Secure Thoughts
The Cloudflare Blog
J
Java Code Geeks

博客园 - 皮皮虾的blog

visual studio 2022 安装net旧版本(net framework4.0和4.5) RxJS中高阶映射操作符的全面讲解:switchMap, mergeMap, concatMap (and exhaustMap) 从0到1: Angular + .NET Core 前后端分离项目Compass的开发过程 rxjs学习 - 2 rxjs学习 Asynchronous programming with async and await Angular 表单介绍 TypeScript null undefine判断 TypeScript 常见方法 VMware虚拟机如何设置CentOS 7 磁盘扩容? MySQL 5.7 配置 MySQL 主从复制相关 Mysql 常用命令 MySQL 5.7 CPU高的定位方法 VS2019 设置tab插入空格不好使的解决方法 CentOS 7 MySQL 5.7 主从设置 VMware安装CentOS7后配置静态IP MySQL explain,type分析(转) - 2 MySQL explain,Extra分析(转) - 1
MySQL 5.7 Sleep 连接太多的处理方式
皮皮虾的blog · 2021-11-15 · via 博客园 - 皮皮虾的blog

睡眠连接过多,会对mysql服务器造成什么影响?

严重消耗mysql服务器资源(主要是cpu, 内存),并可能导致mysql崩溃。

造成睡眠连接过多的原因?

1. 使用了太多持久连接(个人觉得,在高并发系统中,不适合使用持久连接)

2. 程序中,没有及时关闭mysql连接

3. 数据库查询不够优化,过度耗时。

那么,如果要从根本上解决sleep连接过多,就得从以上三点反复检查,但是见效并不快。

网上有人分享,使用shell脚本配合cron,定期杀死睡眠时间太久的连接,但是这种方法非常不可取,典型的以暴制暴,很可能导致数据崩溃,而且,还需要编写相应shell, 设置cron, 实施成本较繁琐,不推荐使用。

那么更好的办法应该是让mysql自己决定这些睡眠连接的命运,实施会更简单,有效。

mysql的配置文件中,有一项:

wait_timeout, 即可设置睡眠连接超时秒数,如果某个连接超时,会被mysql自然终止,多好的办法!

如设置: 

wait_timeout=100 #即设置mysql连接睡眠时间为100秒,任何sleep连接睡眠时间若超过100秒,将会被mysql服务自然终止,要比编写shell脚本更简单。

那么,对于正在运行中的生产服务器,在不能停止服务情况下,修改此项怎么办?很简单,以root用户登录到mysql,执行:

set global wait_timeout=100

即可。

在我的生产环境中,使用这个办法,取得了相当好的效果。

当然,更根本的方法,还是从以上三点排查之:

1. 程序中,不使用持久链接,即使用mysql_connect而不是pconnect。

2.   程序执行完毕,应该显式调用mysql_close

3. 只能逐步分析系统的SQL查询,找到查询过慢的SQL,优化之

如下图所示: 

这里写图片描述 
在Navicat Premium中可以看到很多处于sleep状态的连接,那怎么让MySQL自动关闭这些处理sleep状态的连接呢?

wait_timeout默认值: 
这里写图片描述 
interactive_timeout默认值: 
这里写图片描述

编辑 /etc/my.cnf,在mysqld 下 新增 timeout参数,设置为120秒,如下:

【mysqld】
wait_timeout=120
interactive_timeout=120

注意:要同时设置interactive_timeout和wait_timeout才会生效。

最后重启一下mysql 生效 即可!