欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答

0. 导读

相同的账号、密码,手动客户端连接可以成功,通过MySQL Connectors却失败了,为什么?

1. 现象描述

通过MySQL C API编写的一个程序,在进行用户登录操作的时候,程序报错,登录失败。

但是如果通过mysql客户端,手动登录成功后,再启动客户端程序,不再报错,程序运行成功。

2. 抓包分析问题

学会抓包,就超越了90%的程序员。
sudo tcpdump -i any tcp and port xxx -s 1500 -w filename -v

C程序登录失败时的包

前两个包很正常,第三个包第一次见,wireshark解析的叫做 AuthSwitchRequest.

看下这个包的内容。

mysql客户端登录成功后,再执行C程序登录成功时的包

看下这个AuthSwithRequest包的内容。

认证成功和失败的包数据是不同的,分别是03和04。

3. AuthSwitchRequest包

首先看下AuthSwitchRequest包的官方解释。

Protocol::AuthSwitchRequest:
Authentication Method Switch Request Packet. If both server and client support CLIENT_PLUGIN_AUTH capability, server can send this packet to ask client to use another authentication method. Payload 1 [fe]
string[NUL] plugin name
string[EOF] auth plugin data

可以看出,这个包payload的第一个字节是0xfe,与抓包中的01是不同的,而且AuthSwitchRequest后面跟的应该是plugin name,是个字符串,而抓包中,内容是04。

所以wireshard解包错误了。

在协议里面我们找下payload第一个字节是01的包,找到了AuthMoreData的包。

Protocol::AuthMoreData:
Payload 1 [01]
string[EOF] plugin data

这个包的说明payload后面是string类型,和我们的抓包也是有些出入,先不管这个了。

跟到这里只能去代码里面找下这个包是什么。

4. 服务器认证

目前用户认证默认都走caching_sha2_password的plugin,之前版本都是mysql_native_password。

mysql > select user,host,plugin from mysql.user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+ mysql > show variables like '%auth%';
+-------------------------------+-----------------------+
| Variable_name | Value |
+-------------------------------+-----------------------+
| default_authentication_plugin | caching_sha2_password |
+-------------------------------+-----------------------+
1 row in set (0.01 sec)

服务器端认证代码在sql/auth/sha2password.cc文件中的caching_sha2_password_authenticate函数。

在此函数中,找到了发包的代码,正好可以对应到我们抓包的AuthMoreData。

 996   if (pkt_len != sha2_password::CACHING_SHA2_DIGEST_LENGTH) return CR_ERROR;
997
998 std::pair<bool, bool> fast_auth_result =
999 g_caching_sha2_password->fast_authenticate(
1000 authorization_id, reinterpret_cast<unsigned char *>(scramble),
1001 SCRAMBLE_LENGTH, pkt,
1002 info->additional_auth_string_length ? true : false);
1003
1004 if (fast_auth_result.first) {
1005 /*
1006 We either failed to authenticate or did not find entry in the cache.
1007 In either case, move to full authentication and ask the password
1008 */
1009 if (vio->write_packet(vio, (uchar *)&perform_full_authentication, 1))
1010 return CR_AUTH_HANDSHAKE;
1011 } else {
1012 /* Send fast_auth_success packet followed by CR_OK */
1013 if (vio->write_packet(vio, (uchar *)&fast_auth_success, 1))
1014 return CR_AUTH_HANDSHAKE;
1015 if (fast_auth_result.second) {
1016 const char *username =
1017 *info->authenticated_as ? info->authenticated_as : "";
1018 LogPluginErr(INFORMATION_LEVEL,
1019 ER_CACHING_SHA2_PASSWORD_SECOND_PASSWORD_USED_INFORMATION,
1020 username, hostname ? hostname : "");
1021 }
1022
1023 return CR_OK;
1024 }
1025

首先进行了fast_authenticate,根据这个结果fast_auth_result.first,分别发送了不同的包perform_full_authentication和fast_auth_success。

791 static char request_public_key = '\2';
792 static char fast_auth_success = '\3';
793 static char perform_full_authentication = '\4';

