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:17

[This is outdated, database info is stored in .env now] More easier way to solve the issue by is editing the

app/config/local/database.php

:)

Change the database configuration here and it should work

查看更多
裙下三千臣
3楼-- · 2018-12-31 10:18

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楼-- · 2018-12-31 10:20

It is the correct response for me. I disabled the option pdo_mysql.default_socket in my php.ini and set the database host at 127.0.0.1.

查看更多
公子世无双
5楼-- · 2018-12-31 10:21

Laravel 4: Change "host" in the app/config/database.php file from "localhost" to "127.0.0.1"

Laravel 5: Change "DB_HOST" in the .env file from "localhost" to "127.0.0.1"

I had the exact same problem. None of the above solutions worked for me. I solved the problem by changing the "host" in the /app/config/database.php file from "localhost" to "127.0.0.1".

Not sure why "localhost" doesn't work by default but I found this answer in a similar question solved in a symfony2 post. https://stackoverflow.com/a/9251924/1231563

Update: Some people have asked as to why this fix works so I have done a little bit of research into the topic. It seems as though they use different connection types as explained in this post https://stackoverflow.com/a/9715164/1231563

The issue that arose here is that "localhost" uses a UNIX socket and can not find the database in the standard directory. However "127.0.0.1" uses TCP (Transmission Control Protocol), which essentially means it runs through the "local internet" on your computer being much more reliable than the UNIX socket in this case.

查看更多
何处买醉
6楼-- · 2018-12-31 10:22

This is because PDO treats "localhost" host specially:

Note: Unix only: When the host name is set to "localhost", then the connection to the server is made thru a domain socket. If PDO_MYSQL is compiled against libmysqlclient then the location of the socket file is at libmysqlclient's compiled in location. If PDO_MYSQL is compiled against mysqlnd a default socket can be set thru the pdo_mysql.default_socket setting.

(from http://php.net/manual/en/ref.pdo-mysql.connection.php)

Changing localhost to 127.0.0.1 will "force" the use of TCP.

Note: mysqli_connect is working fine with localhost.

查看更多
妖精总统
7楼-- · 2018-12-31 10:24

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
查看更多
登录 后发表回答