yii2 console MySQL server

yii2 console MySQL server has gone away

经过我的不屑努力和研究终于得到解决方案
修改配置文件common/config/main.php

components中添加'commandClass'=>"\common\lib\DbCommand",添加检测连接配置

return [
    'components' => [
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=127.0.0.1;dbname=test',
            'username' => 'root',
            'password' => 'root',
            'charset' => 'utf8',
            'tablePrefix' => '',
            'commandClass'=>"\common\lib\DbCommand",#添加检测连接配置
        ],
    ]

创建common\lib\DbCommand.php

<?php
/**
 * Name: Db连接检测
 * Author: JiaMeng <666@majiameng.com>
 * Date: 2018/7/6 11:59
 * Description: IndexController.php.
 */
namespace common\lib;
class DbCommand 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) {//2006 mysql has gone away
                $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;
            }
        }
    }
}

测试

<?php
namespace console\controllers;
use yii\console\Controller;
use Yii;

class IndexController extends Controller
{
    public function actionTest(){
        ini_set('default_socket_timeout', -1);

        for ($i=0;$i<=100;$i++){
            echo "\r\n";
            sleep(1);
            echo date('H;i:s').":".$i;
            try{
                $a = \yii::$app->tinymeng->createCommand('SELECT id,username FROM user WHERE id=1');
                $result = $a->queryOne();
                echo json_encode($result);
            }catch (\Exception $exception){
                echo "close !";
            }
        }

    }
}

执行中重启服务器数据库

17;36:07:0{"id":"1","username":"admin"}
17;36:08:1{"id":"1","username":"admin"}
17;36:09:2{"id":"1","username":"admin"}
17;36:10:3{"id":"1","username":"admin"}
17;36:11:4{"id":"1","username":"admin"}
17;36:12:5{"id":"1","username":"admin"}
17;36:13:6{"id":"1","username":"admin"}
17;36:14:7{"id":"1","username":"admin"}
17;36:15:8{"id":"1","username":"admin"}
17;36:17:9{"id":"1","username":"admin"}
17;36:18:10{"id":"1","username":"admin"}
17;36:19:11{"id":"1","username":"admin"}
17;36:20:12{"id":"1","username":"admin"}
17;36:21:13{"id":"1","username":"admin"}
17;36:22:14{"id":"1","username":"admin"}
17;36:23:15close !
17;36:24:16close !
17;36:25:17close !
17;36:27:18{"id":"1","username":"admin"}
17;36:29:19{"id":"1","username":"admin"}
17;36:30:20{"id":"1","username":"admin"}
17;36:31:21{"id":"1","username":"admin"}
Last modification:September 23rd, 2019 at 05:44 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment