需求

要使用 mysqli 相关的函数, 你必须以启用 mysqli 扩展的方式编译 PHP。

MySQL 8

使用 PHP 7.1.16 之前的版本或者 PHP 7.2(PHP 7.2.4 之前的版本), 需要将 MySQL 服务器的默认密码插件设置为:mysql_native_password。 否则,当你连接的时候就会看到类似这样的错误: The server requested authentication method unknown to the client [caching_sha2_password]。 即使你未使用 caching_sha2_password 也会这样。

发生这种错误的原因是,MySQL 8 服务器默认会使用 caching_sha2_password 扩展, 老版本的 PHP 驱动(mysqlnd)无法识别这个扩展。 所以需要在 MySQL 的配置文件 my.cnf 中,设置 default_authentication_plugin=mysql_native_password。 在后续的 PHP 发行版本中,会提供对 MySQL caching_sha2_password 扩展的支持。 目前只有 mysql_xdevapi 扩展是 支持 MySQL 的 caching_sha2_password 扩展的。

add a note add a note

User Contributed Notes 2 notes

up
-7
james at jmwhite dot co dot uk
3 years ago
PHP 7.4 now supports MySQL with caching_sha2_password, although it is a bit unclear around older versions, there seems to be conflicting reports. What is the support of caching_sha2_password in PHP < 7.4?
up
-49
alvaro at demogracia dot com
5 years ago
Supported authentication methods are listed in the "Loaded plugins" row of the "mysqlnd" section in phpinfo(). Look for plugins that start with "auth_plugin_…".
To Top