エラー発生
Laravel導入直後にデータベースを接続しようとしたところ、エラーが発生した。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
[vagrant@localhost laravel_test]$ 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_test_development and table_name = migrations and table_type = 'BASE TABLE') at /home/vagrant/app/laravel/laravel_test/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| Exception trace: 1 PDOException::("PDO::__construct(): The server requested authentication method unknown to the client [caching_sha2_password]") /home/vagrant/app/laravel/laravel_test/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70 2 PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=laravel_test_development", "root", "vagrant", []) /home/vagrant/app/laravel/laravel_test/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70 Please use the argument -v to see more details. |
MySQLのバージョンは8.0.23、PHPのバージョンは7.3.28で、MySQLのデフォルトの認証はcacing_sha2_passwordだが、PHPは7.3でもこれに対応しておらずmysql_native_passwordらしい。
MySQLの認証方法変更
認証方法の確認。
1 2 3 4 5 6 7 8 9 10 |
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 | caching_sha2_password | +------------------+-----------+-----------------------+ 4 rows in set (0.00 sec) |
root
の認証方法をmysql_native_password
に変更しようとするが、エラー。
1 2 |
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'vagrant'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements |
パスワードがポリシーに適合していない。これは新たにMySQLをインストールした時にある状況。パスワード検証内容を確認してみる。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
mysql> SHOW VARIABLES LIKE 'validate_password%'; +--------------------------------------+--------+ | Variable_name | Value | +--------------------------------------+--------+ | validate_password.check_user_name | ON | | validate_password.dictionary_file | | | validate_password.length | 8 | | validate_password.mixed_case_count | 1 | | validate_password.number_count | 1 | | validate_password.policy | MEDIUM | | validate_password.special_char_count | 1 | +--------------------------------------+--------+ 7 rows in set (0.00 sec) |
設定するパスワードに合わせてポリシーなどを変更。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
mysql> SET GLOBAL validate_password.policy=LOW; Query OK, 0 rows affected (0.00 sec) mysql> SET GLOBAL validate_password.length=6; Query OK, 0 rows affected (0.00 sec) mysql> SHOW VARIABLES LIKE 'validate_password%'; +--------------------------------------+-------+ | Variable_name | Value | +--------------------------------------+-------+ | validate_password.check_user_name | ON | | validate_password.dictionary_file | | | validate_password.length | 6 | | validate_password.mixed_case_count | 1 | | validate_password.number_count | 1 | | validate_password.policy | LOW | | validate_password.special_char_count | 1 | +--------------------------------------+-------+ 7 rows in set (0.00 sec) |
改めてrootユーザーの認証方法を変更・確認。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'パスワード'; Query OK, 0 rows affected (0.00 sec) 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 | +------------------+-----------+-----------------------+ 4 rows in set (0.00 sec) |
これでPHP7.3以下で接続可能になる。