首页> 实战笔录 >PHP开发笔记 >ThinkPHP ThinkPHP
PHP/TP5使用supervisor/workerman在CLI模式下mysql断线重连解决办法
作者:小萝卜 2020-06-04 【 TP5 】 浏览 4989
简介supervisor/workerman不停运行的循环体,如何重连是个伤心的事,好在TP可以主动重连,可以写一个小插件,几行代码,解决问题
PHP/TP5使用supervisor/workerman在CLI模式下mysql断线重连解决办法
在thinkphp5.0.X版本早期会遇到长时间开启workerman服务会报错,在thinkphp5.0.24版本已经修复。原因是因为长时间链接数据库,导致数据库断线。
如果tp版本在5.0.24之后,出现了断线问题,只需要开启databases文件里面的断线重连操作:
// 是否需要断线重连
'break_reconnect' => true,
在thinkphp5.0.X-5.024版本,需要做如下操作:
1、修改数据库配置database.php文件,将break_reconnect参数设置为true。断线重连。
// 是否需要断线重连
'break_reconnect' => true,
2、修改 /library/think/db/Connection.php中的isBreak函数,替换为以下最新的isBreak函数。
/**
* 是否断线
* @access protected
* @param \PDOException|\Exception $e 异常对象
* @return bool
*/
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',
];
$error = $e->getMessage();
foreach ($info as $msg) {
if (false !== stripos($error, $msg)) {
return true;
}
}
return false;
}
3、将/library/think/db/connector/Mysql.php中的isBreak函数删除或者注释掉。
修改完后,workerman长时间链接数据库,数据库断开会重连。
以上就是workerman+tp5的错误问题怎么解决的详细内容,更多请关注随便技术网其它相关文章!
很赞哦! (0)
相关文章
- TP6实现前后端分离的图片验证码,验证码怎么以接口形式返回
- TP5 关联模型使用(嵌套关联、动态排序以及隐藏字段)
- TP框架右下角运行时间
- tp5 致命错误: Class 'think\route' not found
- TP5数据库操作时怎么过略不存在的字段
- TP6使用JWT实现中间件验证Token,验证用户登录信息
- tp6定义模板常量__PUBLIC__,__STATIC__
- tp5判断一张数据表是否存在
- TP6上传图片验证时报错think\\Validate::check() must be of the type array
- PHP/ThinkPHP实现用户一段时间没有操作,系统自动退出