MySQL8.xとLaravelのmigrate時にSQLSTATE[HY000] [2054] The server requested authentication method unknown to the client ・・・のエラーが出たときの対処法

  • 2020.03.10
  • PHP
MySQL8.xとLaravelのmigrate時にSQLSTATE[HY000] [2054] The server requested authentication method unknown to the client ・・・のエラーが出たときの対処法
                 
最終更新日から90日以上経過しています。

MySQL8.xとLaravelでmigrateを実行する際に掲題のエラーが発生した為、原因・対応方法についてメモします。

発生したエラー

発生したエラーは以下の通りです。

$ php artisan migrate 
 Illuminate\Database\QueryException  : SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = hogehoge_database and table_name = migrations and table_type = 'BASE TABLE')

 

原因については実はTraceに表示されていました。

Exception trace:

1 PDOException::("PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]")

要するに「caching_sha2_password」という認証方法は知らないですよという形。

原因

公式のリファレンスをみると、MySQL8.0から優先する認証方法が変わり、前述の認証方法となった模様。

ただし、接続に使用しているPDOはその認証方法に対応していない為、エラーとなった。

対応方法

認証方法はユーザー毎に管理されているので、ALTER文で更新してあげる。公式より引用します。

ALTER USER user IDENTIFIED WITH caching_sha2_password BY 'password';

userとpasswordは変更対象のユーザー(パスワード)を指定します。この時、ユーザーのホストも’user’@’localhost’の形で指定してあげます。

PHPカテゴリの最新記事