[英]Laravel 5.5 SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known
After installing Laravel 5.5 for a new project, I get this error:为新项目安装 Laravel 5.5 后,出现此错误:
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known.
.env db connection info: .env 数据库连接信息:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=lavavelproject
DB_USERNAME=root
DB_PASSWORD=
Dev environment: Windows PHP 7.0开发环境:Windows PHP 7.0
Try to clear cache by terminal:尝试通过终端清除缓存:
php artisan config:cache
Do it always when you change config file.更改配置文件时始终执行此操作。
Sometimes the most common issue is using localhost
, change this to 127.0.0.1
and it should work for you.有时最常见的问题是使用
localhost
,将其更改为127.0.0.1
,它应该适合您。
Sometimes you will have to run:有时你必须运行:
php artisan cache:clear
and in the rare moments as I have found also running php artisan key:generate
php artisan cache:clear
并且在我发现的罕见时刻也在运行php artisan key:generate
I had same problem.我有同样的问题。 The solution was to change
DB_HOST=mariadb
to DB_HOST=localhost
.解决方案是将
DB_HOST=mariadb
更改为DB_HOST=localhost
。
If it matters, my localhost is 127.0.0.1.如果重要的话,我的本地主机是 127.0.0.1。
Anyway, for my project and my PC config, the problem comes from Apache (XAMP), I think.无论如何,对于我的项目和我的 PC 配置,我认为问题来自 Apache (XAMP)。
My project run in Docker containers.我的项目在 Docker 容器中运行。
I put in my ".env" file above two lines,我把我的“.env”文件放在两行上方,
DB_HOST=mariadb
#DB_HOST=localhost
. #DB_HOST=localhost
。
The last line (localhost) is commented and not change when I run the project.最后一行 (localhost) 被注释并且在我运行项目时不会改变。 For running
php artisan
commands, I'll deactivate "mariadb" and activate "localhost".为了运行
php artisan
命令,我将停用“mariadb”并激活“localhost”。 Simple trick to not change much.不改变太多的简单技巧。
面对同样的问题,将DB_HOST=mysql
更改为DB_HOST=localhost
解决了我的问题。
On your application .env should be pointing to the IP of your database.在您的应用程序中, .env应该指向您的数据库的 IP。 Your database is the same server as your Laravel application, it should be:
你的数据库和你的 Laravel 应用程序是同一个服务器,它应该是:
.env .env
DB_HOST=127.0.0.1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.