【问题标题】:Can't run migrations in Laravel 8: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known [closed]无法在 Laravel 8 中运行迁移:SQLSTATE [HY000] [2002] php_network_getaddresses:getaddrinfo 失败:名称或服务未知 [关闭]
【发布时间】:2021-03-22 01:43:32
【问题描述】:

我不明白为什么我的迁移在全新安装 Laravel 8 时失败。这就是我得到的结果:

 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 = bunny and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:678
    674▕         // If an exception occurs when attempting to run a query, we'll format the error
    675▕         // message to include the bindings with SQL, which will make this exception a
    676▕         // lot more helpful to the developer instead of just the database's errors.
    677▕         catch (Exception $e) {
  ➜ 678▕             throw new QueryException(
    679▕                 $query, $this->prepareBindings($bindings), $e
    680▕             );
    681▕         }
    682▕ 

      +33 vendor frames 
  34  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

我正在使用带有完美 Laravel 环境的 Kali linux,但版本 8 无法正常工作。

【问题讨论】:

  • 这表示它无法解析你的选项中指定的数据库的IP(可能是你的.env文件)
  • 检查.env 文件是否正确设置了DB_HOST(并且不会出现两次,具有正确和错误的值)。
  • 你尝试过什么调试问题?

标签: php mysql laravel laravel-8


【解决方案1】:

只需在 .env 文件中将 DB_HOST 的值更改为 127.0.0.1

“将DB_HOST=mysql 更改为DB_HOST=127.0.0.1

【讨论】:

  • 我很高兴它有帮助。 @angelito-tan
  • 从问题中的信息来看,无法知道是什么问题,更别说这个答案是否适用了。
【解决方案2】:

在这种情况下,需要检查 .env 文件并将 DB_HOST=mysql 更新为 DB_HOST=localhost 安装 laravel 后我也遇到了同样的问题。

【讨论】:

    【解决方案3】:

    在 .env 文件中将 DB_HOST 的值更改为 127.0.0.1 并没有完全解决我的问题,我还必须重建整个 docker/sail:

    sail down -v

    sail build --no-cache

    先跑后跑

    sail up -d

    我可以跑:

    ./artisan migrate:fresh

    当数据库凭据发生更改时需要重建。

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题 我安装了两个php环境,第一个是xamp的,第二个是电脑的 转到计算机位置。 "C:\Program Files\php-8.0.3" 打开文件 "php.ini" 并添加 没有;

      extension=php_pdo_mysql
      extension=php_pdo_mysql.so 
      

      第二个 点击xamp配置按钮更改配置并在文件php.ini中添加这两行

      extension=php_pdo_mysql
      extension=php_pdo_mysql.so 
      

      没有分号的警告,不要加分号;

      【讨论】:

      • 请在您的答案中添加一些解释,以便其他人可以从中学习。通常,如果缺少扩展名,错误看起来会有所不同
      猜你喜欢
      • 2020-03-28
      • 2015-07-22
      • 2017-06-26
      • 2017-08-13
      • 2019-01-14
      • 2023-03-24
      • 2023-04-02
      • 2017-02-21
      • 2013-12-02
      相关资源
      最近更新 更多