mysql连接数 2023-02-17

2023-02-16  本文已影响0人  9_SooHyun

在使用 MySQL 的时候,可能会遇到一个问题,“Can not connect to MySQL server. Too many connections” -mysql 1040 错误,这是因为MySQL还未释放的连接数已经达到设置的上限,无法建立新连接
MySQL无论如何都会保留一个用于管理员(SUPER)登陆的连接,用于管理员连接数据库进行维护操作,即使当前连接数已经达到了max_connections。因此MySQL的实际最大可连接数为max_connections+1

查看最大连接数相关参数

show variables like '%max_connections%'; --mysql设置的最大连接数
show global status like 'Max_used_connections'; --The maximum number of connections that have been in use simultaneously since the server started.
show global status like 'Max_used_connections_time';  --The time at which Max_used_connections reached its current value.

查看当前连接的状态

mysql> show global status like 'Threads%';
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Threads_cached | 49 | The number of threads in the thread cache.
| Threads_connected | 2 | The number of currently open connections.
| Threads_created | 201 | The number of threads created to handle connections. If Threads_created is big, you may want to increase the thread_cache_size value. The cache miss rate can be calculated as Threads_created/Connections.
| Threads_running | 2 | The number of threads that are not sleeping.

Threads_cached ## mysql管理的线程池中还有多少可以被复用的资源
Threads_connected ## 打开的连接数
Threads_created ## 表示创建过的线程数,如果发现Threads_created值过大的话,表明MySQL服务器一直在创建线程,这也是比较耗资源,可以适当增加配置文件中thread_cache_size值,查询服务器

上一篇 下一篇

猜你喜欢

热点阅读