分析: 确认过后版本一致,判断是 mysql 的配置问题 解决: [mysqld] default_authentication_plugin = mysql_native_password 重启数据库 结果: 成功
分析: php 7.4.x 之前 mysqli 扩展连接默认是用 mysql_native_password 密码验证插件, php 7.4.x 之后 mysqli 扩展连接默认是用 caching_sha2_password 密码验证插件。 所以php 7.4 之后在 mysql 配置中不默认验证插件为 mysql_native_password 时, mysqli扩展 会使用 caching_sha2_password 密码验证插件连接, 故报错。 解决: 1. 重装 php 环境到 7.4.0 以下版本 【不推荐】 2. 根据上面来源进行解决: [mysqld] default_authentication_plugin = mysql_native_password 重启数据库
分析-1: php 7.4.x 的mysqli扩展支持新的 caching_sha2_password,判断 navicat 使用的密码验证插件 caching_sha2_password 版本问题 解决: 在 mysql 最新的客户端使用密码验证插件 caching_sha2_password 修改密码 ALTER USER 'root'@'%' IDENTIFIED BY 'ffc' PASSWORD EXPIRE NEVER; ALTER USER 'root'@'%' IDENTIFIED WITH caching_sha2_password BY 'ffc'; FLUSH PRIVILEGES; 结果: 失败, 仍然报错:Message: mysqli::real_connect(): (HY000/1045): Access denied for user 'root'@'114.254.211.9' (using password: YES). 结论: 与 navicate 的密码验证插件无关 分析-2: 与操作执行的顺序有关。因为当前用户在修改当前用户的密码验证方式,导致此连接不再可用,那么修改之后是否生效呢? 解决: 在 navicat 或者 mysql 客户端中, 在当前用户登录连接后, 通过密码验证插件 caching_sha2_password 修改当前用户的密码, 然后使用当前用户的用户名和新密码重新连接成功后, 成功在 php 7.4.x 的 mysqli 扩展中可以正常连接。 思考: 如果修改的不是当前用户的密码,是否需要断开重新连接呢? 答案: 是需要的,并且要使用修改的用户的用户名和新密码进行重新登录mysql服务器后,才可以在 php 7.4.x 的 mysqli 扩展中连接。
在解决问题之后,对密码验证插件 caching_sha2_password 有些模糊,果然在官网找到了相应解释
Cache clearing operations affect the authentication requirements for subsequent client connections. For each user account, the first client connection for the user after any of the following operations must use a secure connection (made usingTCPusingTLS credentials, a Unix socket file, or shared memory) or RSA key pair-based password exchange:After account creation.After a password change for the account.AfterRENAMEUSERfor the account.AfterFLUSH PRIVILEGES.FLUSHPRIVILEGES clears the entire cache and affects all accounts that use the caching_sha2_password plugin.The other operations clear specific cache entries and affect only accounts that are part of the operation.Once the user authenticates successfully, the account is entered into the cache and subsequent connections do not require a secure connection or the RSA key pair, until another cache clearing event occurs that affects the account.(When the cache can be used, the server uses a challenge-response mechanism that does not use cleartext password transmission and does not require a secure connection.)
大概意思就是:
缓存清除操作会影响后续客户端连接的身份验证要求。 对于每个用户帐户,在执行以下任何操作之后, 该用户的第一个客户端连接必须使用安全连接(使用TLS凭证,Unix套接字文件或共享内存的TCP进行安全连接)或基于RSA密钥对的密码交换: 创建帐户后。 更改帐户密码后。 为该帐户 RENAME USER之后。 FLUSH PRIVILEGES 之后。 FLUSH PRIVILEGES清除整个缓存并影响使用该caching_sha2_password插件的所有帐户 。其他操作将清除特定的缓存条目,并且仅影响属于该操作的帐户。
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!