zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

MySQL 查看当前最大连接数,并发数

mysql并发 查看 最大 当前 连接数
2023-09-14 09:16:40 时间

显示MySQL最大连接数

mysql> show variables like '%max_connections%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 151   |
+-----------------+-------+
1 row in set (0.00 sec)

服务器响应的MySQL最大连接数

mysql> show status like '%connections%';
+-----------------------------------+-------+
| Variable_name                     | Value |
+-----------------------------------+-------+
| Connection_errors_max_connections | 0     |
| Connections                       | 7309  |    #已正常连接正常退出的链接数
| Max_used_connections              | 26    |    #服务器响应的最大链接数
+-----------------------------------+-------+
3 rows in set (0.00 sec)

比较理想的设置是: Max_used_connections / max_connections * 100% ≈ 85%   

最大连接数占上限连接数的85%左右,如果发现比例在10%以下,MySQL服务器连接上线就设置得过高了。

 

查看MySQL线程

mysql> show status like '%Threads%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| Delayed_insert_threads | 0     |
| Slow_launch_threads    | 0     |
| Threads_cached         | 4     |         #缓存中的线程连接数
| Threads_connected      | 14    |         #当前打开的连接数,与processlist一样
| Threads_created        | 18    |         #为处理连接而创建的线程数
| Threads_running        | 3     |         #非睡眠状态的连接数,通常指并发连接数
+------------------------+-------+
6 rows in set (0.00 sec)

缓存未命中率 = Theads_created / Connections

如果Threads_created值很大,要增加 thread_cache_size

thread_cache_size:但客户端断开时,如果缓存中的线程连接数即Threads_cached,比thread_cache_size小,那么这个客户端线程会被放入缓存中,如果可能,通过重用缓存中线程来满足线程请求。如果有许多新连接,可以增加该项值来提高性能。