关于ThinkPHP5.0.*版本 cli模式下php每隔段时间就出错
cli模式下php每隔段时间就出错
官方论坛
日志如下:
# 控制台
Uncaught think\exception\ErrorException: Error while sending STMT_CLOSE packet. PID=23951 in /www/web/work/public_html/thinkphp/library/think/db/Connection.php:318
# log文件
think\db\Connection::free(): send of 9 bytes failed with errno=32 Broken pipe
分析原因
# 长时间数据库会断线 但是新版本会改进断线重连机制
解决方案
# 1.临时解决
# 修改/thinkphp/library/think/db/Connection.php
# 1.1是否需要断线重连
'break_reconnect' => true,
# 1.2 释放查询结果 捕获异常
public function free()
{
try {
$this->PDOStatement = null;
} catch (Exception $e) {
Log::write("has error when free PDOStatement maybe mysql gone away,skip it:" . $e->getMessage(), log::DEBUG);
}
}
# 1.3 是否断线,修改为master最新
protected function isBreak($e)
{
if (!$this->config['break_reconnect']) {
return false;
}
$info = [
'server has gone away',
'no connection to the server',
'Lost connection',
'is dead or not enabled',
'Error while sending',
'decryption failed or bad record mac',
'server closed the connection unexpectedly',
'SSL connection has been closed unexpectedly',
'Error writing data to the connection',
'Resource deadlock avoided',
'failed with errno',
'send of 33 bytes failed with errno=32 Broken pipe',
];
$error = $e->getMessage();
foreach ($info as $msg) {
if (false !== stripos($error, $msg)) {
return true;
}
}
return false;
}
# 2.官方推荐使用最新版本以解决此问题
本文分析了ThinkPHP5.0.*版本在CLI模式下PHP出现的错误,特别是在长时间运行后数据库连接断开的问题。提供了临时解决方案,包括修改Connection.php文件中的断线重连机制和异常捕获,同时建议升级到最新版本以获得官方推荐的修复。
2428

被折叠的 条评论
为什么被折叠?



