在Laravel中迁移表时,“SQLSTATE [HY000] [2002]没有这样的文件或目录”错误

I am getting the following error when I try to migrate a table in Laravel 5 using the "php artisan migrate" command:

'SQLSTATE[HY000] [2002] No such file or directory' in . . . /vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:47

My .env file includes the default settings as follows:

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

My database.php file lists mysql as the default database connection, homestead is listed as the database in my homestead.yaml file, and homestead is one of the tables listed when I access mysql and use the show databases; command.

Any thoughts about what I might be doing wrong?

This seems to be an issue with the socket file. Please let me know if this helps Starting with Zend Tutorial - Zend_DB_Adapter throws Exception: "SQLSTATE[HY000] [2002] No such file or directory"

try 127.0.0.1 instead of localhost in your .env file. It works for me :)

Add mysql.sock path in database.php file like below example

'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',

Eample

'mysql' => [
        'driver' => 'mysql',
        'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
        'host' => env('DB_HOST', 'localhost'),
        'port' => env('DB_PORT', '8889'),

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