关于mysql:新特性解读-MySQL-80-新密码策略下

10次阅读

共计 4142 个字符,预计需要花费 11 分钟才能阅读完成。

作者:杨涛涛

资深数据库专家,专研 MySQL 十余年。善于 MySQL、PostgreSQL、MongoDB 等开源数据库相干的备份复原、SQL 调优、监控运维、高可用架构设计等。目前任职于爱可生,为各大运营商及银行金融企业提供 MySQL 相干技术支持、MySQL 相干课程培训等工作。

本文起源:原创投稿

* 爱可生开源社区出品,原创内容未经受权不得随便应用,转载请分割小编并注明起源。


明天咱们来持续介绍 MySQL 8.0 的新密码策略,别离为双明码策略和内置随机明码生成。

第一,双明码策略:

首先来解释下什么是双明码策略?双明码策略就是在日常运维中,须要定期更改指定用户明码,同时又须要旧明码临时保留肯定时长的一种策略。其作用是提早利用与数据库之间的用户新旧明码对接工夫,进而平滑利用的操作感知。能够在如下场景中应用:

在 MySQL 数据库里咱们部署最多也是最成熟的架构:一主多从。比如说此架构做了读写拆散,主负责解决前端的写流量,读负责解决前端的读流量,为了平安起见,须要定期对利用连贯数据库的用户更改明码。有了双明码机制,对用户明码的更改在利用端能够有肯定的缓冲提早,防止业务中断危险以及开发人员的埋怨。利用端仍然能够应用旧明码来实现对数据库的检索,期待适合机会再应用管理员发来的新密码检索数据库。

双明码机制蕴含主明码与备明码,当备明码不再应用时,告知管理员抛弃备明码,此时用户的主明码即是惟一明码。

具体如何应用呢?用法如下:

管理员先创立一个新用户 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
双明码策略有以下须要留神的事项:
  1. 如果用户自身曾经有双明码策略,再次更改新密码时没有带 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 |
    +-------------+
  1. 新密码为空,不容许应用备用明码。

    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.
  1. 应用双明码策略时,不能更改用户的认证插件。

    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 沟通起来更加协调;随机明码设置能让数据库系统更加平安。

正文完
 0