2
0

More than 3 years have passed since last update.

docker内にてマイグレーション時にエラーが出て詰まった話 PHP laravel

Last updated at Posted at 2020-10-07

docker内にてマイグレーション時にエラーが出て詰まった話 (laravel)

以下エラー内容です

terminal
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (SQL: select * from information_schema.tables where table_schema = db_name and table_name = migrations and table_type = 'BASE TABLE')

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

  Exception trace:

  1   PDOException::("PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known")
      /var/www/html/laravel-vue-app/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=mysql;port=3306;dbname=db_name", "db_user", "db_password", [])
      /var/www/html/laravel-vue-app/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  Please use the argument -v to see more details.

結論としてdocker-compose.ymlと.envのmysqlの設定は一緒じゃないとだめらしい

解決策

laravel(app/)配下の.envがあっているかどうか確認 ->ホスト名に関してはdockerのコンテナ名と合わせる必要がある
docker-compose.ymlの内容に相違がある場合変更する必要性アリ
変更後は  

docker
docker-compose stop
docker-compose up -d

を行う必要があるとのこと
理由:restartだとdocker-compose.ymlの設定が反映されないため
.envをの設定を反映させる場合は

terminal
php artisan config:cache

で反映させる必要がある

原因(追記)

どうやらlaravelがbiludされているappコンテナ-->mysqlがビルドされているdbコンテナ
同士の通信時にエラーが発生しているらしくて,
dockerのコンテナ同士の通信時にはmysqlがビルドされているコンテナにふられたIPアドレスを
使用する必要があるとのこと

terminal
cat /etc/hosts | awk ‘END{print $1}’ | sed -r -e ‘s/[0-9]+$/1/g’

でコンテナのIPアドレスを確認できます
app配下の.envのホストIPがいっしょかどうか確認したほうがいいでしょう

2
0
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
2
0