您现在的位置是:首页 > PHP学习

李清波 2020-05-09 PHP学习 1025 复制当前网址

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;
}


文章来源:https://www.liqingbo.com/blog-1691.html

评论