"No such file or directory" or "No such host is known" when running migrations

56,774

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

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

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

to config/database.php file.

Share:
56,774
Jay Bienvenu
Author by

Jay Bienvenu

Software developer with a focus on data-driven applications for business and more.

Updated on December 13, 2020

Comments

  • Jay Bienvenu
    Jay Bienvenu over 3 years

    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')
    
  • Ildar Nasurlaev
    Ildar Nasurlaev over 3 years
    Thank you! You are life saver)
  • Chords
    Chords almost 3 years
    Why does localhost work in SequelAce but not the .env?
  • Danny Sofftie
    Danny Sofftie about 2 years
    Found this after spending a couple of hours. Works like a charm