在MySQL 8.4中,如果启用了`mysql_native_password`插件但未正确加载,可能会遇到错误信息Plugin 'mysql_native_password' is not loaded。以下是一些可能的解决步骤:1.

摘要:现象 最近遇到一个有趣的案例:在一个新创建的 MySQL 8.4 实例中,使用用户 u2 登录时,返回了Plugin 'mysql_native_password' is not loaded
现象 最近遇到一个有趣的案例:在一个新创建的 MySQL 8.4 实例中,使用用户 u2 登录时,返回了Plugin 'mysql_native_password' is not loaded错误。 $ mysql -h127.0.0.1 -P3316 -uu2 -p123 mysql: [Warning] Using a password on thecommandline interface can be insecure. ERROR 1524 (HY000): Plugin'mysql_native_password'is not loaded 奇怪的是,检查mysql.user表后却发现: 实例里并没有 u2 这个用户; 现有用户中并没有用户在使用 mysql_native_password。 mysql>selecthost,user,pluginfrommysql.user; +-----------+------------------+-----------------------+ | host | user | plugin | +-----------+------------------+-----------------------+ | % | root | caching_sha2_password | | localhost | mysql.infoschema | caching_sha2_password | | localhost | mysql.session | caching_sha2_password | | localhost | mysql.sys | caching_sha2_password | | localhost | root | caching_sha2_password | +-----------+------------------+-----------------------+ 5 rows inset(0.05sec) 有意思的是,同样是不存在,如果使用 u1 登录,返回的却是Access denied for user 'xxx'@'xxx'错误: $ mysql -h127.0.0.1 -P3316 -uu1 -p123 mysql: [Warning] Using a password on thecommandline interface can be insecure. ERROR 1045 (28000): Access deniedforuser'u1'@'127.0.0.1'(using password: YES) 问题来了: 同样不存在,为什么 u1 和 u2 会返回不同的错误? 明明没有用户在使用 mysql_native_password,为什么 MySQL 会提示Plugin 'mysql_native_password' is not loaded? 根因分析 下面结合 MySQL 客户端与服务端的认证流程,来分析上述报错。 一、 客户端向 MySQL 服务端发起连接请求。 二、 服务端收到请求后,会调用do_auth_once()函数对客户端进行身份认证。 首次认证时,MySQL 会调用默认的密码认证插件进行认证。 在 MySQL 8.4 之前,默认的密码认证插件由 default_authentication_plugin 参数决定: 5.7 默认是 mysql_native_password。 8.0 改成了 caching_sha2_password。 到了 8.4,移除了这个参数,默认插件被固定为 caching_sha2_password。 所以,在 MySQL 8.4 中,MySQL 会调用 caching_sha2_password 插件向客户端发送一个握手包(handshake packet)。 握手包的内容包括通信协议版本、服务端版本、随机数(盐值)、服务端能力标志、默认字符集编号、密码认证插件名称等。 三、 客户端收到握手包后,默认会根据包中指定的认证插件(在 MySQL 8.4 中是 caching_sha2_password)生成并返回一个握手响应包(handshake response)。 响应包的内容包括客户端能力标志、用户名、加密后的密码、要连接的库名、客户端使用的认证插件等。 四、 服务端收到响应包后,会调用parse_client_handshake_packet()函数进行处理。该函数主要做: 读取客户端能力标志。
阅读全文