====== Laravel の php artisan migrate で The server requested authentication method unknown to the client と出るとき ======
Docker に限った話ではないのですが、マイグレーションをしようとした際に以下のエラーがでました。
$ 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_db and table_name = migrations and table_type = 'BASE TABLE')
at /var/www/production/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 Doctrine\DBAL\Driver\PDOException::("SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client")
/var/www/production/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:31
2 PDOException::("PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]")
/var/www/production/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:27
Please use the argument -v to see more details.
原因は、MySQL8.0から認証プラグインが caching_sha2_password に変更されたけれど、Laravel 側が対応していないためエラーが表示されるとのこと。
そこで、my.cnf に認証プラグインを mysql_native_password にするように記載し、Dockerfile で DBコンテナにコピーするようにする。
# ディレクトリは適時移動
$ vim conf.d/my.cnf
[mysqld]
default_authentication_plugin= mysql_native_password
$ vim Dockerfile
FROM mysql:latest
COPY ./conf.d /etc/mysql/conf.d
これで、コンテナを一度削除し、起動し直すと、マイグレーションに成功するはずです。