PDOException SQLSTATE[HY000] [2002] No such file or directory

后端 未结 30 3205
南方客
南方客 2020-11-22 03:23

I believe that I\'ve successfully deployed my (very basic) site to fortrabbit, but as soon as I connect to SSH to run some commands (such as php artisan migrate

30条回答
  •  清歌不尽
    2020-11-22 03:40

    For anyone trying to create a fresh db connection not on laravel but bumped here seeking for answers to run PDO from the Terminal. This would be of help to you. And you can refactor it to work best for you.

    SOCKS == '')
           {
               // run shell command to get 
               $socks = shell_exec('netstat -ln | grep mysql');
               $socks = trim(substr($socks, strpos($socks, '/')));
    
               $this->SOCKS = strlen($socks) > 0 ? ';unix_socket='.$socks : '';
           }
           else
           {
              $this->SOCKS = ';unix_socket='.$this->SOCKS;
           }
    
           $dsn = $this->DRIVER.':host='.$this->DBHOST.';dbname='.$this->DB;
    
           // add socks
           $dsn .= $this->SOCKS;
    
           // add port
           $dsn .= (strlen($this->PORT) > 0) ? ';port='.$this->PORT : '';
    
           // extablish connection
           $con = new PDO($dsn, $user, $pass);
    
           // return PDO instance.
           return $con;
       }
       // - ends here
    
       // now you can call $this->con() within class to use connection 
       // would run fine on any terminal
    
    } 
    

    hope it helps!

提交回复
热议问题