可以看到我们C程序登录失败时,给我们发送的是perform_full_authentication,而认证成功发送的是fast_auth_success包。

什么情况下会出现perform_full_authentication包呢?代码中给出了说明,我们就不去看fast_authenticate的代码逻辑了,从说明就能了解到大概情况。

1006 We either failed to authenticate or did not find entry in the cache. 1007 In either case, move to full authentication and ask the password

也就是说,如果cache中没有记录,或者认证失败,会进入perform_full_authentication流程。我们从这个认证插件的名字caching_sha2_password就可以知道,这是个带cache的认证插件。

而这正好解释了为什么用mysql客户端手动登陆后,我们C程序就登录成功了,因为cache中已经有了记录。

那么:为什么手动客户端认证就能成功呢?而我们自己写的C程序就会失败呢?

且看客户端认证分析。

5. 客户端认证

客户端认证的代码逻辑在sql-common/client_authentication.cc中的caching_sha2_password_auth_client函数中。

514     if (pkt_len != 1 || *pkt != perform_full_authentication) {
515 DBUG_PRINT("info", ("Unexpected reply from server."));
516 return CR_ERROR;
517 }
518
519 /* If connection isn't secure attempt to get the RSA public key file */
520 if (!connection_is_secure) {
521 public_key = rsa_init(mysql);
......
523 if (public_key == NULL && mysql->options.extension &&
524 mysql->options.extension->get_server_public_key) {
525 // If no public key; request one from the server.
......
540 }
541
542 if (public_key) {
543 /*
......
584 } else {
585 set_mysql_extended_error(mysql, CR_AUTH_PLUGIN_ERR, unknown_sqlstate,
586 ER_CLIENT(CR_AUTH_PLUGIN_ERR),
587 "caching_sha2_password",
588 "Authentication requires secure connection.");
589 return CR_ERROR;
590 }
591 } else {
592 /* The vio is encrypted already; just send the plain text passwd */
593 if (vio->write_packet(vio, (uchar *)mysql->passwd, passwd_len))
594 return CR_ERROR;
595 }

可以看到客户端收到perform_full_authentication包后,根据connection_is_secure进行了分支。

我们的客户端中的连接肯定是没有开启SSL的,所以会走进if (!connection_is_secure)流程。

mysql客户端登录默认是开启SSL认证的,故走了else流程。

我们使用mysql客户端登录时的命令如下,是开启了ssl的:

shell> bin/mysql -h127.0.0.1 -uroot -P3301 -ppassword

如果客户端想禁用ssl,需要加上–ssl-mode=disable选项。

所以,这就解释了为什么客户端能登录成功了。

现在我们看下if (!connection_is_secure)流程,发现没有进入下面的if流程,这样就不会生成public_key,导致public_key为空。

if (public_key == NULL && mysql->options.extension &&
524 mysql->options.extension->get_server_public_key)

具体来说,是因为我们的连接选项没有设置mysql->options.extension->get_server_public_key。

6. 解决方法

在不开启ssl选项的时候,我们需要设置get_server_public_key选项。

+  bool get_server_public_key = true;
+ mysql_options(m_client, MYSQL_OPT_GET_SERVER_PUBLIC_KEY,
+ &get_server_public_key);

参考文档

connection-phase-packets, https://dev.mysql.com/doc/internals/en/connection-phase-packets.html

authentication method mismatch, https://dev.mysql.com/doc/internals/en/authentication-method-mismatch.html

Enjoy GreatSQL

本文由博客一文多发平台 OpenWrite 发布!

技术分享|MySQL caching_sha2_password认证异常问题分析的更多相关文章

  1. 技术分享 | MySQL中MGR中SECONDARY节点磁盘满,导致mysqld进程被OOM Killed

    欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答 在MGR测试中,人为制造磁盘满问题后,节点被oom killed 问题描述 在对 ...

  2. 技术分享 | MySQL Group Replication集群对IP地址的限制导致的一些问题与解决办法

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 1. 遇到问题 测试人员小玲准备在docker环境中部署MGR集群进行一些测试,她有三个容器,容器IP分别是: 172.3 ...

  3. 技术分享 | MySQL数据误删除的总结

    欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答 内容提要 用delete语句 使用drop.truncate删除表以及drop删 ...

  4. 技术分享 | mysql 表数据校验

    1. checksum table. checksum table 会对表一行一行进行计算,直到计算出最终的 checksum 结果.比如对表 n4 进行校验(记录数 157W,大小为 4G) [yt ...

  5. 技术分享 | 简单测试MySQL 8.0.26 vs GreatSQL 8.0.25的MGR稳定性表现

    欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答 GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. M ...

  6. MySQL 8.0新增特性详解【华为云技术分享】

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...

  7. 尖峰7月线上技术分享--Hadoop、MySQL

      7月2号晚20:30-22:30 东大博士Dasight分享主题<大数据与Hadoop漫谈> 7月5号晚20:30-22:30  原支付宝MySQL首席DBA分享主题<MySQL ...

  8. 技术分享|闪回在MySQL中的实现和改进

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 3306π 福州站,以下内容是由万里数据库,研发工程师唐洁分享的MySQL闪回方案完整PPT. Enjoy GreatSQ ...

  9. 技术分享 | Prometheus+Grafana监控MySQL浅析

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 简介 Prometheus 一套开源的监控&报警&时间序列数据库的组合,通常 Kubernetes 中都会 ...

随机推荐

  1. CSS基础学习(一)

    1.设置背景颜色:background-color 例:background-color:#d0e4fe;或background-color:blue; 2.字体颜色·:color 例:color:r ...

  2. RabbitMQ 环境安装

    每日一句 Wisdom is knowing what to do next, skill is knowing how to do it, and virtue is doing it. 智慧是知道 ...

  3. Druid数据库连接池使用体验

    写在前面 在实际工作中我们我们使用较多的则是Spring默认的HikariDataSource数据库连接池,但是它无法提供可视化监控SQL这一能力,而这在很多场景下往往又是我们需要的功能,因此今天来学 ...

  4. CabloyJS究竟是一款什么样的框架

    CabloyJS是什么样的框架 CabloyJS 是一款自带工作流引擎的 Node.js 全栈框架,一款面向开发者的低代码开发平台,更是一款兼具低代码的开箱即用和专业代码的灵活定制的 PAAS 平台 ...

  5. 一文澄清网上对 ConcurrentHashMap 的一个流传甚广的误解!

    大家好,我是坤哥 上周我在极客时间某个课程看到某个讲师在讨论 ConcurrentHashMap(以下简称 CHM)是强一致性还是弱一致性时,提到这么一段话 这个解释网上也是流传甚广,那么到底对不对呢 ...

  6. 获取在线ip

    /** * 获取在线IP * @return String */ function getOnlineIp($format=0) { global $S_GLOBAL; if(empty($S_GLO ...

  7. Charles如何抓取https请求-移动端+PC端

    Charles安装完成,默认只能抓取到http请求,如果查看https请求,会显示unkonw或其它之类的响应.所以需要先进行一些配置,才能抓取到完整的https请求信息.下面针对PC端和手机端抓包的 ...

  8. Kubernetes-23:详解如何将CPU Manager做到游刃有余

    k8s中为什么要用CPU Manager? 默认情况下,kubelet 使用CFS配额来执行 Pod 的 CPU 约束.Kubernetes的Node节点会运行多个Pod,其中会有部分的Pod属于CP ...

  9. MySQL 8.0 新特性梳理汇总

    一 历史版本发布回顾 从上图可以看出,基本遵循 5+3+3 模式 5---GA发布后,5年 就停止通用常规的更新了(功能不再更新了): 3---企业版的,+3年功能不再更新了: 3 ---完全停止更新 ...

  10. 强化学习-Windows安装gym、atari和box2d环境

    安装gym pip3 install gym pip3 install gym[accept-rom-license] 安装atari环境[可选] 下载安装VS build tools 如果出现 OS ...