MySQL SQLSTATE[HY000] [2002] php_network_getaddresses:getaddrinfo 失败:名称或服务未知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30182984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:59:51  来源:igfitidea点击:

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

mysqllaravellaravel-5openshift

提问by hjvcghcghchg

I download from https://github.com/luciddreamz/laravelLaravel for openshift then upload over my repository over github. This code for connect to database not work. The problem is that load variable from .envfile that locate in root of project

我从https://github.com/luciddreamz/laravelLaravel下载openshift,然后通过 github 上传到我的存储库。此连接到数据库的代码不起作用。问题是从.env位于项目根目录的文件加载变量

for solve this problem change.env

为了解决这个问题改变.env

# local environment only
#   for production, see .openshift/.env

APP_ENV=APPLICATION_ENV
APP_DEBUG=true
APP_URL=OPENSHIFT_APP_DNS
APP_KEY=OPENSHIFT_SECRET_TOKEN

DB_DRIVER=mysql
DB_HOST=OPENSHIFT_MYSQL_DB_HOST
DB_PORT=OPENSHIFT_MYSQL_DB_PORT
DB_DATABASE=OPENSHIFT_APP_NAME
DB_USERNAME=OPENSHIFT_MYSQL_DB_USERNAME 
DB_PASSWORD=OPENSHIFT_MYSQL_DB_PASSWORD

CACHE_DRIVER=apc
SESSION_DRIVER=file

my error :SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

我的错误:SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

createConnection('mysql:host=OPENSHIFT_MYSQL_DB_HOST;port=OPENSHIFT_MYSQL_DB_PORT;dbname=OPENSHIFT_APP_NAME', 
array('driver' => 'mysql', 
      'host' => 'OPENSHIFT_MYSQL_DB_HOST', 
      'port' => 'OPENSHIFT_MYSQL_DB_PORT', 
      'database' => 'OPENSHIFT_APP_NAME', 
      'username' => 'OPENSHIFT_MYSQL_DB_USERNAME', 
      'password' => 'OPENSHIFT_MYSQL_DB_PASSWORD', 
      'charset' => 'utf8',  
      'collation' => 'utf8_unicode_ci', 
      'prefix' => '', 
      'strict' => false, 
      'name' => 'mysql'), 
array('0', '2', '0', false, '0')) in MySqlConnector.php line 20

error message

错误信息

回答by Taz

First line of the error message describes the error type: "PDOException". The next line displays PDO::errorInfo, i.e:

