Ken
Ken

Reputation: 3141

"SQLSTATE[HY000] [2002] No such file or directory" error when migrating table in Laravel

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?

Upvotes: 3

Views: 11305

Answers (5)

Hedayatullah Sarwary
Hedayatullah Sarwary

Reputation: 2834

Change localhost as your DATABASE_HOST in the .env file, change it to 127.0.0.1, and after that run php artisan config:clear and then run php artisan migrate:install again.

Or if your problem is not solved, use these changes in your .env files:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306

Then in Database.php add folder location:

'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),

Finally, try:

php artisan config:clear
php artisan migrate:install

Upvotes: 2

LivingPhate
LivingPhate

Reputation: 34

Okay, this is might be a special case to me however I had this issue.

I moved my files from a Laragon based server to a mamp based server on a different machine. I had this error for days and I only received this error when running php artisan migrate and not when loading the page. When loading the page i received table not found which is understandable as the table isn't there.

After quite some time I just though of running composer update and then it fixed the error and worked for me. Not sure if this helps anyone but I came back to this post in case it somehow does.

Upvotes: 0

Walter
Walter

Reputation: 81

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

Upvotes: 3

lucasloo
lucasloo

Reputation: 101

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

Upvotes: 10

Noman Ur Rehman
Noman Ur Rehman

Reputation: 6957

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"

Upvotes: 0

Related Questions