“No such file or directory” or “No such host is known” when running migrations

断了今生、忘了曾经 提交于 2021-01-09 12:30:30

问题


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')

回答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.




回答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



回答3:


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




回答4:


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.




回答5:


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




回答6:


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.




回答7:


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.



来源:https://stackoverflow.com/questions/46407490/no-such-file-or-directory-or-no-such-host-is-known-when-running-migrations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!