php 如何查看打开的 MySQL 连接数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6502036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I see how many MySQL connections are open?
提问by Sahal
How can I see how many connections have been opened during the current request via mysql_connect
in PHP running on Apache?
如何通过mysql_connect
在 Apache 上运行的 PHP 查看当前请求期间打开了多少个连接?
I know that if I call mysql_connect
function 100 times with the same parameters, it will always return the same connection link. It will not start new connection once the connection already exists.
我知道如果我mysql_connect
用相同的参数调用函数 100 次,它总是会返回相同的连接链接。一旦连接已经存在,它将不会启动新连接。
But I just want to make sure mysql_connect
is not starting a new one.
但我只是想确保mysql_connect
没有开始一个新的。
I am working with a legacy system which contains many mysql_connect
function calls.
我正在使用包含许多mysql_connect
函数调用的旧系统。
Is there any setting in Apache or is there any way I can log this number of connections in Apache or MySQL log file?
Apache 中是否有任何设置,或者有什么方法可以在 Apache 或 MySQL 日志文件中记录此连接数?
回答by Abhay
I think there are a couple of ways:
我认为有以下几种方法:
SHOW STATUS WHERE `variable_name` = 'Threads_connected'
or you can do a SHOW PROCESSLIST
and find out unique values in the Id
column. In old PHP API mysql
, there is mysql_list_processes
function that does the same as SHOW PROCESSLIST
, too.
或者您可以执行 aSHOW PROCESSLIST
并找出Id
列中的唯一值。在旧的 PHP API 中mysql
,也有mysql_list_processes
与 相同的功能SHOW PROCESSLIST
。
But first one should work for you. And perhaps you might like to check on other STATUS variables
但第一个应该适合你。也许您可能想检查其他STATUS 变量
回答by Kamil Dziedzic
There are other useful variables regarding connections and in your particular case variable Connections
might help find out if your code is making too many connections. Just check it value before and after running code.
还有其他关于连接的有用变量,在您的特定情况下,变量Connections
可能有助于确定您的代码是否建立了过多的连接。只需在运行代码之前和之后检查它的值。
# mysql -e 'SHOW STATUS WHERE variable_name LIKE "Threads_%" OR variable_name = "Connections"'
+-------------------+-------+
| Variable_name | Value |
+-------------------+-------+
| Connections | 22742 |
| Threads_cached | 1 |
| Threads_connected | 87 |
| Threads_created | 549 |
| Threads_running | 51 |
+-------------------+-------+
Connections
The number of connection attempts (successful or not) to the MySQL server.
Threads_cached
The number of threads in the thread cache.
Threads_connected
The number of currently open connections.
Threads_created
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
The number of threads that are not sleeping.
连接
连接到 MySQL 服务器的尝试次数(成功与否)。
线程缓存
线程缓存中的线程数。
Threads_connected
当前打开的连接数。
Threads_created
为处理连接而创建的线程数。如果 Threads_created 很大,您可能需要增加 thread_cache_size 值。缓存未命中率可以计算为 Threads_created/Connections。
线程_运行
未休眠的线程数。
回答by Alexander Yancharuk
Current connections status:
当前连接状态:
mysqladmin status
Look at Threads:
count. More detailed information about current connections can be obtained with the commands:
看Threads:
数。可以使用以下命令获取有关当前连接的更多详细信息:
user@host:~$ mysqladmin -uroot -ppass extended-status | grep Threads
| Threads_cached | 0 |
| Threads_connected | 3 |
| Threads_created | 3 |
| Threads_running | 1 |
user@host:~$ mysqladmin -uroot -ppass processlist
+----+------+-----------+----+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+-------+------------------+
| 53 | root | localhost | | Sleep | 258 | | |
| 54 | root | localhost | | Sleep | 253 | | |
| 58 | root | localhost | | Query | 0 | | show processlist |
+----+------+-----------+----+---------+------+-------+------------------+
FYImysqladmin -v -uroot -ppass processlist
is analog of show full processlist
.
FYImysqladmin -v -uroot -ppass processlist
是show full processlist
.
Commands can be shortened to any unique prefix, and called simultaneously:
命令可以缩短为任何唯一的前缀,并同时调用:
user@host:~$ mysqladmin -v -uroot -ppass proc stat
+----+------+-----------+----+---------+------+-------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+----+---------+------+-------+-----------------------+
| 53 | root | localhost | | Sleep | 951 | | |
| 54 | root | localhost | | Sleep | 946 | | |
| 65 | root | localhost | | Query | 0 | | show full processlist |
+----+------+-----------+----+---------+------+-------+-----------------------+
Uptime: 1675 Threads: 3 Questions: 171 Slow queries: 0 Opens: 235
Flush tables: 1 Open tables: 57 Queries per second avg: 0.102
回答by gmhk
You could use the MySQL command show processlistto get the number of connections.
您可以使用 MySQL 命令show processlist来获取连接数。
回答by X10nD
I dont think you can see the number of connection, but can limit the connections to the mysql server.
我不认为您可以看到连接数,但可以限制与mysql服务器的连接。