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

后端 未结 30 2528
南方客
南方客 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

    I ran into this problem when running PHPUnit in Elixir/Gulp, and Homestead as my Vagrant enviroment.

    In my case I edited the .env file from DB_HOST=localhost to DB_HOST=192.168.10.10 where 192.168.10.10 is the IP of my Vagrant/Homestead host.

    0 讨论(0)
  • 2020-11-22 03:40

    I had the same problem using Docker and MySQL service name db in docker_compose.yml file:

    I added the following in the .env file:

    DB_HOST=db
    

    you should also assure that your host is discoverable from the php app.

    It was because PHP didn't figure out which host to use to connect.

    0 讨论(0)
  • 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.

    <?php
    
    class db
    {
       private $DBHOST = 'localhost'; // you don't need 127.0.0.1
       private $DRIVER = 'mysql';
       private $PORT   = '8888'; // database port. 8888 is mine
       private $DB     = 'example-db';
       private $PASS   = 'example-pass';
       private $USER   = 'root';
       private $SOCKS  = ''; // can fill this or leave blank.
    
    
       // - connect (dummy connection)
       private function con()
       {
           if ($this->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!

    0 讨论(0)
  • 2020-11-22 03:41

    I'm running on MAMP Pro and had this similar problem when trying to migrate (create db tables). Tried a few of these mentioned suggestions as well but didn't do it for me.

    So, simply (after an hour googling), I added two things to the /config/database.php.

    'port' => '1234',
    'unix_socket' => '/path/to/my/socket/mysqld.sock'
    

    Works fine now!

    0 讨论(0)
  • 2020-11-22 03:44

    I had similar problems accessing my Drupal website. I fixed it by opening the command line, and restarting my MySQL server or service:

    service mysqld restart
    

    This should work. If it doesn't, restart your local webserver:

    service httpd restart
    

    That should be enough. Hope it works for other environments, too. Note that these commands generally require superuser privileges.

    0 讨论(0)
  • 2020-11-22 03:46

    The error message indicates that a MySQL connection via socket is tried (which is not supported).

    In the context of Laravel (artisan), you probably want to use a different / the correct environment. Eg: php artisan migrate --env=production (or whatever environment). See here.

    0 讨论(0)
提交回复
热议问题