简体   繁体   English

在 PHP 中获取“MySQL 服务器已经消失”,即使我正在关闭我的数据库连接

[英]Getting “MySQL Server has gone away” in PHP, even if I'm closing my database connection

I have a PHP Socket Server that I can connect to via Telnet.我有一个可以通过 Telnet 连接的 PHP 套接字服务器。 Once connected, I am able to send messages in a certain format and they're saved in the database.连接后,我能够以某种格式发送消息并将它们保存在数据库中。

What happens is that when the PHP Socket receives a message, it opens a Database connection, executes the query, then closes the connection.发生的情况是,当 PHP Socket 收到消息时,它打开一个数据库连接,执行查询,然后关闭连接。 When it receives another message, it opens the connection again, executes the query, then closes the connection.当它收到另一条消息时,它再次打开连接,执行查询,然后关闭连接。

So far, this works when I'm sending messages in an interval of 5-10 minutes.到目前为止,当我以 5-10 分钟的时间间隔发送消息时,这是有效的。 However, when the interval increases for over an hour or so, I get a MySQL Server has gone away error.然而,当间隔增加一个多小时左右时,我得到一个MySQL Server has gone away错误。

Upon doing some research, the common solution seems to be increasing the wait time, which is not an option for me.经过一些研究,常见的解决方案似乎是增加等待时间,这对我来说不是一个选择。 The PHP Socket server is supposed to be open 24/7, and I doubt there's a way to increase the wait time to infinity. PHP Socket 服务器应该是 24/7 全天候开放的,我怀疑是否有办法将等待时间增加到无穷大。

The other option is to check in PHP itself if the MySQL Server has gone away or not, and depending on the result, reset the MySQL Server, and try to establish the connection again.另一种选择是检查 PHP 本身是否 MySQL 服务器已经消失,并根据结果重置 MySQL 服务器,并尝试再次建立连接。

Does anyone know how to do this in PHP?有谁知道如何在 PHP 中做到这一点? Or if anyone has other methods in keeping a MySQL Server constantly alive in a PHP Socket server, I would also be open to that idea.或者,如果有人有其他方法可以使 MySQL 服务器在 PHP Socket 服务器中始终保持活动状态,我也愿意接受这个想法。

You'll get this error if the database connection has timed out (perhaps from a long-running process since the last time the connection was used).如果数据库连接超时(可能是自上次使用连接以来的长时间运行过程),您将收到此错误。

You can easily check the connection and restore it if necessary with a single command:您可以使用单个命令轻松检查连接并在必要时恢复它:

$mysqli->ping()

This error means that the connection has been established to the DBMS but has subsequently been lost.此错误意味着与 DBMS 的连接已建立,但随后丢失。 I've run sites with hundreds of concurrent connections to the same database instance handling thousands of queries per minute and have only seen this error when a query has been deliberately killed by an admin.我运行的站点有数百个并发连接到同一个数据库实例,每分钟处理数千个查询,并且只有在管理员故意终止查询时才看到此错误。 You should check your config for the interactive timeout and read your server logs.您应该检查交互式超时的配置并阅读服务器日志。

Given your description of the error (you really need to gather more data and characterized the circumstances of the error) the only explanation which springs to mind is that there is a deadlock somewhere.鉴于您对错误的描述(您确实需要收集更多数据并描述错误的情况),唯一能想到的解释是某处存在死锁。

I would question whether there is any benefit to closing the connection after each message (depending on the usage of the system).我会质疑在每条消息之后关闭连接是否有任何好处(取决于系统的使用情况)。 Artistic phoenix's comment is somewhat confused.文艺凤凰的评论有些糊涂。 But if there are capacity issues then I'd suggest using persistent connections with your existing open/close model, but I doubt that is relevant to the problem you describe here.但是,如果存在容量问题,那么我建议对您现有的打开/关闭模型使用持久连接,但我怀疑这与您在此处描述的问题有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM