A
A
Alexander Bulatov2021-02-12 09:50:43
Yii
Alexander Bulatov, 2021-02-12 09:50:43

How to fix SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution?

Good day!

I'm trying to deploy an application on yii2 basic in docker.
Error SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution.

My files:

docker-compose.yml

version: '3.5'
services:
  php:
    image: yiisoftware/yii2-php:7.4-apache
    volumes:
      - ~/.composer-docker/cache:/root/.composer/cache:delegated
      - ./:/app:delegated
    ports:
      - '8000:80'
    depends_on:
      - db
  db:
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=verysecret
      - MYSQL_DATABASE=mdb
      - MYSQL_USER=yii2
      - MYSQL_PASSWORD=secret
    ports:
      - '33061:3306'
    volumes:
      - ./mysql-dump:/etc/dump


file ./config/db.php
return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=mysql;dbname=mdb',
    'username' => 'root',
    'password' => 'verysecret',
    'charset' => 'utf8mb4',
    'tablePrefix' => 'dtl_',

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


I would be grateful for any thought about solving this problem and especially for a solution.

UPD. Yes, indeed, the name of the service in the compose file must be the same host we are accessing. That is, the dsn parameter in the ./config/db.php file should be like this:
mysql:host=db;dbname=mdb
But I still get an error, but a different one. Here it is: UPD2. If you add a name to the container forcibly (a parameter in the container_name db service), then there will be an error:
SQLSTATE[HY000] [2002] Connection refused

Forbidden
You don't have permission to access this resource.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
planc, 2021-02-12
@planc

you have a container named db, and in the mysql config you connect to the mysql host
'dsn' => 'mysql:host=mysql;dbname=mdb',

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question