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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
Lohrmann on Cybersecurity
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
G
GRAHAM CLULEY
The GitHub Blog
The GitHub Blog
NISL@THU
NISL@THU
AWS News Blog
AWS News Blog
博客园 - 【当耐特】
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
阮一峰的网络日志
阮一峰的网络日志
Cyberwarzone
Cyberwarzone
V
V2EX
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
月光博客
月光博客
MongoDB | Blog
MongoDB | Blog
Scott Helme
Scott Helme
A
Arctic Wolf
美团技术团队
S
Schneier on Security
P
Proofpoint News Feed
G
Google Developers Blog
The Hacker News
The Hacker News
S
Securelist
Microsoft Security Blog
Microsoft Security Blog
Project Zero
Project Zero
T
The Exploit Database - CXSecurity.com
量子位
T
Threatpost
Spread Privacy
Spread Privacy
Help Net Security
Help Net Security
B
Blog
WordPress大学
WordPress大学
B
Blog RSS Feed
J
Java Code Geeks
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
Cloudbric
Cloudbric

博客园 - 一起走过的路

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