当前位置:网站首页>技术分享 | MySQL:caching_sha2_password 快速问答
技术分享 | MySQL:caching_sha2_password 快速问答
2022-06-26 07:41:00 【jeanron100】
作者:胡呈清
爱可生 DBA 团队成员,擅长故障分析、性能优化,个人博客:https://www.jianshu.com/u/a95ec11f67a8,欢迎讨论。
本文来源:原创投稿
*爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。
一个报错
在使用客户端登录MySQL8.0时,我们经常会遇到下面这个报错:
ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
网络上很多帖子教我们将用户认证插件修改成 mysql_native_password 来解决,那么事实上这是怎么一回事呢?本文就来探讨一二。
caching_sha2_password 简介
caching_sha2_password 是 MySQL 8.0.4 引入的一个新的身份验证插件,它的特点从其命名就可以窥探出一二:
sha2_password:其实就是 sha256_password,这是 MySQL5.6 就引入的身份验证插件,其优点是对加盐密码进行多轮 SHA256 哈希,以确保哈希转换更安全。其缺点为它要求使用安全连接或使用 RSA 密钥对进行密码交换的未加密连接,因此其身份验证的效率较低。
caching:在 sha256_password 的基础上增加缓存,有缓存的情况下不需要加密连接或 RSA 密钥对,已达到安全和效率并存。
其实上面这个介绍不太容易懂,下面我们以问答方式来揭开 caching_sha2_password 的面纱。
Q:要求使用安全连接或使用 RSA 密钥对进行密码交换的未加密连接是什么意思?
caching_sha2_password 对密码安全性要求更高,要求用户认证过程中在网络传输的密码是加密的:
如果是 SSL 加密连接,则使用 SSL 证书和密钥对来完成 "对称加密密钥对(在TSL握手中生成)" 的交换,后续使用“对称加密密钥对” 加密密码和数据。具体见:MySQL:SSL 连接浅析;
如果是非 SSL 加密连接,则在连接建立时客户端使用 MySQL Server 端的 RSA 公钥加密用户密码,Server 端使用 RSA 私钥解密验证密码的正确性,可以防止密码在网络传输时被窥探。
tips:SSL 加密连接会不止会加密用户密码,还会加密数据(SQL 请求、返回的结果);非加密连接只使用 RSA 密钥对进行用户密码的加密。
Q:未加密连接是怎么使用 RSA 密钥对进行密码交换的?
当用户验证成功后,会把用户密码哈希缓存起来。新连接客户端发起登录请求时,MySQL Server 端会判断是否命中缓存,如果没有缓存,对于未加密的连接,caching_sha2_password 插件要求连接建立时使用 RSA 进行加密密码交换,否则报错,其过程为:
客户端如果拥有服务端的 RSA 公钥,则使用 --server-public-key-path 选项指定 RSA 公钥文件;
客户端使用 RSA 公钥对用户密码进行加密,请求连接;
服务端使用 RSA 私钥进行解密,验证密码的正确性。
如果客户端没有保存服务端的 RSA 公钥文件,也可以使用 --get-server-public-key 选项从服务器请求公钥,则在建立连接时,服务端会先将 RSA 公钥发送给客户端。
如果 --server-public-key-path、--get-server-public-key 都没有指定,则会报下面这个经典的错误:
[[email protected] ~] mysql -h172.16.21.4 -utest -ptestpass --ssl-mode=disable
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.指定 --get-server-public-key 则能成功登录:
[[email protected] ~] mysql -h172.16.21.4 -utest -ptestpass --ssl-mode=disable --get-server-public-key -e "select 1"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---+
| 1 |
+---+
| 1 |
+---+如果 test 用户登陆成功,有了缓存,则下次认证时未加密连接不再要求使用 RSA 密钥对:
[[email protected] ~] mysql -h172.16.21.4 -utest -ptestpass --ssl-mode=disable -e "select 1"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---+
| 1 |
+---+
| 1 |
+---+注意:上述客户端是指 mysql 默认命令行客户端,--server-public-key-path、--get-server-public-key 参数也只适用于 mysql 客户端
RSA 密钥对保存在哪里?
RSA 钥对默认保存 MySQL datadir 下,用于非 SSL 连接时的密码加密交换:使用 RSA 公钥加密密码,使用 RSA 私钥解密:
private_key.pem RSA公钥
public_key.pem RSA私钥Q:密码哈希缓存何时失效?
当用户验证成功后,密码哈希会缓存起来,缓存会在以下情况被清理:
当用户的密码被更改时;
当使用 RENAME USER 重命名用户时;
执行 FLUSH PRIVILEGES 时;
MySQL 重启。
Q:复制用户使用 caching_sha2_password 插件需要注意什么?
对于 MGR ,如果设置 group_replication_ssl_mode=DISABLED ,则也必须使用下面的变量来指定 RSA 公钥,否则报错:
group_replication_recovery_get_public_key :向服务端请求 RSA 公钥;
group_replication_recovery_public_key_path :指定本地 RSA 公钥文件。
设置一个就行,考虑拷贝 RSA 公钥到各节点麻烦,建议设置 group_replication_recovery_get_public_key=ON 。
对于异步/半同步复制,需要在 change master 命令中指定:MASTER_PUBLIC_KEY_PATH = 'key_file_path' 或 GET_MASTER_PUBLIC_KEY = {0|1}
含义同上,建议:GET_MASTER_PUBLIC_KEY = 1
参考资料
https://dev.mysql.com/blog-archive/mysql-8-0-4-new-default-authentication-plugin-caching_sha2_password/
https://dev.mysql.com/doc/refman/8.0/en/caching-sha2-pluggable-authentication.html
本文关键字:#caching_sha2_password#
文章推荐:
新特性解读 | MySQL 8.0:explain analyze 分析 SQL 执行过程
关于SQLE
爱可生开源社区的 SQLE 是一款面向数据库使用者和管理者,支持多场景审核,支持标准化上线流程,原生支持 MySQL 审核且数据库类型可扩展的 SQL 审核工具。
如何获取
| 类型 | 地址 |
|---|---|
| 版本库 | https://github.com/actiontech/sqle |
| 文档 | https://actiontech.github.io/sqle-docs-cn/ |
| 发布信息 | https://github.com/actiontech/sqle/releases |
| 数据审核插件开发文档 | https://actiontech.github.io/sqle-docs-cn/3.modules/3.7_auditplugin/auditplugin_development.html |
更多关于 SQLE 的信息和交流,请加入官方QQ交流群:637150065...
边栏推荐
- QT basics tutorial: qstring
- Crosslinked porphyrin based polyimide ppbpi-2, ppbpi-1-cr and ppbpi-2-cr; Porous porphyrin based hyperbranched polyimide (ppbpi-1, ppbpi-2) supplied by Qiyue
- Error: the specified LINQ expression contains a reference to a query associated with a different context
- Children play games (greed, prefix and) - Niuke winter vacation training camp
- [UVM basics] understanding of sequence and sequencer
- 有序排列
- 一文分析EventBus-事件总线的使用方法和实现原理
- php array_ Merge details
- 蓝桥杯嵌入式学习总结(新版)
- Class class of box selection four to and polygon box selection based on leaflet encapsulation
猜你喜欢

