今天我们来继续介绍 MySQL 8.0 的新密码策略, 分别为双密码策略和内置随机密码生成。
第一,双密码策略:
首先来解释下什么是双密码策略?双密码策略就是在日常运维中,需要定期更改指定用户密码,同时又需要旧密码暂时保留一定时长的一种策略。其作用是延迟应用与数据库之间的用户新旧密码对接时间,进而平滑应用的操作感知。可以在如下场景中使用:
管理员先创建一个新用户 ytt ,密码是 root_old ,完了更改他的密码为 root_new 。此时 root_new 即为主密码,而 root_old 即为备密码。
mysql:(none)>create user ytt identified by 'root_old';
Query OK, 0 rows affected, 2 warnings (0.24 sec)
mysql:(none)>alter user ytt identified by 'root_new' retain current password;
Query OK, 0 rows affected (0.17 sec)复制
接下来用户 ytt 分别使用备密码与主密码连接 MySQL 并且执行一条简单的 SQL 语句:
备密码连接数据库:
root@ytt-ubuntu:/home/ytt# mysql -h ytt-ubuntu -P 3306 -uytt -proot_old -e "select 'hello world'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| hello world |
+-------------+
| hello world |
+-------------+复制
主密码连接数据库:
root@ytt-ubuntu:/home/ytt# mysql -h ytt-ubuntu -P 3306 -uytt -proot_new -e "select 'hello world'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| hello world |
+-------------+
| hello world |
+-------------+复制
相关业务更改完成后,即可告知管理员丢弃备密码:
root@ytt-ubuntu:/home/ytt# mysql -S /opt/mysql/mysqld.sock
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 8.0.27 MySQL Community Server - GPL
...
mysql:(none)>alter user ytt discard old password;
Query OK, 0 rows affected (0.02 sec)
mysql:(none)>\q
Bye复制
双密码策略有以下需要注意的事项:
如果用户本身已经有双密码策略,再次更改新密码时没有带 retain current password 子句,那之前的主密码被替换成新改的密码,但是备密码不会被替换。比如更改新密码为 root_new_new ,此时备密码依然是 root_old ,并非之前的主密码 root_new 。下面例子中输入密码 root_old 依然可以连接数据库,而输入密码 root_new 则被数据库拒绝连接:
mysql:(none)>alter user ytt identified by 'root_new_new';
Query OK, 0 rows affected (0.16 sec)
root@ytt-ubuntu:/home/ytt# mysql -h ytt-ubuntu -u ytt -proot_old -e "select 'hello world'"
mysql: [Warning] Using a password on the command line interface can be insecure.
+-------------+
| hello world |
+-------------+
| hello world |
+-------------+
root@ytt-ubuntu:/home/ytt# mysql -h ytt-ubuntu -u ytt -proot_new -e "select 'hello world'"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'ytt'@'ytt-ubuntu' (using password: YES)复制
还有一点需要注意的细节,如果不带 retain current password 子句,并且更改新密码为空串,那么主备密码则会统一更改为空串。下面例子中数据库拒绝之前的备密码连接:
mysql:(none)>alter user ytt identified by '';
Query OK, 0 rows affected (0.80 sec)
root@ytt-ubuntu:/home/ytt# mysql -h ytt-ubuntu -u ytt -proot_old -e "select 'hello world'"
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'ytt'@'ytt-ubuntu' (using password: YES)
root@ytt-ubuntu:/home/ytt# mysql -h ytt-ubuntu -u ytt -e "select 'hello world'"
+-------------+
| hello world |
+-------------+
| hello world |
+-------------+复制
新密码为空,不允许使用备用密码。
mysql:(none)>alter user ytt identified by '' retain current password;
ERROR 3895 (HY000): Current password can not be retained for user 'ytt'@'%' because new password is empty.复制
使用双密码策略时,不能更改用户的认证插件。
mysql:(none)>alter user ytt identified with sha256_password by 'root_new' retain current password;
ERROR 3894 (HY000): Current password can not be retained for user 'ytt'@'%' because authentication plugin is being changed.复制
第二,随机密码生成:
以往旧版本有生成随机密码的需求,在 MySQL 端无法直接设定,除非封装用户密码设定逻辑,并且在代码里实现随机密码生成。比如用存储过程,脚本等等。
MySQL 8.0 直接可以设置用户随机密码
mysql:(none)>create user ytt_new identified by random password;
+---------+------+----------------------+-------------+
| user | host | generated password | auth_factor |
+---------+------+----------------------+-------------+
| ytt_new | % | >h<m3[bnigz%*f/SnLfp | 1 |
+---------+------+----------------------+-------------+
1 row in set (0.02 sec)复制
也可以用 set password 子句来设置随机密码
mysql:(none)>set password for ytt_new to random;
+---------+------+----------------------+-------------+
| user | host | generated password | auth_factor |
+---------+------+----------------------+-------------+
| ytt_new | % | 5wzZ+0[27cd_CW/]<ua, | 1 |
+---------+------+----------------------+-------------+
1 row in set (0.04 sec)复制
另外,随机密码的长度由参数 generated_random_password_length 调整,默认为 20 个。
总结
双密码策略能让应用和DBA沟通起来更加协调;随机密码设置能让数据库系统更加安全。
新特性解读 | 来聊聊 MySQL8.0 的 json 模式校验
文章至此。
以下是个人微信公众号,欢迎关注: