thinkphp5+workerman 报错问题

在thinkphp5.0.X版本早期会遇到长时间开启workerman服务会报错,在thinkphp5.0.24版本已经修复。原因是因为长时间链接数据库,导致数据库断线。

解决的方案:

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长时间链接数据库,数据库断开会重连。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值