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

1次阅读

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

作者:杨涛涛

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

本文起源:原创投稿

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


引言

引言

这里来介绍下 MySQL 8.0 版本自带的新密码验证策略。

注释

咱们十分相熟这样的模式:用户想更改本人明码,须要提供原来明码或者追加手机验证码才能够,这种模式在 MySQL 数据库里始终不存在。在 MySQL 8.0 之前的版本,普通用户能够间接更改本人明码,不须要旧明码验证,也不须要知会管理员,比方用户 ytt_admin 须要更改明码,在 MySQL 5.7 下间接敲 alter user 命令即可:

root@ytt-ubuntu:~# mysql -uytt_admin -proot1234 -P5734 -h ytt-ubuntu -e "alter user ytt_admin identified by'root'"
mysql: [Warning] Using a password on the command line interface can be insecure.
这样的明码更改行为其实不是很平安,假如有上面的场景呈现:
用户 ytt_admin 登录到 MySQL 服务后,做了些日常操作,实现后遗记退出;此时刚好有一个居心叵测的用户 ytt_fake 进入 ytt_admin 的登录环境,间接敲命令 alter user 即可更改用户 ytt_admin 的明码,并且退出以后登录环境,用户 ytt_admin 本尊再次登录 MySQL,就会提醒明码谬误,不容许登录,此时用户 ytt_admin 大脑必定是懵的。
为了避免这类不安全事件的产生,MySQL 8.0 公布了一系列明码验证策略。这里介绍第一项:以后明码验证策略设置!
以后明码验证策略有两种办法来给到具体用户。
第一种,从管理员侧来设置单个用户的以后明码验证策略。

创立用户或者更改用户设置时应用子句:password require current(示意强制此用户满足以后明码验证策略)。

mysql:(none)>create user ytt_admin identified by 'root123' password require current;
Query OK, 0 rows affected (0.11 sec)

之后以用户 ytt_admin 登录 MySQL 并且更改明码,提醒须要提供旧明码才行。

root@ytt-ubuntu:/home/ytt# mysql -h ytt-ubuntu -uytt_admin -proot123
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 33
Server version: 8.0.27 MySQL Community Server - GPL

mysql:(none)>alter user ytt_admin identified by 'root';
ERROR 3892 (HY000): Current password needs to be specified in the REPLACE clause in order to change it.

接下来,alter user 跟上子句 replace 来让用户 ytt_admin 输出旧明码,胜利更改新密码。

mysql:(none)>alter user ytt_admin identified by 'root' replace 'root123';
Query OK, 0 rows affected (0.00 sec)

如果有的场景下须要放弃 MySQL 旧版本的明码更改行为,管理员侧能够用子句:password require current optional 敞开新个性。

-- (optional 关键词可用 default 代替,参考全局明码验证参数设置)
mysql:(none)>alter user ytt_admin password require current optional;
Query OK, 0 rows affected (0.04 sec)

来再次验证下用户 ytt_admin 更改明码的行为:又变更为不平安的 MySQL 旧版本平安行为。

mysql:(none)>alter user ytt_admin identified by 'root';
Query OK, 0 rows affected (0.01 sec)
第二种,设置全局参数,来强制所有用户应用以后明码验证策略。

MySQL 8.0 新版本内置的参数 password_require_current 定义一个全局明码策略,默认敞开。开启这个选项时,要求用户更改明码时必须提供旧明码。

开启全局参数:

mysql:(none)>set persist password_require_current=on;
Query OK, 0 rows affected (0.00 sec)

创立另外一个新用户 ytt_usage:

mysql:(none)>create user ytt_usage identified by 'root123';
Query OK, 0 rows affected (0.00 sec)

以用户 ytt_usage 登录 MySQL 更改本人明码:间接回绝更改,须要提供旧明码。

root@ytt-ubuntu:~# mysql -uytt_usage -proot123 -h ytt-ubuntu
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 37
Server version: 8.0.27 MySQL Community Server - GPL

...

mysql:(none)>alter user ytt_usage identified by 'root';
ERROR 3892 (HY000): Current password needs to be specified in the REPLACE clause in order to change it.
mysql:(none)>

replace 子句提供旧明码再次胜利更改新密码:

mysql:(none)>alter user ytt_usage identified by 'root' replace 'root123';
Query OK, 0 rows affected (0.02 sec)

这里有一个须要留神的点:尽管全局参数开启,然而 alter user 命令优先级更高,能够间接笼罩全局参数设置。上面是全局参数开启的环境下,用 alter user 命令来敞开用户 ytt_usage 的以后明码验证策略。

mysql:(none)>alter user ytt_usage password require current optional;
Query OK, 0 rows affected (0.11 sec)

接下来用户 ytt_usage 又复原为 MySQL 旧版本的平安行为:

mysql:(none)>alter user ytt_usage identified by 'rootnew';
Query OK, 0 rows affected (0.11 sec)

还有另外一个子句:password require current default,具体行为由全局参数 password_require_current 的设置决定,全局参数敞开,这个子句复原 MySQL 旧版本平安行为;全局参数开启,这个子句应用 MySQL 新版本平安行为。

mysql:(none)>alter user ytt_usage password require current default;
Query OK, 0 rows affected (0.09 sec)

总结:

本文介绍的以后明码验证策略,使得 MySQL 朝着更加平安的方向致力。

正文完
 0