您现在的位置是:首页 > 博客日记 > Php Php

yii2 console MySQL server

2018-09-18 15:26:00 【Php】 人已围观

yii2 console MySQL server has gone away

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

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

  1. return [
  2. 'components' => [
  3. 'db' => [
  4. 'class' => 'yii\db\Connection',
  5. 'dsn' => 'mysql:host=127.0.0.1;dbname=test',
  6. 'username' => 'root',
  7. 'password' => 'root',
  8. 'charset' => 'utf8',
  9. 'tablePrefix' => '',
  10. 'commandClass'=>"\common\lib\DbCommand",#添加检测连接配置
  11. ],
  12. ]

创建common\lib\DbCommand.php

  1. <?php
  2. /**
  3. * Name: Db连接检测
  4. * Author: JiaMeng <666@majiameng.com>
  5. * Date: 2018/7/6 11:59
  6. * Description: IndexController.php.
  7. */
  8. namespace common\lib;
  9. class DbCommand extends \yii\db\Command{
  10. public function execute()
  11. {
  12. try {
  13. return parent::execute();
  14. } catch (\yii\db\Exception $e) {
  15. if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) {//2006 mysql has gone away
  16. $this->db->close();
  17. $this->db->open();
  18. $this->pdoStatement = null ;
  19. return parent::execute();
  20. }else{
  21. throw $e;
  22. }
  23. }
  24. }
  25. protected function queryInternal($method, $fetchMode = null){
  26. try {
  27. return parent::queryInternal($method, $fetchMode);
  28. } catch (\yii\db\Exception $e) {
  29. if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) {
  30. $this->db->close();
  31. $this->db->open();
  32. $this->pdoStatement = null ;
  33. return parent::queryInternal($method, $fetchMode);
  34. }else{
  35. throw $e;
  36. }
  37. }
  38. }
  39. }

测试

  1. <?php
  2. namespace console\controllers;
  3. use yii\console\Controller;
  4. use Yii;
  5. class IndexController extends Controller
  6. {
  7. public function actionTest(){
  8. ini_set('default_socket_timeout', -1);
  9. for ($i=0;$i<=100;$i++){
  10. echo "\r\n";
  11. sleep(1);
  12. echo date('H;i:s').":".$i;
  13. try{
  14. $a = \yii::$app->tinymeng->createCommand('SELECT id,username FROM user WHERE id=1');
  15. $result = $a->queryOne();
  16. echo json_encode($result);
  17. }catch (\Exception $exception){
  18. echo "close !";
  19. }
  20. }
  21. }
  22. }

执行中重启服务器数据库

  1. 17;36:07:0{"id":"1","username":"admin"}
  2. 17;36:08:1{"id":"1","username":"admin"}
  3. 17;36:09:2{"id":"1","username":"admin"}
  4. 17;36:10:3{"id":"1","username":"admin"}
  5. 17;36:11:4{"id":"1","username":"admin"}
  6. 17;36:12:5{"id":"1","username":"admin"}
  7. 17;36:13:6{"id":"1","username":"admin"}
  8. 17;36:14:7{"id":"1","username":"admin"}
  9. 17;36:15:8{"id":"1","username":"admin"}
  10. 17;36:17:9{"id":"1","username":"admin"}
  11. 17;36:18:10{"id":"1","username":"admin"}
  12. 17;36:19:11{"id":"1","username":"admin"}
  13. 17;36:20:12{"id":"1","username":"admin"}
  14. 17;36:21:13{"id":"1","username":"admin"}
  15. 17;36:22:14{"id":"1","username":"admin"}
  16. 17;36:23:15close !
  17. 17;36:24:16close !
  18. 17;36:25:17close !
  19. 17;36:27:18{"id":"1","username":"admin"}
  20. 17;36:29:19{"id":"1","username":"admin"}
  21. 17;36:30:20{"id":"1","username":"admin"}
  22. 17;36:31:21{"id":"1","username":"admin"}


关注TinyMeng博客,更多精彩分享,敬请期待!
 

很赞哦! ()