简体   繁体   English

PHP PDO查询等待不同MySQL连接上的请求,返回“Mysql Server已经消失”

[英]PHP PDO query waiting for a request on a different MySQL connection, returns “Mysql Server Has Gone Away”

I am having a problem with a website, connecting to a MySQL database using two types of connection on different parts: some PDO, some mysql_connect(). 我有一个网站的问题,在不同的部分使用两种类型的连接连接到MySQL数据库:一些PDO,一些mysql_connect()。

The first part of the website is requesting MySQL using the very classic 'mysql_query()' PHP function. 该网站的第一部分是使用非常经典的'mysql_query()'PHP函数请求MySQL。 This part makes some heavy queries on geographical data. 这部分对地理数据进行了一些重要的查询。 Some of these requests (already optimized) take a long time. 其中一些请求(已经过优化)需要很长时间。

Another part of the site is more recent, and made using Doctrine via a PDO connection. 该站点的另一部分是最新的,并通过PDO连接使用Doctrine。

The problem is, when one of the big processes is being ran in one browser page (can take around 1minute to process and return the page), if a user opens another page the PDO connection is in sleep mode, and holds the whole page from loading. 问题是,当一个大进程在一个浏览器页面中运行时(可能需要大约1分钟来处理并返回页面),如果用户打开另一个页面,PDO连接处于睡眠模式,并保留整个页面加载。 After 60s (wait_timeout of mysql) the connection is killed, and the PDO gets an exception "The MySQL Server has gone away". 60秒后(mysql的wait_timeout)连接被终止,PDO得到一个异常“MySQL服务器已经消失”。

What is strange is that other pages with only classical mysql_connect() and mysql_query() can be run without a problem in parallel, only PDO queries are holding back and eventually dying. 奇怪的是,只有经典的mysql_connect()和mysql_query()的其他页面可以并行运行而没有问题,只有PDO查询会阻止并最终死亡。

Any input would be really appreciated. 任何意见都会非常感激。

Closing this question, it was in fact related to the php session being held up on write, preventing the other process from running. 关闭这个问题,它实际上与写入时保持的php会话有关,阻止其他进程运行。 session_write_close() resolved it. session_write_close()解决了它。

There are various reasons that a connection gets closed. 连接关闭的原因有很多。

Reference: https://dev.mysql.com/doc/refman/5.0/en/gone-away.html 参考: https//dev.mysql.com/doc/refman/5.0/en/gone-away.html

I too faced the similar problem on using PDO where the hosting administrator kills the connection if it sleeps more than a minute. 我也遇到了使用PDO的类似问题,如果主机管理员睡眠超过一分钟,主机管理员就会终止连接。 Hence I came up with my own class which will wrap the PDO class. 因此,我想出了自己的类,它将包装PDO类。 This will detect whether a connection is closed and will try to reconnect on query execution. 这将检测连接是否已关闭,并将尝试重新连接查询执行。

Answer Below 回答下面

PDO: MySQL server has gone away PDO:MySQL服务器已经消失

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

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