关于PHP 8.4编译配置与MySQL 8.4 caching_sha2_password认证的技术咨询
背景情况
我在MySQL中创建了采用caching_sha2_password认证方式的用户,执行的SQL语句如下:
CREATE USER '[user]'@'[host]' IDENTIFIED WITH caching_sha2_password BY '[password]'; GRANT INSERT, SELECT ON [db].[table] TO '[user]'@'[host]';
我特意没有使用mysql_native_password,因为该认证方式在MySQL 8.4中默认禁用,且在MySQL 9.0中已被移除。
查阅官方文档时,我看到了两处相关说明:
PHP 8.4「Choosing a library」部分的编译建议:
// Recommended, compiles with mysqlnd
$ ./configure --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd
// Alternatively recommended, compiles with mysqlnd
$ ./configure --with-mysqli --with-pdo-mysql
// Not recommended, compiles with libmysqlclient
$ ./configure --with-mysqli=/path/to/mysql_config --with-pdo-mysql=/path/to/mysql_configMySQL 8.4「8.4.1.2 Caching SHA-2 Pluggable Authentication」部分的说明:
Installing SHA-2 Pluggable Authentication
The caching_sha2_password plugin exists in server and client forms:- The server-side plugin is built into the server, need not be loaded explicitly, and cannot be disabled by unloading it.
- The client-side plugin is built into the libmysqlclient client library and is available to any program linked against libmysqlclient.
The server-side plugin uses the sha2_cache_cleaner audit plugin as a helper to perform password cache management. sha2_cache_cleaner, like caching_sha2_password, is built in and need not be installed.
当前遇到的问题
我当前编译PHP 8.4使用的配置命令是:
./configure \ --prefix=$path_to_php_bin \ --with-apxs=/usr/local/apache2/bin/apxs \ --enable-mbstring \ --with-mysqli \ --with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-iconv=$(brew --prefix libiconv) \ --enable-fpm
但当我在网页中尝试创建mysqli连接时,触发了致命错误:
Fatal error: Uncaught mysqli_sql_exception: The server requested authentication method unknown to the client [caching_sha2_password] in [path_to_global_php_commands_include_file]:201 #0 [path_to_global_php_commands_include_file](201): mysqli_connect([host], [user], [password], [db]) #1 [path_to_mysqli_connect_variables_file](10): connectDBi([host], [user], [password], [db]) #2 [path_to_php/html_header_file](12):include('[path_to_mysqli_connect_variables_file]') #3 [path_to_website_index_file](3): include('[path_to_php/html_header_file]') #4 {main} thrown in [path_to_global_php_include_commands_file] on line 201
我猜测这个错误是因为我没有使用libmysqlclient编译PHP导致的——毕竟PHP文档明确不推荐使用libmysqlclient,但MySQL文档又指出需要链接该库才能使用caching_sha2_password的客户端插件。
我的疑问
- 如果要在MySQL 8.*环境下使用caching_sha2_password,必须用libmysqlclient编译PHP 8.*吗?那为什么PHP官方会推荐不这么做?
- 有没有比用libmysqlclient编译PHP 8.*更好的方式来启用caching_sha2_password支持?
- 如果我必须用libmysqlclient编译,使用命令:
$ ./configure --with-mysqli=/path/to/mysql_config --with-pdo-mysql=/path/to/mysql_config,那my.conf里需要添加什么配置才能让PHP编译后有效支持caching_sha2_password? - 在
--with-mysqli=...和--with-pdo-mysql=...里的mysql_config指的是my.conf吗?
麻烦各位大佬帮忙解答,谢谢!




