博文 > yii2 console MySQL server

yii2 console MySQL server

Php 10002018-09-18 15:26:00

yii2 console MySQL server has gone away

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

  1. <?php
  2. namespace common\lib;
  3. class Command extends \yii\db\Command{
  4. public function execute()
  5. {
  6. try {
  7. return parent::execute();
  8. } catch (\yii\db\Exception $e) {
  9. if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) {
  10. $this->db->close();
  11. $this->db->open();
  12. $this->pdoStatement = null ;
  13. return parent::execute();
  14. }else{
  15. throw $e;
  16. }
  17. }
  18. }
  19. protected function queryInternal($method, $fetchMode = null){
  20. try {
  21. return parent::queryInternal($method, $fetchMode);
  22. } catch (\yii\db\Exception $e) {
  23. if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) {
  24. $this->db->close();
  25. $this->db->open();
  26. $this->pdoStatement = null ;
  27. return parent::queryInternal($method, $fetchMode);
  28. }else{
  29. throw $e;
  30. }
  31. }
  32. }
  33. }
  34. 'db' => [
  35. 'class' => 'yii\db\Connection',
  36. 'dsn' => 'mysql:host='.$db_config['host'].';dbname='.$db_config['dbname'],
  37. 'username' => $db_config['username'],
  38. 'password' => $db_config['password'],
  39. 'charset' => 'utf8',
  40. 'commandClass'=>"\\common\\lib\\Command", //<==注意这里要替换
  41. ],