概念描述
从8.0.14版本开始,MySQL实现了双密码功能:允许用户同时有两个密码存在,这个新功能确实可以解决很多实际问题,典型使用场景为:
1. 密码修改:修改密码前后服务访问不中断,不影响业务。
2. 角色区分:两类不同的用户使用不同的密码,随时可以中断一类用户的使用。
从8.0.18版本开始,MySQL又提供了一个产生随机密码的功能,在创建用户时候可以指定随机密码,创建完用户后,会显示出一个20位长度的随机密码,再也不用去为想一个符合安全级别的复杂密码而发愁了。
测试验证
- 双密码功能测试场景:
-- 1. 创建业务用户app_user 和第一个密码 “old_password”: root@localhost:(none) 09:07:54 >create user app_user@'%' identified by 'old_password'; Query OK, 0 rows affected (0.08 sec) root@localhost:(none) 09:09:00 >grant all on testdb.* to app_user@'%'; Query OK, 0 rows affected (0.01 sec) root@localhost:(none) 09:09:16 >flush privileges; Query OK, 0 rows affected (0.01 sec) -- 2. 为用户app_user创建第二个密码(新密码为主密码,旧密码为辅助密码) root@localhost:(none) 09:10:47 >alter user app_user@'%' identified by 'new_password' RETAIN CURRENT PASSWORD; Query OK, 0 rows affected (0.01 sec) 说明:这里请记住关键字:“RETAIN CURRENT PASSWORD” 必须指定,这条语句是必须要带上的。 -- 3. 验证是否可以两个密码都同时能登录 ## 使用旧密码old_password登录 [root@c1 ~]# /data/mysql/mysql8032/bin/mysql -uapp_user -pold_password -h 192.168.139.128 -P 3832 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 42 Server version: 8.0.32 MySQL Community Server - GPL Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. app_user@192.168.139.128:(none) 09:17:04 >show grants; +------------------------------------------------------+ | Grants for app_user@% | +------------------------------------------------------+ | GRANT USAGE ON *.* TO `app_user`@`%` | | GRANT ALL PRIVILEGES ON `testdb`.* TO `app_user`@`%` | +------------------------------------------------------+ 2 rows in set (0.01 sec) app_user@192.168.139.128:(none) 09:17:09 >exit Bye ## 使用新密码new_password登录 [root@c1 ~]# /data/mysql/mysql8032/bin/mysql -uapp_user -pnew_password -h 192.168.139.128 -P 3832 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 43 Server version: 8.0.32 MySQL Community Server - GPL Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. app_user@192.168.139.128:(none) 09:17:30 >show grants; +------------------------------------------------------+ | Grants for app_user@% | +------------------------------------------------------+ | GRANT USAGE ON *.* TO `app_user`@`%` | | GRANT ALL PRIVILEGES ON `testdb`.* TO `app_user`@`%` | +------------------------------------------------------+ 2 rows in set (0.00 sec) 说明:经验证,同时使用新密码和旧密码都可以无缝登录MySQL数据库,权限都一样,可以同时使用。 附加试验:将新密码从 new_password修改为newnew_password root@localhost:(none) 09:31:29 >alter user app_user@'%' identified by 'newnew_password'; Query OK, 0 rows affected (0.01 sec) 使用修改后的新密码 和之前的旧密码 依然可以正常登录(新密码的修改不影响旧密码的使用) [root@c1 ~]# /data/mysql/mysql8032/bin/mysql -uapp_user -pnewnew_password -h 192.168.139.128 -P 3832 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 51 Server version: 8.0.32 MySQL Community Server - GPL Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. app_user@192.168.139.128:(none) 09:32:19 >exit Bye [root@c1 ~]# /data/mysql/mysql8032/bin/mysql -uapp_user -pold_password -h 192.168.139.128 -P 3832 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 52 Server version: 8.0.32 MySQL Community Server - GPL Copyright (c) 2000, 2023, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. app_user@192.168.139.128:(none) 09:32:23 > -- 4. 删除旧密码(辅助密码): root@localhost:(none) 09:22:47 >alter user app_user@'%' DISCARD OLD PASSWORD; Query OK, 0 rows affected (0.01 sec) root@localhost:(none) 09:23:06 >exit Bye ## 删除旧密码后,再使用旧密码登录被拒绝 [root@c1 ~]# /data/mysql/mysql8032/bin/mysql -uapp_user -pold_password -h 192.168.139.128 -P 3832 mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user 'app_user'@'c1' (using password: YES) [root@c1 ~]#
复制
- 随机密码功能测试场景:
## 创建用户时指定关键字RANDOM PASSWORD root@localhost:(none) 09:40:12 >create user user1@'%' IDENTIFIED BY RANDOM PASSWORD, -> user2@'10.202.173.%' IDENTIFIED BY RANDOM PASSWORD, -> user3@'%.enmotech.com' IDENTIFIED BY RANDOM PASSWORD; +-------+----------------+----------------------+-------------+ | user | host | generated password | auth_factor | +-------+----------------+----------------------+-------------+ | user1 | % | hF*T(GV)68n;569U5mYk | 1 | | user2 | 10.202.173.% | Vx6TQefn9/-SYeIu1<[R | 1 | | user3 | %.enmotech.com | OaGedK_@3@UVGe1a<:Zz | 1 | +-------+----------------+----------------------+-------------+ 3 rows in set (0.01 sec) root@localhost:(none) 09:42:26 >set password for user1@'%' to random; +-------+------+----------------------+-------------+ | user | host | generated password | auth_factor | +-------+------+----------------------+-------------+ | user1 | % | VWn*Anx;J__RFPknv{L] | 1 | +-------+------+----------------------+-------------+ 1 row in set (0.00 sec) root@localhost:(none) 09:42:44 > ## 控制随机密码长度的变量generated_random_password_length,默认为20个字符 root@localhost:(none) 09:42:44 >show variables like 'generated_random_password_length'; +----------------------------------+-------+ | Variable_name | Value | +----------------------------------+-------+ | generated_random_password_length | 20 | +----------------------------------+-------+ 1 row in set (0.01 sec) ## 可以在线修改随机密码的长度 root@localhost:(none) 09:45:32 >set generated_random_password_length=50; Query OK, 0 rows affected (0.00 sec) root@localhost:(none) 09:45:45 >set password for user3@'%.enmotech.com' to random; +-------+----------------+----------------------------------------------------+-------------+ | user | host | generated password | auth_factor | +-------+----------------+----------------------------------------------------+-------------+ | user3 | %.enmotech.com | 2Q&tfmV)XB74LDJS7eO;RaG1+uelUo0W635qh3pI.HRv}wsQ7, | 1 | +-------+----------------+----------------------------------------------------+-------------+ 1 row in set (0.00 sec)
复制
知识总结
- MySQL8.0.14版本以后的双密码可以有效解决修改密码产生的影响,同时也可以让两个密码承担起不同的作用。
- 如果用户已经存老密码/辅助密码,只修改新密码/主密码(不使用RETAIN CURRENT PASSWORD),老密码/辅助密码保持不变。
- 如果指定新密码为空,那么老密码也会被设置为空,不建议这样做。
- 对自身账号设置辅助密码,自身用户需要具有 APPLICATION_PASSWORD_ADMIN权限。
- 对其他账号设置辅助密码,自身用户需要有CREATE USER权限。
- MySQL8.0.18版本以后随机密码可以让创建复杂密码更加简单高效,并可以设置随机密码的长度。
参考文档
https://dev.mysql.com/doc/refman/8.0/en/password-management.html
最后修改时间:2023-04-21 09:11:42
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
学习了

