MySQL使用ALTER USER修改密码

MySQL使用ALTER USER修改密码

本文将介绍在 MySQL 中怎样使用 alter user 修改用户密码。

MySQL修改用户的密码有两种方法:

ALTER USER 

下面的语句在 mysql 5.7 版本中执行成功。实例如下:

(1)使用明文的方式修改 test 用户的密码为 123456。如下:

mysql> alter user test identified by '123456';
Query OK, 0 rows affected

(2)修改当前登录用户的密码,其中:user() 方法将返回当前用户信息。实例如下:

mysql> select user();
+----------------+
user()         |
+----------------+
| test@localhost |
+----------------+
1 row in set (0.00 sec)
 
mysql> alter user user() identified by 'aaaaaa';
Query OK, 0 rows affected (0.00 sec)

(3)使密码过期

mysql> alter user test identified by '123456' password expire;
Query OK, 0 rows affecte

(4)使密码从不过期

mysql> alter user test identified by '123456' password expire never;
Query OK, 0 rows affected

(5)按默认设置过期时间

mysql> alter user test identified by '123456' password expire default;
Query OK, 0 rows affected

(6)指定密码的过期间隔,如下:

mysql> alter user test identified by '123456' password expire interval 90 day;
Query OK, 0 rows affected

在 MySQL 文档里,推荐使用 ALTER USER 修改用户密码。ALTER USER 官网参考手册:https://dev.mysql.com/doc/refman/5.7/en/alter-user.html

SET PASSWORD

使用 SET PASSWORD 的密码有两种:

(1)使用默认加密

mysql> set password for test='123456';
ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number

错误“ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number”(密码散列应该是一个41位的十六进制数字)意思是不能输入明文,可以使用 password('') 来生成密码。

(2)使用 PASSWORD() 函数加密

mysql> set password for test=password('123abc');
Query OK, 0 rows affected

注意:使用 PASSWORD('auth_string') 的方式已经被废弃,在以后的版本会把它移除,所以不建议使用它来修改密码。

 

我们愈是学习,愈觉得自己的贫乏。 —— 雪莱
 
COME FROM :https://www.hxstrive.com/article/728.htm#page1
posted @ 2022-04-01 11:26  seasonzone  阅读(4948)  评论(0编辑  收藏  举报