yii2 mysql gone away,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"}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值