Tsinghua Yaoban chendanqi won Sloan award! He is a classmate with last year's winner Ma Tengyu. His doctoral thesis is one of the hottest in the past decade
![[recommend 10 easy idea plug-ins with less tedious and repetitive code]](/img/74/69ca02e3d83404f7b0df07c308a59d.png)
[recommend 10 easy idea plug-ins with less tedious and repetitive code]

The performance of iron and steel enterprises was expected to be good in January this year. Since February, the prices of products of iron and steel enterprises have increased significantly. A mighty

What are the characteristics of digital factory in construction industry

What is Wi Fi 6 (802.11ax)? Why is Wi Fi 6 important?

Okhttp3 source code explanation (IV) cache strategy, disadvantages of Android mixed development

MXNet对NIN网络中的网络的实现

Basic use of swiperefreshlayout, local refresh of flutterprovider

Nine hours, nine people and nine doors (01 backpack deformation) - Niuke

Here is the command to display the disk space usage. Go ahead and pay attention to more wonderful things!
随机推荐
JS modularization
Jemter stress test - basic requirements - [teaching]
Minor problems in importing D
Liquid crystal texture diagram of purple solid mm-tpp-10c methacrylic acid decanoxy tetraphenyl porphyrin and mm-tpp-12c methacrylic acid dodecanoxy tetraphenyl porphyrin - Qi Yue display
Xiaosha's counting (bit operation, Combinatorial Mathematics) - Niuke
Es performance tuning and other features
Alkynyl crosslinked porphyrin based polyimide materials (ppbpi-h-cr, ppbpi Mn cr.ppbpi Fe Cr); Metalloporphyrin based polyimide (ppbpi Mn, ppbpi FE) supplied by Qiyue
信息学奥赛一本通 1355:字符串匹配问题(strs)
[UVM practice] Chapter 2: a simple UVM verification platform (4) the ultimate masterpiece of UVM: sequence
Jemter 压力测试 -基础请求-【教学篇】
My colleague asked a question I never thought about. Why did kubernetes' superfluous' launch the static pod concept?
Tetra - (4-pyridyl) porphyrin tpyp and metal complexes zntpyp/fetpyp/mntpyp/cutpyp/nitpyp/cotpyp/ptpyp/pdtpyp/cdtpyp (supplied by Qiyue porphyrin)
Cloud native integration data warehouse heavy release
OSPF design principles, commands take H3C as an example
Liangshui Xianmu shows his personal awareness as a unity3d worker
Es string type (text vs keyword) selection
Redis (4) -- Talking about integer set
Installation homebrew error summary
职场“大冤种”,不仅身累,心也被掏空……
Teach you how to use the harmonyos local simulator