我有一个脚本,每晚做大量的跑腿工作。
它使用在循环中执行的PDO准备好的语句。
前几个都运行得很好,但是后来我发现它们都失败了,并显示错误:"MySQL服务器已经消失了“。
我们运行MySQL 5.0.77。
PHP版本5.2.12
站点的其余部分运行得很好。
发布于 2010-02-10 04:07:37
MySQL手册的B.5.2.9. MySQL server has gone away部分列出了可能导致此错误的原因。
也许你正处于这种情况下?--尤其是考虑到你正在运行一个很长的操作,关于wait_timeout的观点可能会很有趣……
发布于 2011-01-04 03:23:32
很可能是您向服务器发送的数据包长度超过了允许的最大数据包长度。
当您尝试插入超过服务器最大数据包大小的BLOB时,即使是在本地服务器上,您也会在客户端看到以下错误消息:
MySQL服务器已消失
并在服务器日志中显示以下错误消息:(如果启用了错误日志记录)
错误1153获得了一个大于'max_allowed_packet‘字节的数据包
要解决此问题,您需要确定要插入的最大BLOB的大小,并相应地在my.ini中设置max_allowed_packet,例如:
[mysqld]
...
max_allowed_packet = 200M
...发布于 2015-07-03 16:55:41
我也遇到过同样的问题,如果出现超时,托管服务器管理就会终止连接。
因为我在主要部分使用了查询,所以我写了一个代码,我们可以包含下面的类并将类名替换为"ConnectionManagerPDO“,而不是使用PDO类。我刚刚打包了PDO类。
final class ConnectionManagerPDO
{
private $dsn;
private $username;
private $passwd;
private $options;
private $db;
private $shouldReconnect;
const RETRY_ATTEMPTS = 3;
public function __construct($dsn, $username, $passwd, $options = array())
{
$this->dsn = $dsn;
$this->username = $username;
$this->passwd = $passwd;
$this->options = $options;
$this->shouldReconnect = true;
try {
$this->connect();
} catch (PDOException $e) {
throw $e;
}
}
/**
* @param $method
* @param $args
* @return mixed
* @throws Exception
* @throws PDOException
*/
public function __call($method, $args)
{
$has_gone_away = false;
$retry_attempt = 0;
try_again:
try {
if (is_callable(array($this->db, $method))) {
return call_user_func_array(array($this->db, $method), $args);
} else {
trigger_error("Call to undefined method '{$method}'");
/*
* or
*
* throw new Exception("Call to undefined method.");
*
*/
}
} catch (\PDOException $e) {
$exception_message = $e->getMessage();
if (
($this->shouldReconnect)
&& strpos($exception_message, 'server has gone away') !== false
&& $retry_attempt <= self::RETRY_ATTEMPTS
) {
$has_gone_away = true;
} else {
/*
* What are you going to do with it... Throw it back.. FIRE IN THE HOLE
*/
throw $e;
}
}
if ($has_gone_away) {
$retry_attempt++;
$this->reconnect();
goto try_again;
}
}
/**
* Connects to DB
*/
private function connect()
{
$this->db = new PDO($this->dsn, $this->username, $this->passwd, $this->options);
/*
* I am manually setting to catch error as exception so that the connection lost can be handled.
*/
$this->db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
/**
* Reconnects to DB
*/
private function reconnect()
{
$this->db = null;
$this->connect();
}
}然后,用户可以像在PDO中一样开始使用上面的类。
try {
$db = new ConnectionManagerPDO("mysql:host=localhost;dbname=dummy_test", "root", "");
$query = $db->query("select * from test");
$query->setFetchMode(PDO::FETCH_ASSOC);
}
catch(PDOException $e){
/*
handle the exception throw in ConnectionManagerPDO
*/
}https://stackoverflow.com/questions/2232150
复制相似问题