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

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
A
About on SuperTechFans
IT之家
IT之家
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
The GitHub Blog
The GitHub Blog
Vercel News
Vercel News
G
Google Developers Blog
J
Java Code Geeks
宝玉的分享
宝玉的分享
T
Tailwind CSS Blog
Cloudbric
Cloudbric
L
LINUX DO - 最新话题
MyScale Blog
MyScale Blog
H
Heimdal Security Blog
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
Latest news
Latest news
I
Intezer
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
月光博客
月光博客
T
Threatpost
博客园 - 【当耐特】
S
Schneier on Security
P
Privacy International News Feed
G
GRAHAM CLULEY
T
Tenable Blog
AWS News Blog
AWS News Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
博客园 - Franky
Engineering at Meta
Engineering at Meta
美团技术团队
S
Secure Thoughts
T
Troy Hunt's Blog
Microsoft Security Blog
Microsoft Security Blog
SecWiki News
SecWiki News
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Cisco Talos Blog
Cisco Talos Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Martin Fowler
Martin Fowler
Webroot Blog
Webroot Blog
Google DeepMind News
Google DeepMind News
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