1年前

评论
这个功能不错

2年前

评论
相关阅读
【MySQL 30周年庆】MySQL 8.0 OCP考试限时免费!教你免费领考券
墨天轮小教习
2797次阅读
2025-04-25 18:53:11
MySQL 30 周年庆!MySQL 8.4 认证免费考!这次是认真的。。。
严少安
821次阅读
2025-04-25 15:30:58
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
475次阅读
2025-04-17 17:02:24
MySQL 9.3 正式 GA,我却大失所望,新特性亮点与隐忧并存?
JiekeXu
420次阅读
2025-04-15 23:49:58
3月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
369次阅读
2025-04-15 14:48:05
记录MySQL数据库的一些奇怪的迁移需求!
陈举超
269次阅读
2025-04-15 15:27:53
MySQL 8.0 OCP 1Z0-908 考试解析指南(二)
JiekeXu
262次阅读
2025-04-30 17:37:37
SQL优化 - explain查看SQL执行计划(下)
金同学
246次阅读
2025-05-06 14:40:00
MySQL 8.4 新特性深度解析:功能增强、废弃项与移除项全指南
JiekeXu
221次阅读
2025-04-18 20:21:32
MySQL 8.0 OCP 1Z0-908 考试题解析指南
青年数据库学习互助会
218次阅读
2025-04-30 12:17:54