LoginSignup
43
33

More than 3 years have passed since last update.

dockerを使ったlaravelでの「Connection refused」エラー

Last updated at Posted at 2019-10-27

趣旨

Connection refusedでdatabaseが繋がらない状況だったのでその対処をまとめました。

内容

以下を実行したところ、、

# php artisan migrate

Connection refusedのエラーをよく見るとlaravelの.env設定とdocker-composeの設定があっていないのではとの仮説


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

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

  Exception trace:

  1   PDOException::("SQLSTATE[HY000] [2002] Connection refused")
      /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  2   PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=database", "root", "password", [])
      /var/www/html/laravel/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70

  Please use the argument -v to see more details.

docker-compose.ymlを確認します。

docker-compose.yml
  mysql:
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: [データベースの名前]
      MYSQL_USER: [データベースのユーザ名]
      MYSQL_PASSWORD: [データベースのパスワード名]
      MYSQL_ROOT_PASSWORD: [データベースのパスワード名]
    ports:
      - "3306:3306"
    volumes:
      - mysql-data:/var/lib/mysql

laravelの中に置いている.envはdocker-compose.ymlで定義した名に合わせます。
一般的にPORTはmysqlなら3306で。
<公式>
https://readouble.com/laravel/5.4/ja/homestead.html

.env
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=[docker-composeで定義したデータベース名]
DB_USERNAME=[docker-composeで定義したユーザ名]
DB_PASSWORD=[docker-composeで定義したパスワード名]

DB_HOSTはdocker-compose.ymlで「mysql」と命名していたので、「mysql」としてますが、「db」などで命名している場合はDB_HOST=dbにしてください。
docker-composeでリスタートします。

 $ docker-compose restart

コンテナに入り、ご自身が作成したlaravelのルートファイルで再度php artisan migrateをします。

 $ docker-compose exec app bash

# ls
docker  docker-compose.yml  laravel

# cd laravel

# php artisan migrate

できました!!!

# php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.11 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.04 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.03 seconds)
43
33
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
43
33