16
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Docker】MySQLに接続できないエラー(php_network_getaddresses: getaddrinfo failed: Name or service not known)

Posted at

Dockerを用いてlaravelの環境構築を試みている時に起こったエラーです。

開発環境

Docker 20.10.5
docker-compose 1.28.5
php 7.2
laravel 7.30.4
Composer 2.0.12
MySQL 8.0
nginx 1.15.12-alpine

エラー

Dockerで作成したコンテナに入り、MySQLに接続するために
php artisan migrateを実行するとターミナルに下記のエラーが表示される。

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

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667|         // If an exception occurs when attempting to run a query, we'll format the error
    668|         // message to include the bindings with SQL, which will make this exception a
    669|         // lot more helpful to the developer instead of just the database's errors.
    670|         catch (Exception $e) {
  > 671|             throw new QueryException(
    672|                 $query, $this->prepareBindings($bindings), $e
    673|             );
    674|         }
    675| 

      +36 vendor frames 
  37  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

解決策

調べてみると上記エラーは.envでホスト名が間違ってる時に出るものだそうです。

.env
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=laravel_local
DB_USERNAME=laravel_user
DB_PASSWORD=laravel123
docker-compose.yml
version: '3' 

services:
    
  #省略

  mysql:
    image: mysql:8.0
    container_name: app_db
    ports:
      - 3306:3306
    environment:
      MYSQL_DATABASE: laravel_local
      MYSQL_ROOT_USER: root
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: laravel_user
      MYSQL_PASSWORD: laravel123
      TZ: 'Asia/Tokyo'
      
    volumes:
      - ./docker-config/mysql/data:/var/lib/mysql
      - ./docker-config/mysql/my.cnf:/etc/mysql/conf.d/my.cnf

    depends_on:
      - mysql-volume

  mysql-volume:
    image: busybox
    volumes:
      - ./docker-config/mysql/data:/var/lib/mysql

  #省略

.envの中のDB_HOST=dbが誤ってるということなので調べてみると、
DB_HOSTはserviceの名前らしいです。
今回はdocker-compose.ymlでmysqlと命名しているので合わせる必要があります。

.env
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel_local
DB_USERNAME=laravel_user
DB_PASSWORD=laravel123

これで再度コンテナ内でphp artisan migrateを実行すれば上記のエラーは解消されます。

おまけ(SQLSTATEエラー)

このエラーは.envのどこの設定が間違っているかで表示されるものが違うんじゃないかと思い、ロールバックして試してみます。

DB_CONNECTIONが異なる場合

Database connection [DB] not configured.

DB_PORTが異なる場合

SQLSTATE[HY000] [2002] Connection refused

DB_HOSTが異なる場合

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

DB_DATABASEが異なる場合

SQLSTATE[HY000] [1045] Access denied for user 'laravel_user'@'%' (using password: YES) 

DB_USERNAMEが異なる場合

SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client

DB_PASSWORDが異なる場合

SQLSTATE[HY000] [1045] Access denied for user 'laravel_user'@'%'
16
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
18

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?