LoginSignup
0
0

More than 3 years have passed since last update.

Laravelでmigrate出来なかった(できた)

Posted at

どうしたの

こちらの記事を参考にさせていただきながらLaravelのチュートリアルを進めている最中、migrateをしようとしたところ

terminal
SQLSTATE[HY000] [2054] The server requested authentication 
method unknown to the client 
(SQL: select * from info rmation_schema.tables where 
table_schema = homestead and table_name = migrations)

というエラーが出て困ったので、その解決ログ。
環境は以下の通り。
- macOS Mojave 10.14.5
- MySQL 8.0.16
- Laravel 5.5.46(環境の違いが怖かったので上記記事と同じバージョン)

原因

どうやらMySQL8.0.4以降は認証方式が従来のものから変更されているらしく、それが原因みたい(こちらの記事を参考にさせて頂きました)

という訳で認証方式を変更する。

まずmysqlにログインしてから(参考記事の通りだとrootでログイン、各環境に合わせてください)

terminal
$ mysql -uroot

以下のコマンドで対象ユーザの認証方式を変更する。

terminal
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';

今回はユーザ名が"root"、ホスト名が"localhost"、パスワードが""(空文字列またはnull)だったので上記で変更できた。より一般的には

terminal
mysql> ALTER USER 'username'@'hostname' IDENTIFIED WITH mysql_native_password BY 'password';

で変更できるみたい(間違ってたらごめんなさい)。

ユーザー名は以下で確認しましょう。

terminal
mysql> SELECT user, host, plugin FROM mysql.user;

+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | mysql_native_password |
+------------------+-----------+-----------------------+

これを見るとroot以外の認証方式がcaching_sha2_passwordという形式になってるのが分かりますね。それとうまくrootの認証方式を変更できていることもわかります。

上記の通り変更してから、

terminal
mysql> exit;

でMySQLからログアウトする。その後もう一度migrateを行う。

terminal
$ php artisan migrate

すると

terminal
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Migrating: 2019_07_11_151815_create_tasks_table
Migrated:  2019_07_11_151815_create_tasks_table

できた!!
最後にちゃんと上記の通りtableが作成されているか確認しておきましょう。

terminal
$ mysql -uroot
mysql> show tables from homestead ;
+---------------------+
| Tables_in_homestead |
+---------------------+
| migrations          |
| password_resets     |
| tasks               |
| users               |
+---------------------+

いい感じですね。

まとめ

環境構築で死ぬのが怖くてLaravelのバージョンを上げられません(こわい)。
ところで認証方式を変更しちゃうの、セキュリティ的にどうなんでしょうという気がします。要調査という感じですね。ひとまず解決できて安心です。

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