错误消息的第一行描述了错误类型:“PDOException”。下一行显示PDO::errorInfo,即:

  1. SQLSTATE error code (a five characters alphanumeric identifier defined in the ANSI SQL standard).
  2. Driver-specific error code.
  3. Driver-specific error message.
  1. SQLSTATE 错误代码(ANSI SQL 标准中定义的五个字符的字母数字标识符)。
  2. 驱动程序特定的错误代码。
  3. 特定于驱动程序的错误消息。
  • "HY000" is a general server error (see Server Error Codes and Messagesin MySQL docs).
  • "2002" is MySQL Client Error Code meaning "Can't connect to local MySQL server through socket" (see (Client Error Codes and Messagesin MySQL docs).
  • The driver specific error code and message ("php_network_getaddresses: getaddrinfo failed: Name or service not known") tell you that PDO is not able to resolve the host name.
  • “HY000”是一般服务器错误(请参阅MySQL 文档中的服务器错误代码和消息)。
  • “2002”是 MySQL 客户端错误代码,意思是“无法通过套接字连接到本地 MySQL 服务器”(请参阅​​(MySQL 文档中的客户端错误代码和消息)。
  • 驱动程序特定的错误代码和消息(“php_network_getaddresses: getaddrinfo failed: Name or service not known”)告诉您 PDO 无法解析主机名。

The stack trace you attached, line 3, reveals that you did not specify the database connection parameters in the configuration file. The error show up when you test on local, right? You need to update /.envwith the actual database connection parameters.

您附加的堆栈跟踪(第 3 行)表明您没有在配置文件中指定数据库连接参数。在本地测试时会出现错误,对吗?您需要/.env使用实际的数据库连接参数进行更新。

回答by gauravbhai daxini

Check your DB_HOSTon your .envfile

检查您DB_HOST.env文件

DB_HOST=http://localhost/ --> DB_HOST=localhost


php artisan config:clearit will help you, clear cached config

php artisan config:clear它会帮助你,清除缓存的配置

回答by Hyder B.

I'm using Laravel with Docker. I solved this problem by changing the value of DB_HOSTfrom localhostto mysql.

我正在将 Laravel 与 Docker 一起使用。我改变的价值解决了这个问题DB_HOST,从localhostmysql

DB_HOST=mysql

回答by mare96

If someone facing this problem when using Docker, be sure of you are using your version of SQL.

如果有人在使用 时遇到此问题Docker,请确保您使用的是您的 SQL 版本。

In my case: MYSQL_VERSION=latestchanging to MYSQL_VERSION=5.7.

就我而言:MYSQL_VERSION=latest更改为MYSQL_VERSION=5.7.

Then you need to restart your Docker images with docker system prune -a.

然后你需要用docker system prune -a.

Also in your .envyou need to change DB_HOST=mysql. And run php artisan config:clear.

另外在您.env需要更改DB_HOST=mysql. 并运行php artisan config:clear

I think it will help someone.

我认为它会帮助某人。

回答by mare96

It looks like you did not add a MySQL database to your application, OR you added it after you installed laravel. In that case, you need to stop & start (not restart) your application so that it will pick up your environment variables. (rhc app stop , rhc app start ). If you did not add a database yet, you will need to add one of the mysql cartridges, and then stop & start your application using the previously shown commands.

看起来您没有在应用程序中添加 MySQL 数据库,或者您在安装 laravel 后添加了它。在这种情况下,您需要停止并启动(而不是重新启动)您的应用程序,以便它接收您的环境变量。(rhc 应用程序停止,rhc 应用程序启动)。如果您还没有添加数据库,您将需要添加一个 mysql 墨盒,然后使用前面显示的命令停止和启动您的应用程序。

回答by mukade

I was having the same problem and everyone was talking about this is related to DNS configuration, which make sense, since your container maybe isn't knowing how to resolve the name of the domain where your database is.

我遇到了同样的问题,每个人都在谈论这与 DNS 配置有关,这是有道理的,因为您的容器可能不知道如何解析数据库所在域的名称。

I guess your can configure that at the moment you start your container, but I think it's better to config this once and for all.

我想您可以在启动容器的那一刻进行配置,但我认为最好一劳永逸地进行配置。

I'm using Windows 10 and in this case docker's gui give us some facilities.

我使用的是 Windows 10,在这种情况下,docker 的 gui 为我们提供了一些便利。

Just right click on docker's icon in the tray bar and select "Settings" item.

只需右键单击托盘栏中的泊坞窗图标,然后选择“设置”项。

Docker's options

Docker 的选项

Then, on the Docker's window, select the "Network" section and change the DNS option from "Automatic" to "Fixed" and hit "Apply". Docker will restart itself after that. I putted the Google's DNS (8.8.8.8) and it worked fine to me.

然后,在 Docker 的窗口中,选择“网络”部分并将 DNS 选项从“自动”更改为“固定”,然后点击“应用”。之后 Docker 将自行重启。我使用了 Google 的 DNS (8.8.8.8),它对我来说效果很好。

docker's network section with dns config

带有 dns 配置的 docker 网络部分

Hope it helps.

希望能帮助到你。

回答by Douwe

We're using symfony with doctrine and we are in the process of automating deployment. I got this error when I simply hadn't provided the correct db creds in parameters.yml (I was running doctrine:migrations:migrate)

我们正在使用 symfony 和学说,我们正在自动化部署。当我没有在 parameters.yml 中提供正确的数据库凭证时,我收到了这个错误(我正在运行学说:迁移:迁移)

This thread has sent me on a bit of a wild goose chase, so I'm leaving this here so others might not have to.

这个线程让我有点疯狂,所以我把它留在这里,这样其他人可能就不必了。

回答by gtamborero

If you are on production server and the .env file doesn't work at all, go to /bootstrap/cache/config.php and on the line 230 more or less you will find the database data that is beeing cached from the .env file.

如果您在生产服务器上并且 .env 文件根本不起作用,请转到 /bootstrap/cache/config.php 并且在第 230 行或多或少您会发现正在从 .env 缓存的数据库数据文件。

'mysql' => 
  array (
    'driver' => 'mysql',
    'host' => '127.0.0.1',
    'port' => '3306',
    'database' => 'yorDBname',
    'username' => 'YOURUSERNAME',
    'password' => 'yourpass',
    'unix_socket' => '',
    'charset' => 'utf8mb4',
    'collation' => 'utf8mb4_unicode_ci',
    'prefix' => '',
    'strict' => true,
    'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
  ),

回答by ChronoFish

Just as on FYI to others suddenly experiencing this on an otherwise (previously) working Laravel installation:

就像其他人在其他(以前)工作的 Laravel 安装中突然遇到这种情况一样,仅供参考:

My Laravel environment is on a Ubuntu VirtualBox hosted on a Win-10 laptop.

我的 Laravel 环境位于 Win-10 笔记本电脑上托管的 Ubuntu VirtualBox 上。

I used my phone to share Internet via USB and suddenly started experiencing this issue. I changed to using the phone as wireless hotspot and the issue went away. This is a nameserver/networking issue and is not specific to Laravel or MySQL.

我用我的手机通过 USB 共享互联网,突然开始遇到这个问题。我改用手机作为无线热点,问题就解决了。这是一个名称服务器/网络问题,并不是 Laravel 或 MySQL 特有的。

回答by zmii

In my case, everything was set up correctly, but my Dockerinfrastructure needed more RAM. I'm using Docker for Mac, where default RAM was around 1 GB, and as MySQL uses around 1.5Gb of RAM ( and probably was crashing ??? ), changing the Docker RAM utilization level to 3-4 Gb solved the issue.

就我而言,一切都设置正确,但我的Docker基础设施需要更多 RAM。我正在使用 Mac 的 Docker,其中默认 RAM 大约为 1 GB,而 MySQL 使用大约 1.5 GB 的 RAM(并且可能正在崩溃???),将 Docker RAM 利用率级别更改为 3-4 Gb 解决了该问题。