yii2 console MySQL server has gone away

一.问题:
现在有1个使用yii2的console执行的php脚本,该脚本一直在后台执行.
追踪输出日志发现报:

2006 MySQL server has gone away

二.主要代码如下:

try{
Yii::$app->db->open();
    if (Yii::$app->db->getIsActive()) {
       // 做insert操作
    }
}catach(\yii\db\Exception $e){
   var_dump($e);
  // 重新连接
 Yii::$app->db->close();
 Yii::$app->db->open();
}

三.场景:

这个php脚本一直在后台执行,当有数据过来时,执行insert操作,无数据过来不执行insert操作。可能昨天18:00来条数据,然后insert成功,然后到第二天上午10:00才来另一条数据;也可能间隔会很小;

四.分析:
我的insert操作只是插入一个int型字段和一个时间戳,所以问题不会处在这个insert执行时间过长上。
由于间隔时间有可能太长,超过了mysql的wait_timeout,导致出现这个:

MySQL server has gone away
 

五.yii2怎么解决?
1.本人没有修改mysql.ini的timeout参数权限,所以这种解决办法不可行
2.解决思路也比较简单:在每次Insert之前判断db connection是否active,否则重新连接,那我的代码为什么不能解决这个问题?该怎样写?注意用yii2类解决

  1. github上有相关讨论,但是没有说一个标准的解决办法.

阅读 8.5k
2 个回答

经过我的不屑努力和研究终于得到解决方案

<?php
namespace common\lib;
class Command extends \yii\db\Command{
    public function execute()
    {
        try {
            return parent::execute();
        } catch (\yii\db\Exception $e) {
            if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) {
                $this->db->close();
                $this->db->open();
                $this->pdoStatement = null ;
                return parent::execute();
            }else{
                throw $e;
            }
        }
    }

    protected function queryInternal($method, $fetchMode = null){
        try {
            return parent::queryInternal($method, $fetchMode);
        } catch (\yii\db\Exception $e) {
            if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) {
                $this->db->close();
                $this->db->open();
                $this->pdoStatement = null ;

                return parent::queryInternal($method, $fetchMode);
            }else{
                throw $e;
            }
        }
    }


}
'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host='.$db_config['host'].';dbname='.$db_config['dbname'],
            'username' => $db_config['username'],
            'password' => $db_config['password'],
            'charset' => 'utf8',
            'commandClass'=>"\\common\\lib\\Command", //<==注意这里要替换
        ],

Google查找mysql has gone away关键字传送到这里来的,看来我们是有缘人,这么久都没人回答,我来试着解答一下。

首先,我们来看一下 getIsActive方法的实现:

   /**
     * Returns a value indicating whether the DB connection is established.
     * @return boolean whether the DB connection is established
     */
    public function getIsActive()
    {
        return $this->pdo !== null;
    }

WTF!竟然是粗暴、简单地判断pdo属性是否为null,这样肯定没办法判断远端mysql是否已断开连接了。

你可以在每次mysql操作之后,调用\Yii::$app->db->close()来关闭数据库连接,即可解决。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题