LoginSignup
1
0

More than 3 years have passed since last update.

ローカル環境にDockerを使用しLaravelをgitからclone後、php artisan migrateのエラーでハマった件

Posted at

どうも、初心者プログラマーTKOです。
今回は新しいdocker環境を試そうと今まで作っていたLaravelアプリをgitからcloneした際に、php artisan migrateのエラーではまった件について話させていただきます。

新規Laravelプロジェクト 初期設定

リポジトリをクローン後

$ docker-compose up -d --build 

$ docker-compose exec app bash 

# composer install

これで設定したローカルのポート番号にアクセスするとLaravelの画面が表示されます。

ここから
mysqlのDockerfileで設定した

MYSQL_DATABASE=laravel_local
MYSQL_USER=phper
MYSQL_PASSWORD=***
MYSQL_ROOT_PASSWORD=***

の値を参考にホスト側の.envファイルを設定します。

DB_PORT=3306
DB_DATABASE=laravel_local
DB_USERNAME=phper
DB_PASSWORD=***

その後

# cp .env.example .env

# php artisan key:generate

後はマイグレーションで終わり

と思ったのですが、、、

  Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')

  at /work/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   Doctrine\DBAL\Driver\PDO\Exception::("SQLSTATE[HY000] [2002] Connection refused")
      /work/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDO/Exception.php:18

  2   Doctrine\DBAL\Driver\PDO\Exception::new(Object(PDOException))
      /work/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:42

  Please use the argument -v to see more details.

出ました!予想外のエラー、、、

とりあえず何らかのキャッシュが残っているんだろうなと思い

# php artisan config:clear

# composer dump-autoload

を試してみましたが解決せず

DBが登録されていないのかと思いDB内に入るも


$ docker-compose exec db bash 
# mysql -u phper -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| laravel_local      |
+--------------------+
2 rows in set (0.02 sec)

ちゃんと登録されている、、、

調べたところ

.envの
DB_HOST=127.0.0.1

DB_HOST=db
だそうです!

なるほど、dockerはコンテナ同士で通信してるからローカルのIPアドレスを指定しても意味ないってことですね。

以後気をつけます。

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