我认为这是某处代码中的问题,但是代码太简单了,我不确定它可能是什么。
我已经验证 wait_timeout 足够高,并且已经完成了这里的所有操作:http: //dev.mysql.com/doc/refman/5.1/en/gone-away.html没有任何成功。
这在一个脚本运行中执行的第二个查询中重复发生,所以我确定这是一个编码错误。
我围绕 PDO 类创建了一个非常简单的包装器,以拥有一个单例数据库句柄:
<?php
class PDOWrapper
{
protected static $instance;
protected $dbh;
function __construct()
{
if ( is_null(static::$instance) )
{
static::$instance = $this;
$this->connect_to_db();
}
}
static function instance()
{
if ( is_null(static::$instance) )
{
new static;
}
return static::$instance;
}
private function connect_to_db()
{
$db_info = array(
0 => array(
'hostname' => "Host",
'username' => "User",
'password' => "Pass",
'db' => "DB",
)
);
//Try to connect to the database
try
{
$dbh = new PDO('mysql:host=' . $db_info[0]['hostname'] . ';dbname=' . $db_info[0]['db'], $db_info[0]['username'], $db_info[0]['password'], array( PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => true ));
}
catch (PDOException $e)
{
log_message("Error connecting to DB!: " . $e->getMessage(), LOG_LEVEL_CRITICAL );
return false;
}
$this->dbh = $dbh;
}
public static function get_dbh()
{
if ( is_null(static::$instance) )
{
new static;
}
return static::$instance->dbh;
}
}
然后我像这样使用包装器:
function somefunc(){
$dbh = PDOWrapper::get_dbh();
$future_sth = $dbh->prepare("SELECT * FROM some_table");
$future_sth->execute();
$ret = $future_sth->fetchAll(PDO::FETCH_ASSOC);
print_r($ret);
$future_sth->closeCursor();
return $ret;
}
作为事件循环的一部分,我反复调用此函数。第一次调用它时,print_r 运行良好,它会打印出我希望看到的行。
但是,在函数执行一次后,我得到以下信息:
Warning: Error while sending QUERY packet. PID=92871
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2006 MySQL server has gone away'
我不知道为什么它会“消失”。我的 my.cnf 看起来不错。等待超时是巨大的,无论如何,只要我运行第二个查询,就会立即发生这种情况。有任何想法吗?
在 MySQL 错误日志中看起来没有任何明显错误:
120925 12:48:46 mysqld_safe Starting mysqld daemon with databases from /usr/local/var/mysql
120925 12:48:46 [Warning] The syntax '--log' is deprecated and will be removed in a future release. Please use '--general-log'/'--general-log-file' instead.
120925 12:48:46 [Warning] Setting lower_case_table_names=2 because file system for /usr/local/var/mysql/ is case insensitive
120925 12:48:46 InnoDB: The InnoDB memory heap is disabled
120925 12:48:46 InnoDB: Mutexes and rw_locks use GCC atomic builtins
120925 12:48:46 InnoDB: Compressed tables use zlib 1.2.5
120925 12:48:46 InnoDB: Initializing buffer pool, size = 128.0M
120925 12:48:46 InnoDB: Completed initialization of buffer pool
120925 12:48:46 InnoDB: highest supported file format is Barracuda.
120925 12:48:46 InnoDB: Waiting for the background threads to start
120925 12:48:47 InnoDB: 1.1.8 started; log sequence number 2273680401
120925 12:48:47 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
120925 12:48:47 [Note] - '0.0.0.0' resolves to '0.0.0.0';
120925 12:48:47 [Note] Server socket created on IP: '0.0.0.0'.
120925 12:48:47 [Note] Event Scheduler: Loaded 0 events
120925 12:48:47 [Note] /usr/local/Cellar/mysql/5.5.25a/bin/mysqld: ready for connections.
Version: '5.5.25a-log' socket: '/tmp/mysql.sock' port: 3306 Source distribution