In PHP, are there multiple connections to the database server when web servers serve multiple requests concurrently at the same time?

huangapple go评论54阅读模式
英文:

In PHP, are there multiple connections to the database server when web servers serve multiple requests concurrently at the same time?

问题

For example, assume I have a script that read/update/delete directly (without caching) data from the database (MySQL)
如果我有一个脚本,直接(没有缓存)从数据库(MySQL)读取/更新/删除数据,
If there are n concurrent requests that execute this script at that time, there are also n connections to the database at that time, am I right?
如果在那个时候有 n 个并发请求执行这个脚本,那么在那个时候也会有 n 个与数据库的连接,我说得对吗?

英文:

For example, assume I have a script that read/update/delete directly (without caching) data from the database (MySQL)
If there are n concurrent requests that execute this script at that time, there are also n connections to the database at that time, am I right?

Thanks in advance

答案1

得分: 3

在PHP中,数据库连接是资源。资源不在请求之间共享。例如,您不能在一个PHP请求中打开文件句柄,然后在不同的PHP请求中使用该文件句柄。资源会在请求结束时自动关闭。

需要读取或写入数据库的每个请求都会打开自己的数据库连接。如果多个请求同时运行,那么数据库可能会有多个连接。

也有例外情况:PHP脚本不一定会在启动时立即打开数据库连接,或者它可能会在脚本完成之前关闭数据库连接。另一个PHP脚本可能根本不需要访问数据库,因此它会跳过打开数据库连接。因此,您可能会有并发的PHP请求执行,但并非所有请求都持有一个打开的数据库连接。这取决于您的代码,这也是上面某些评论含糊不清的原因。

尽管如此,在典型的PHP应用程序中,通常的编码实践是在脚本启动后不久打开数据库连接,并在请求的整个过程中保持连接打开,依赖于PHP垃圾收集来在脚本退出时关闭数据库连接。

因此,我会说在任何典型的PHP应用程序中,多个并发请求会持有相同数量的并发数据库连接。

您可以使用MySQL查询工具来进行检查:

  • SHOW GLOBAL STATUS LIKE 'Threads_connected'; 显示在运行此命令时的当前打开客户端连接数。
  • SHOW GLOBAL STATUS LIKE 'Max_used_connections'; 显示自上次MySQL服务器重新启动以来的连接数峰值。
  • SHOW PROCESSLIST; 显示有关当前连接的详细信息,如它们正在运行的SQL语句(或者如果它们在SQL语句之间空闲则显示“Sleep”)。
英文:

In PHP, database connections are resources. Resources are not shared between requests. For example, you can't open a file handle in one PHP request and use that file handle in a different PHP request. Resources are closed automatically at the end of the request.

Each request that needs to read or write to the database opens its own database connection. If multiple requests are running concurrently, then the database will likely have multiple connections.

There are exceptions: a PHP script doesn't necessarily open a database connection immediately as it starts, or it could close the database connection before the script finishes. Or another PHP script may not need to access the database at all, so it skips opening a database connection. You might therefore have concurrent PHP requests executing, but not all of them holding an open database connection. This depends on your code, which is the reason some of the comments above are equivocating.

That said, it is common PHP coding practice to open a database connection soon after the script starts, and to leave it open for the duration of the request, relying on PHP garbage collection to close the database connection as the script exits.

So I would say in any typical PHP application, multiple concurrent requests hold an equivalent number of concurrent database connections.

You can check by using a MySQL query tool:

  • SHOW GLOBAL STATUS LIKE 'Threads_connected'; shows the current number of open client connections at the moment you run this command.

  • SHOW GLOBAL STATUS LIKE 'Max_used_connections'; shows the high-water mark of connections in the past (since the last restart of MySQL Server).

  • SHOW PROCESSLIST; shows details about the current connections like what SQL statement they are running (or "Sleep" if they are idle between SQL statements).

huangapple
  • 本文由 发表于 2023年2月6日 21:56:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75362249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定