Error while sending STMT_PREPARE packet. PID=18017

image.png

这个报错是长时间连接数据库会断线,导致这个原因有多种可能,最有可能是:

1、大批量对数据库增删改;

2、增删改是因服务器卡;

3、其他可能性,未知;

第一步:修改数据库配置文件 database.php ,设置为true,开启断线重连;

// 是否需要断线重连
'break_reconnect' => true,

第二步:修改 /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;
    }

第三步:注释 /library/think/db/connector/Mysql.php中的isBreak函数

/**
 * 是否断线
 * @access protected
 * @param  PDOException|Exception  $e 异常对象
 * @return bool
 */
protected function isBreak($e)
{
    if (!$this->config['break_reconnect']) {
        return false;
    }

    $error = $e->getMessage();

    foreach ($this->breakMatchStr as $msg) {
        if (false !== stripos($error, $msg)) {
            return true;
        }
    }
    return false;
}

温馨提示: 本文最后更新于2024-12-31 20:22:47,某些文章具有时效性,若有错误或已失效,请在下方 留言或联系 蚂蚁官方
© 版权声明
THE END
喜欢就支持一下吧
点赞13赞赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容