3
2

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&LaravelでSQLSTATE[HY000] [2002] Connection refusedが出た時の対処法

Last updated at Posted at 2020-07-04

はじめに

こんなエラーでた。

terminal
  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 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| 

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

結論

configがしっかり反映されていなかったので、以下のコマンドでconfigに.envの内容を反映させると解消された。

terminal(Docker内)
$ php artisan config:clear

対処法

php artisan tinkerで.envの内容が反映されているか確かめる

terminal(Docker内)
$ php atisan tinker

>> config('database')
=> [
     "default" => "mysql",
     "connections" => [
       "sqlite" => [
         "driver" => "sqlite",
         "url" => null,
         "database" => "xxx",
         "prefix" => "",
         "foreign_key_constraints" => true,
       ],
       "mysql" => [
         "driver" => "mysql",
         "url" => null,
         "host" => "mysql", // この辺に注目
         "port" => "3306",
         "database" => "xxx", // この辺に注目 
         "username" => "xxx", // この辺に注目
         "password" => "xxx", // この辺に注目
         "unix_socket" => "",
         "charset" => "utf8mb4",
         "collation" => "utf8mb4_unicode_ci",
         "prefix" => "",
         "prefix_indexes" => true,
         "strict" => true,
         "engine" => null,
         "options" => [],
       ],

       # 以下省略

configに、.envの内容が反映されてなかったら結論で示したコマンドでどうにかなるかも(自分は解決した)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?