PDOException SQLSTATE[HY000] [2002] No such file o

2020-05-01 08:42发布

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 or php artisan db:seed) I get an error message:

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

At some point the migration must have worked, because my tables are there - but this doesn't explain why it isn't working for me now.

30条回答
Rolldiameter
2楼-- · 2020-05-01 09:00

I encountered the [PDOException] SQLSTATE[HY000] [2002] No such file or directory error for a different reason. I had just finished building a brand new LAMP stack on Ubuntu 12.04 with Apache 2.4.7, PHP v5.5.10 and MySQL 5.6.16. I moved my sites back over and fired them up. But, I couldn't load my Laravel 4.2.x based site because of the [PDOException] above. So, I checked php -i | grep pdo and noticed this line:

pdo_mysql.default_socket => /tmp/mysql.sock => /tmp/mysql.sock

But, in my /etc/my.cnf the sock file is actually in /var/run/mysqld/mysqld.sock.

So, I opened up my php.ini and set the value for pdo_mysql.default_socket:

pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock

Then, I restarted apache and checked php -i | grep pdo:

pdo_mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock

That fixed it for me.

查看更多
【Aperson】
3楼-- · 2020-05-01 09:01

If you are using Laravel Homestead, make sure you're calling the commands on the server.

homestead ssh

Then simply cd to the right directory and fire your command there.

查看更多
仙女界的扛把子
4楼-- · 2020-05-01 09:03

If you are using Laravel Homestead, here is settings

(include Vagrant-Virtual Machine)

.bash-profile

alias vm="ssh vagrant@127.0.0.1 -p 2222"

database.php

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', '127.0.0.1'),
        'database'  => env('DB_DATABASE', 'homestead'),
        'username'  => env('DB_USERNAME', 'homestead'),
        'password'  => env('DB_PASSWORD', 'secret'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

Terminal

vm

vagrant@homestead:~/Code/projectFolder  php artisan migrate:install
查看更多
该账号已被封号
5楼-- · 2020-05-01 09:06

I had this problems when I was running my application using docker containers.

The solution was put the name of the MySQL service container I was using in docker_compose.yml on DB_HOST. In my case, it was db :

DB_HOST=db

Hope it helps.

查看更多
孤傲高冷的网名
6楼-- · 2020-05-01 09:07

In my case, I was running php artisan migrate on my mac terminal, when I needed to ssh into vagrant and run it from there. Hope that helps someone the headache.

查看更多
叛逆
7楼-- · 2020-05-01 09:09

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!

查看更多
登录 后发表回答