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

2018-12-31 09:27发布

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.

28条回答
不流泪的眼
2楼-- · 2018-12-31 10:07

As of Laravel 5 the database username and password goes in the .env file that exists in the project directory, e.g.

DB_HOST=127.0.0.1
DB_DATABASE=db1
DB_USERNAME=user1
DB_PASSWORD=pass1

As you can see these environment variables are overriding the 'forge' strings here so changing them has no effect:

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

More information is here https://mattstauffer.co/blog/laravel-5.0-environment-detection-and-environment-variables

查看更多
梦醉为红颜
3楼-- · 2018-12-31 10:08

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.

查看更多
春风洒进眼中
4楼-- · 2018-12-31 10:08

If anyone are still looking for the answer, just check your .env file. For some reason laravel create a .env.example file, so all this answers didn't work for me. I fixed my issue renamming .env.example to .env

查看更多
倾城一夜雪
5楼-- · 2018-12-31 10:09

Building on the answer from @dcarrith ...

Instead of editing the config files, I created an alias in the location that PHP is looking that connects to the real mysql.sock. (source)

Just run these two commands (no restart needed):

mkdir /var/mysql
ln -s /tmp/mysql.sock /var/mysql/mysql.sock
查看更多
春风洒进眼中
6楼-- · 2018-12-31 10:09

In may case, I'd simply used

vagrant up

instead of

homestead up

for my forge larval setup using homestead. I'm assuming this meant the site was getting served, but the MySQL server wasn't ever booted. When I used the latter command to launch my vagrant box, the error went away.

查看更多
萌妹纸的霸气范
7楼-- · 2018-12-31 10:11

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.

查看更多
登录 后发表回答