我对此进行了测试,PHP 7.4 中的 mysqli 确实支持 caching_sha2_password。
php -v
PHP 7.4.0 (cli) (built: Nov 29 2019 16:18:44) ( NTS )
这是我编写的一个简单的 PHP 脚本,用于连接到在 docker 容器中运行的 MySQL 8.0.17(使用端口 6603):
<?php
error_reporting(E_ALL);
$conn = new mysqli('127.0.0.1', 'root', 'root', '', 6603);
$result = $conn->query("SELECT NOW()");
$now = $result->fetch_row()[0];
echo "$now\n";
我确认我的 MySQL 实例正在使用 caching_sha2_password:
mysql> select user,host,plugin from mysql.user where user='root';
+------+-----------+-----------------------+
| user | host | plugin |
+------+-----------+-----------------------+
| root | % | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------+-----------+-----------------------+
mysql> select @@default_authentication_plugin;
+---------------------------------+
| @@default_authentication_plugin |
+---------------------------------+
| caching_sha2_password |
+---------------------------------+
运行我的PHP脚本,成功了。
php my.php
2019-12-08 18:11:58
但是,当我尝试将密码更改为''
就像你一样,我能够重现相同的错误:
PHP 警告:mysqli::__construct():在第 5 行 /Users/bkarwin/Documents/SO/my.php 中执行 caching_sha2 auth: 0 时出现意外的服务器响应
当我恢复密码并将其设置为非空字符串时,问题就解决了。
不要使用空白密码。