0

I try to run a yii2 basic project with docker in macOS BigSur. This is my 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

and this is 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',
];

I use the same folder migrations like in the yii2 advanced. When I run command ./yii migrate, the error "Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: nodename nor servname provided, or not known' " is occured. What I did wrong? I try a lot of stuff on internet and don't understand this one. And not to solve my problem. --> How fix the error Exception when execute yii migrate?

I can access my database. I can not do any migrations. trow me this error:

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}

  • 1
    Does this answer your question? [How fix the error Exception when execute yii migrate?](https://stackoverflow.com/questions/51331174/how-fix-the-error-exception-when-execute-yii-migrate) – rob006 May 01 '21 at 09:43
  • No, I didn't understand what we say... if is to change something on my docker or if is to change something on DB connection – Daniel Batista May 01 '21 at 10:07
  • I can acces on my database... I can't do migrations... – Daniel Batista May 01 '21 at 10:16
  • Try to name your mysqldb container with `container_name` property in docker config and use that name as host in db.php – Scilef May 05 '21 at 15:27

1 Answers1

0

I solve my question using this command. I don't know why but solves. (I´m guessing I´m using docker side).

docker-compose run --rm php yii migrate