温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

执行缓存时出现意外的服务器响应\u sha2身份验证:109或者 (HY000/1045)怎么办

发布时间:2021-12-30 09:34:25 来源:亿速云 阅读:280 作者:柒染 栏目:大数据

执行缓存时出现意外的服务器响应\u sha2身份验证:109或者 (HY000/1045)怎么办,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

mysqli::real_connect(): Unexpected server response while doing caching_sha2 auth: 109 或者 Message: mysqli::real_connect(): (HY000/1045): Access denied for user 'root'@'114.254.211.9' (using password: YES).

前提条件:确定用户和密码已支持远程连接, 且密码的加密规则已修改为 密码验证插件 mysql_native_password 支持的密码

报错 mysqli::real_connect(): Unexpected server response while doing caching_sha2 auth: 109 来源1:mysql_native_password 本地可以连,线上连不了

    分析:
        确认过后版本一致,判断是 mysql 的配置问题
    解决:[mysqld]default_authentication_plugin = mysql_native_password

        重启数据库
    结果:
        成功

报错 mysqli::real_connect(): Unexpected server response while doing caching_sha2 auth: 109 来源2:mysql_native_password 线上可以连,本地由php 7.3.x 更新为 php 7.4.x 之后连不了

    分析:
        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

            重启数据库

报错 Message: mysqli::real_connect(): (HY000/1045): Access denied for user 'root'@'114.254.211.9' (using password: YES) 来源:php 7.4.x 环境中,在刚使用 docker 创建的数据库时默认的密码验证插件就是 caching_sha2_password ,而且可以连;之后同样密码验证插件是 caching_sha2_password,使用 navicate 修改密码后连不了。

    分析-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 using TCP using TLS 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.After RENAME USER for the account.After FLUSH PRIVILEGES.FLUSH PRIVILEGES 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插件的所有帐户 。其他操作将清除特定的缓存条目,并且仅影响属于该操作的帐户。

php 7.4.x 的 mysqli 扩展中默认密码验证插件为:caching_sha2_password

密码验证插件为:caching_sha2_password 的用户,在修改密码之后必须要重新使用客户端成功连接后方可在php 7.4.x 的 mysqli 扩展中连接。而密码验证插件为:mysql_native_password 的用户不需要重新通过客户端连接成功即可在php 7.4.x 的 mysqli 扩展中连接

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI