36

I deleted the migrations table from a Laravel 5.4 database named laravel. When I run php artisan migrate:install, I get this error:

[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] No such file or directory
(SQL: select * from information_schema.tables where table_schema = laravel
    and table_name = migrations)

I deleted and recreated the database. I also ran composer update. No luck. I can run the command in phpMyAdmin and create the table manually.

This problem sometimes also manifests itself with similar 2002 errors:

[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known.
(SQL: select * from information_schema.tables where table_schema = laravel
    and table_name = migrations and table_type = 'BASE TABLE')
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
(SQL: select * from information_schema.tables where table_schema = laravel
    and table_name = migrations and table_type = 'BASE TABLE')
miken32
  • 42,008
  • 16
  • 111
  • 154
Jay Bienvenu
  • 3,069
  • 5
  • 33
  • 44
  • this works. https://stackoverflow.com/questions/29305502/php-artisan-migrate-with-mamp-and-unix-socket – President Edewor Jul 18 '18 at 11:45
  • Does this answer your question? [PDOException: SQLSTATE\[HY000\] \[2002\] No such file or directory](https://stackoverflow.com/questions/29695450/pdoexception-sqlstatehy000-2002-no-such-file-or-directory) – Anand Mainali Jul 09 '20 at 15:48

8 Answers8

104

If you are using localhost as your DATABASE_HOST in the .env file, change it to 127.0.0.1, then run php artisan config:clear and now try php artisan migrate:install again.

miken32
  • 42,008
  • 16
  • 111
  • 154
Camilo
  • 6,504
  • 4
  • 39
  • 60
19

Here's what I recommend for .env file:

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
kaleazy
  • 5,922
  • 2
  • 47
  • 51
7

I fixed this issue by setting environment variables in .env file:

DB_CONNECTION=mysql 
DB_HOST=mysql 
DB_PORT=3306 

Actually I just changed from DB_HOST=127.0.0.1 to DB_HOST=mysql.

ahmnouira
  • 1,607
  • 13
  • 8
6

If you are using MAMP Pro (not always necessary for the free version of MAMP) then the best thing to do is add this to your .env file:

DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

This assumes your MAMP installation is at /Applications/MAMP of course (the default).

Credit: A comment in this question by @Uncoke

James Cushing
  • 718
  • 1
  • 9
  • 18
3

I am using MAMP on macOS and after editing localhost to 127.0.0.1 and port to 8888 this problem was fixed by adding the following

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

to config/database.php file.

felmez
  • 90
  • 10
3

For mac users, add this to your .env file

DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock  
MUHINDO
  • 788
  • 6
  • 10
2

In some cases this may happen because mysql server is not running. It happened to me using Laravel 5.7 and restarting mysql server solved it. For ubuntu check the status of mysql server service mysql status. You can restart mysql server using command service mysql restart

William Mandai
  • 304
  • 4
  • 8
0

Posting a resolution to my own question:

I deleted the folder and recreated the code base, making sure to point my environment to the correct database server. This time it worked. I don't know exactly what had gone missing to cause the original error.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Jay Bienvenu
  • 3,069
  • 5
  • 33
  • 44