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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:35:16  来源:igfitidea点击:

How can I see how many MySQL connections are open?

phpmysqlapache

提问by Sahal

How can I see how many connections have been opened during the current request via mysql_connectin PHP running on Apache?

如何通过mysql_connect在 Apache 上运行的 PHP 查看当前请求期间打开了多少个连接?

I know that if I call mysql_connectfunction 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_connectis not starting a new one.

但我只是想确保mysql_connect没有开始一个新的。

I am working with a legacy system which contains many mysql_connectfunction 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 PROCESSLISTand find out unique values in the Idcolumn. In old PHP API mysql, there is mysql_list_processesfunction 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 Connectionsmight 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 processlistis analog of show full processlist.

FYImysqladmin -v -uroot -ppass processlistshow 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服务器的连接。