我用 PHP 编写了一些代码,可以从 .edu 域返回 html 内容。这里简单介绍一下:Errors regarding Web Crawler in PHP
当要抓取的链接数量很少(大约 40 个 URL)时,爬虫工作正常,但在这个数字后我收到“MySQL 服务器已消失”错误。
我将 html 内容作为长文本存储在 MySQL 表中,但我不明白为什么在至少 40-50 次插入后出现错误。
非常感谢在这方面的任何帮助。
请注意,我已经更改了 wait_timeout 和 max_allowed_packet 以适应我的查询和 php 代码,现在我不知道该怎么做。请在这方面帮助我。
最佳答案
您可能倾向于在查询之前通过“ping”mysql 服务器来处理这个问题。这是一个坏主意。有关原因的更多信息,请查看此 SO 帖子:Should I ping mysql server before each query?
处理该问题的最佳方法是将查询包装在 try/catch
block 中并捕获任何数据库异常,以便您可以适本地处理它们。这在长时间运行和/或守护程序类型的脚本中尤其重要。因此,这是一个非常基本的示例,使用“连接管理器”来控制对数据库连接的访问:
class DbPool {
private $connections = array();
function addConnection($id, $dsn) {
$this->connections[$id] = array(
'dsn' => $dsn,
'conn' => null
);
}
function getConnection($id) {
if (!isset($this->connections[$id])) {
throw new Exception('Invalid DB connection requested');
} elseif (isset($this->connections[$id]['conn'])) {
return $this->connections[$id]['conn'];
} else {
try {
// for mysql you need to supply user/pass as well
$conn = new PDO($dsn);
// Tell PDO to throw an exception on error
// (like "MySQL server has gone away")
$conn->setAttribute(
PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION
);
$this->connections[$id]['conn'] = $conn;
return $conn;
} catch (PDOException $e) {
return false;
}
}
}
function close($id) {
if (!isset($this->connections[$id])) {
throw new Exception('Invalid DB connection requested');
}
$this->connections[$id]['conn'] = null;
}
}
class Crawler {
private $dbPool;
function __construct(DbPool $dbPool) {
$this->dbPool = $dbPool;
}
function crawl() {
// craw and store data in $crawledData variable
$this->save($crawledData);
}
function saveData($crawledData) {
if (!$conn = $this->dbPool->getConnection('write_conn') {
// doh! couldn't retrieve DB connection ... handle it
} else {
try {
// perform query on the $conn database connection
} catch (Exception $e) {
$msg = $e->getMessage();
if (strstr($msg, 'MySQL server has gone away') {
$this->dbPool->close('write_conn');
$this->saveData($val);
} else {
// some other error occurred
}
}
}
}
}
关于mysql - 解决 "MySQL server has gone away"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8689649/