执行 yii migrate 时出现“异常”错误如何解决?

问题描述 投票:0回答:2

我尝试在 macOS BigSur 中使用 docker 运行 yii2 基本项目。这是我的 docker-compose.yml:

version: '2'
services:
  php:
    image: yiisoftware/yii2-php:7.4-apache
    volumes:
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      - ./:/app:delegated
    ports:
      - '8000:80'
  mysqldb:
    image: mysql
    ports:
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=p@ssw0rd1
      - MYSQL_DATABASE=yii2basic

这是 config/db.php:

<?php

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=mysqldb;port=3306;dbname=yii2basic',
    'username' => 'root',
    'password' => 'p@ssw0rd1',
    'charset' => 'utf8',

    // Schema cache options (for production environment)
    //'enableSchemaCache' => true,
    //'schemaCacheDuration' => 60,
    //'schemaCache' => 'cache',
];

我使用与 yii2 高级中相同的文件夹迁移。当我运行命令 ./yii migrate 时,出现错误“Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodenamenor servnameprovided, or notknown' “发生了。我做错了什么? 我在互联网上尝试了很多东西,但不明白这一点。而不是为了解决我的问题。 --> 执行 yii migrate 时出现异常如何解决?

我可以访问我的数据库。我无法进行任何迁移。给我这个错误:

php yii migrate
Yii Migration Tool (based on Yii v2.0.42-dev)

Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known'

in /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/db/Connection.php:651

Error Info:
Array
(
    [0] => HY000
    [1] => 2002
    [2] => php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known
)

Caused by: Exception 'PDOException' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known'

in /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/db/Connection.php:721

Caused by: Exception 'PDOException' with message 'PDO::__construct(): php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known'

in /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/db/Connection.php:721

Stack trace:
#0 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/db/Connection.php(721): PDO->__construct('mysql:host=mysq...', 'root', 'p@ssw0rd1', NULL)
#1 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/db/Connection.php(640): yii\db\Connection->createPdoInstance()
#2 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/db/Connection.php(1063): yii\db\Connection->open()
#3 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/db/Connection.php(1050): yii\db\Connection->getMasterPdo()
#4 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/db/Command.php(261): yii\db\Connection->getSlavePdo()
#5 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/db/Command.php(1163): yii\db\Command->prepare(true)
#6 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/db/Command.php(410): yii\db\Command->queryInternal('fetchAll', NULL)
#7 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/db/mysql/Schema.php(319): yii\db\Command->queryAll()
#8 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/db/mysql/Schema.php(125): yii\db\mysql\Schema->findColumns(Object(yii\db\TableSchema))
#9 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/db/Schema.php(758): yii\db\mysql\Schema->loadTableSchema('migration')
#10 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/db/Schema.php(194): yii\db\Schema->getTableMetadata('{{%migration}}', 'schema', true)
#11 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/console/controllers/MigrateController.php(211): yii\db\Schema->getTableSchema('{{%migration}}', true)
#12 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(894): yii\console\controllers\MigrateController->getMigrationHistory(NULL)
#13 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/console/controllers/BaseMigrateController.php(169): yii\console\controllers\BaseMigrateController->getNewMigrations()
#14 [internal function]: yii\console\controllers\BaseMigrateController->actionUp(0)
#15 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#16 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/base/Controller.php(181): yii\base\InlineAction->runWithParams(Array)
#17 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/console/Controller.php(184): yii\base\Controller->runAction('', Array)
#18 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/base/Module.php(534): yii\console\Controller->runAction('', Array)
#19 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/console/Application.php(181): yii\base\Module->runAction('migrate', Array)
#20 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/console/Application.php(148): yii\console\Application->runAction('migrate', Array)
#21 /Users/danielz3/PhpstormProjects/Portfoleo/vendor/yiisoft/yii2/base/Application.php(392): yii\console\Application->handleRequest(Object(yii\console\Request))
#22 /Users/danielz3/PhpstormProjects/Portfoleo/yii(20): yii\base\Application->run()
#23 {main}

php yii2 migration yii2-basic-app yii-migrations
2个回答
0
投票

我使用这个命令解决了我的问题。我不知道为什么,但解决了。 (我猜我正在使用 docker 端)。

docker-compose run --rm php yii migrate  

0
投票

如果您使用docker桌面,请在容器列表中选择容器并进入终端,然后输入

php yii migrate

希望这能帮助别人

© www.soinside.com 2019 - 2024. All rights reserved.