LoginSignup
16
11

More than 3 years have passed since last update.

Laravel 6 でmysqlのDBに接続できない!(解決方法)

Last updated at Posted at 2020-03-01

環境

macOS Catalina
Laravel Framework 6.17.1
PHP 7.2.28

Laravelを試してみようと思って公式(の和訳)のLaravel 4.2 Laravelクイックスタート
をやってみたところルーティング(app/routes.php)がなかったり、php artisan migrateを実行すると

Illuminate\Database\QueryException  : SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')

とかエラー出てなんやねんってなったので解決方法をメモ。

そもそもLaravel 4.2じゃなかった件

クイックスタート通りに

composer create-project laravel/laravel your-project-name --prefer-dist

をすると現在(2020年3月2日)ではlaravel6系がインストールされる。
僕の場合でもLaravel Framework 6.17.1がインストールされ、laravel4系に比べていくつか変更がありルーティングはroutes/web.phpに書くことになったのでapp/routes.phpなんてものはない。

最新の和訳(終)は5.8系だったけどどうせだから最新のものでやりたいじゃん?ってことで6系でクイックスタートできるように試行錯誤した。

適当にDB作成してなんとなく.envを編集してもlaravelさんはmysqlを認知してくれない

適当にやってたからかphp artisan migrateでマイグレートしようとしても

22 04:17:42 :test-app$ php artisan migrate

   Illuminate\Database\QueryException  : SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')

  at /Users/僕の名前/Documents/勉強/laravel/test-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| 

うーん🤔

laravelちゃんはなんだかmysqlのことがキライみたいで、いつもいつも不愛想にエラードボドボして、メッセージイタイイタイなのだった。

なのでmysqlをlaravelちゃんに認知してもらおう。
とりあえず新規ユーザー作成して権限与えてパスワード認証方式がcaching_sha2_passwordだとlaravelちゃんは対応してないからmysql_native_passwordに変更してあげよう。

やること箇条書きにするとこんな感じ

  • コンソールでmysqlにrootでログイン

    • mysql -u root
  • ユーザー作成

    • create user 'ユーザー名'@'localhost' identified by 'パスワード';
    • SELECT Host, User FROM mysql.user;//作成できたか確認。
  • DB作成

    • create database laravel;
    • show databases;//作成できたか確認。
  • 権限変更

    • grant all privileges on laravel.* to '作成したユーザー名'@'localhost';
    • FLUSH PRIVILEGES;
  • ユーザーのパスワード認証方式を変更

    • ALTER USER '作成したユーザー名'@'localhost' IDENTIFIED WITH mysql_native_password BY 'パスワード':
    • SELECT user, host, plugin FROM mysql.user;//変更できているか確認
  • ユーザーに移動

    • quitして一度mysqlから抜ける
    • コンソールでmysql -u 作成したユーザー名 -pパスワード:
    • show databases; //DBがあるか一応確認

これでLaravelさんがmysqlにアクセスする準備が整った。

あとは.env を修正する。

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel(DB名)
DB_USERNAME=作成したユーザー
DB_PASSWORD=作成したユーザーのパスワード

これでマイグレートが走るはず
php artisan migrate

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.02 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.02 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.01 seconds)
Migrating: 2020_03_01_172839_create_tasks_table
Migrated:  2020_03_01_172839_create_tasks_table (0.01 seconds)

できた!やったねチノちゃん🎉!

参考

Laravel 6 基本のタスクリスト
https://qiita.com/ucan-lab/items/36f6e89abad26a68f69a#laravel%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB

Laravel 6.x インストール
https://readouble.com/laravel/6.x/ja/installation.html

Laravel5.7、mysql8の環境でDB接続が失敗する場合の対応
https://qiita.com/akashixi/items/61f2fdb618c591bc41e2

16
11
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
16
11