深圳全飞鸿

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 565|回复: 0
打印 上一主题 下一主题

MySQL5.7 锁定用户

[复制链接]

800

主题

1379

帖子

7713

积分

版主

Rank: 7Rank: 7Rank: 7

积分
7713
跳转到指定楼层
楼主
发表于 2020-6-22 17:54:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
使用ALTER USER 语句锁定

  1. mysql>ALTER USER 'demo'@'localhost' ACCOUNT LOCK;
  2. Query OK, 0 rows affected (0.00 sec)
复制代码


使用被锁账号登录会报ERROR 3118错误:

  1. $ mysql -udemo -p
  2. Enter password:
  3. ERROR 3118 (HY000): Access denied for user 'demo'@'localhost'. Account is locked.
复制代码


解锁账号

  1. mysql>ALTER USER 'demo'@'localhost' ACCOUNT UNLOCK;
  2. Query OK, 0 rows affected (0.00 sec)
复制代码


查看用户是否锁定
  1. select user,host,account_locked from mysql.user;
复制代码



5.7加入了LOCK ACCOUNT功能和ORACLE一样了,
但是5.6貌似没有,但是可以代替用如下方法设置密码过期。

The mysql.usertable now has a password_expiredcolumn. Its default value is 'N', but
can be set to 'Y'with the new ALTER USER statement. After an account's password has been
expired, all operations performed in subsequent connections to the server using the account result
in an error until the user issues a SET PASSWORDstatement to establish a new account password.
For more information, see Section 13.7.1.1, “ALTER USERSyntax”, and Section 6.3.6, “Password
Expiration and Sandbox Mode”.


就是下面这个语法
  1. mysql> alter user mytest1@'%' password expire;
复制代码


如果要恢复
  1. set password for mytest1@'%' = '*******';
复制代码


注意:只要重置一下过期用户的密码就行(注意不能使用 update mysql.user方法来重置密码)

其实就是MYSQL.USER下面的字段 password_expired 标示了是否过期。



  1. mysql> select user,host,password,password_expired from mysql.user where user = 'root';
  2. +------+---------------------+-------------------------------------------+------------------+
  3. | user | host                | password                                  | password_expired |
  4. +------+---------------------+-------------------------------------------+------------------+
  5. | root | localhost           | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | N                |
  6. | root | all-middle-mysql-10 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | Y                |
  7. | root | 127.0.0.1           | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | Y                |
  8. | root | ::1                 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B | Y                |
  9. +------+---------------------+-------------------------------------------+------------------+
  10. 4 rows in set (0.00 sec)
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|nagomes  

GMT+8, 2025-5-8 21:07 , Processed in 0.053861 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表