Learn or Die

生涯勉強。Macです。

LaravelでMySQLの接続エラー

マイグレーションを実行するとエラーがでる

MAMP環境です。 php artisan migrateを実行すると以下のエラーが出る

Illuminate\Database\QueryException

  SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravel_task 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|

原因

私の場合は、.envファイルの記述ミスでした。 ポート番号をデフォルトのままにしていたのがエラーの原因です。

MAMPのポート番号を確認

  1. MAMPの「Open WebStart page」を押下 スクリーンショット 2020-05-10 21.02.21.png

  2. 開いたページにポート番号が載っています。 スクリーンショット 2020-05-10 21.03.04.png

  3. ポート番号を修正後再度マイグレーションを実行。

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.03 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.01 seconds)

無事実行できました