PDO:MySQL 服务器已消失
我有一个脚本,每晚都要进行大量的跑腿工作。
它使用在循环中执行的 PDO 准备语句。
前几个运行良好,但后来我发现它们都失败并出现错误: “MySQL 服务器已经消失”。
我们运行 MySQL 5.0.77。
PHP 版本 5.2.12
网站的其余部分运行良好。
I have a script that does a lot of legwork nightly.
It uses a PDO prepared statement that executes in a loop.
The first few are running fine, but then I get to a point where they all fail with the error:
"MySQL server has gone away".
We run MySQL 5.0.77.
PHP Version 5.2.12
The rest of the site runs fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您向服务器发送的数据包很可能比允许的最大数据包长。
当您尝试插入超过服务器最大数据包大小的
BLOB
时,即使在本地服务器上,您也会在客户端看到以下错误消息:并且服务器日志中出现以下错误消息:(如果启用了错误日志记录)
要解决此问题,您需要确定将插入的最大
BLOB
的大小,并设置max_allowed_packet
相应地在my.ini
中,例如:Most likely you sent a packet to the server that is longer than the maximum allowed packet.
When you try to insert a
BLOB
that exceeds your server's maximum packet size, even on a local server you will see the following error message on clientside:And the following error message in the server log: (if error logging is enabled)
To fix this, you need to decide what is the size of the largest
BLOB
that you will ever insert, and setmax_allowed_packet
inmy.ini
accordingly, for example:B.5.2.9。 MySQL 手册的 MySQL 服务器已消失 部分列出了导致此错误的可能原因。
也许你正处于其中一种情况? -- 特别是考虑到您正在运行长时间操作,关于
wait_timeout
可能很有趣......The B.5.2.9. MySQL server has gone away section of the MySQL manual has a list of possible causes for this error.
Maybe you are in one of those situations ? -- Especially considering you are running a long operation, the point about
wait_timeout
might be interesting...我遇到了同样的问题,如果超时,托管服务器管理会终止连接。
由于我主要使用了查询,所以我编写了一个代码,我们可以包含下面的类并将类名替换为“ConnectionManagerPDO”,而不是使用 PDO 类。我刚刚完成了 PDO 类。
然后就可以像在 PDO 中一样开始使用上面的类了。
I had the same problem where the hosting server administration kills connection if there is a timeout.
Since I have used the query in major part I wrote a code which instead of using PDO class we can include the below class and replace the classname to "ConnectionManagerPDO". I just wrapped the PDO class.
Then use can start using the above class as you do in PDO.
尝试在您的 Pod 实例上使用
PDO::setAttribute(PDO::ATTR_EMULATE_PREPARES, true)
。不知道这会有帮助,但没有日志数据,这就是我所得到的一切。Try using
PDO::setAttribute(PDO::ATTR_EMULATE_PREPARES, true)
on your pod instance(s). Dont know that it will help but with no log data its all i got.很可能您的连接已被终止(例如,通过 wait_timeout 或另一个线程发出 KILL 命令)、服务器崩溃或您以某种方式违反了 mysql 协议。
后者很可能是 PDO 中的一个错误,如果您使用服务器端准备好的语句或多结果,则极有可能(提示:不要)
服务器崩溃需要进行调查;查看服务器日志。
如果您仍然不知道发生了什么,请使用网络数据包转储程序(例如 tcpdump)转储连接的内容。
您还可以启用通用查询日志 - 但在生产中要非常小心。
It's likely that either your connection has been killed (e.g. by wait_timeout or another thread issuing a KILL command), the server has crashed or you've violated the mysql protocol in some way.
The latter is likely to be a bug in PDO, which is extremely likely if you're using server-side prepared statements or multi-results (hint: Don't)
A server crash will need to be investigated; look at the server logs.
If you still don't know what's going on, use a network packet dumper (e.g. tcpdump) to dump out the contents of the connection.
You can also enable the general query log - but do it very carefully in production.
Nathan H,下面是用于 pdo 重新连接的 php 类 + 代码使用示例。
附上屏幕截图。
Nathan H, below is php class for pdo reconnection + code usage sample.
Screenshot is attached.
今天早上在 Laravel 中更改数据库属性后,我遇到了同样的错误。我注释掉了旧的设置并粘贴了新的设置。问题是新设置缺少
DB_CONNECTION
变量:显然您需要添加您正在使用的任何连接类型:sqlite、mysql,...
I got this same error this morning after changing my DB properties in Laravel. I'd commented out the old settings and pasted in new ones. The problem was that the new settings where missing the
DB_CONNECTION
variable:Obviously you need to add whatever connection type you are using: sqlite, mysql, ...
我遇到了完全相同的问题。
我通过对 PDO 对象进行取消设置而不是将其设置为 NULL 来解决此问题。
例如:
此外,强烈建议取消所有 PDO 对象的设置。这包括包含准备好的语句的变量。
I had the exact same problem.
I resolved this issue by doing unset on the PDO object instead of seting it to NULL.
For example:
Also, it is highly recommended to unset all your PDO objects. That includes variables that contain prepared statements.
试试这个。它可能会起作用
try this. It may work