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

推荐订阅源

B
Blog RSS Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
GRAHAM CLULEY
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cybersecurity and Infrastructure Security Agency CISA
Simon Willison's Weblog
Simon Willison's Weblog
Latest news
Latest news
C
CERT Recently Published Vulnerability Notes
T
Threatpost
V
Vulnerabilities – Threatpost
AWS News Blog
AWS News Blog
Blog — PlanetScale
Blog — PlanetScale
C
Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
U
Unit 42
The Register - Security
The Register - Security
T
The Blog of Author Tim Ferriss
Stack Overflow Blog
Stack Overflow Blog
The Hacker News
The Hacker News
AI
AI
Project Zero
Project Zero
Scott Helme
Scott Helme
S
Securelist
Vercel News
Vercel News
GbyAI
GbyAI
S
Security @ Cisco Blogs
I
InfoQ
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
Forbes - Security
Forbes - Security
Google Online Security Blog
Google Online Security Blog
W
WeLiveSecurity
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Heimdal Security Blog
G
Google Developers Blog
D
DataBreaches.Net
The Last Watchdog
The Last Watchdog
D
Docker
MyScale Blog
MyScale Blog
T
Tor Project blog
Cyberwarzone
Cyberwarzone
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
T
Tenable Blog
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 聂微东
月光博客
月光博客

博客园 - 一起走过的路

Nginx GeoIP2 模块编译安装与配置指南(附防国外访问实战) Apollo批量给新创建的用户授可编辑权限 物理机Jenkins接入K8s环境 ingress配置https报错certificate.lua:259: call(): failed to set DER private key: d2i_PrivateKey_bio() failed, context: ssl_certificate_by_lua* elk收集分析nginx日志,并绘制图形 Zabbix Scheduled reports中文乱码 kubernetes mysql-StatefulSet报错处理 Jenkins合并代码Git报错处理过程 nginx集群同步方案 zabbix密码复杂度有效期安全增强,符合三级等保要求。 archery关闭导出功能 自动同步bing壁纸 CentOS7更新OpenSSH8 elk监听Java日志发送微信报警 OPENVPN 同时连接多个VPN - 一起走过的路 nginx拒绝国外IP访问 keepalived健康检查及双主MySQL健康检查脚本 执迷不悟 splunk设置索引周期和索引大小
MySQL查询当前连接数的语句
一起走过的路 · 2025-03-28 · via 博客园 - 一起走过的路

1. 查看当前总连接数

SHOW STATUS LIKE 'Threads_connected';
  • 返回当前建立的连接总数

2. 查看最大连接数配置

SHOW VARIABLES LIKE 'max_connections';
  • 显示服务器允许的最大并发连接数

3. 查看详细的连接信息

  • 显示所有连接的详细信息(用户、来源IP、执行的SQL等)

4. 按用户分组统计连接数

SELECT user, COUNT(*) as connections 
FROM information_schema.processlist 
GROUP BY user;

5. 查看连接数使用率

SELECT 
  (SELECT VARIABLE_VALUE 
   FROM performance_schema.global_status 
   WHERE VARIABLE_NAME = 'Threads_connected') AS current_connections,
  
  (SELECT VARIABLE_VALUE 
   FROM performance_schema.global_variables 
   WHERE VARIABLE_NAME = 'max_connections') AS max_connections,
  
  ROUND((SELECT VARIABLE_VALUE 
         FROM performance_schema.global_status 
         WHERE VARIABLE_NAME = 'Threads_connected') / 
        (SELECT VARIABLE_VALUE 
         FROM performance_schema.global_variables 
         WHERE VARIABLE_NAME = 'max_connections') * 100, 2) AS connection_usage_rate;

6. 查看不同状态的连接数

SELECT command, COUNT(*) 
FROM information_schema.processlist 
GROUP BY command;

7. 查看空闲连接数(Sleep状态)

SELECT COUNT(*) 
FROM information_schema.processlist 
WHERE command = 'Sleep';

注意事项:

  1. 需要至少 PROCESS 权限才能查看所有连接
  2. 生产环境连接数接近 max_connections 时需要扩容或优化
  3. 长时间 Sleep 的连接可以考虑适当调低 wait_timeout 参数

如果需要终止连接,可以使用:

KILL [connection_id];  -- 从SHOW PROCESSLIST结果中获取ID