Please enable Javascript to view the contents

密码过期

 ·  ☕ 3 分钟

5.7以后的某些mysql,需要改变策略,不然密码会过期

1
2
3

alter user 'root'@'localhost' password expire never; 

如果要恢复到默认的策略,就是360天过期:

1
2

alter user 'root'@'localhost' password expire default; 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
MySQL [(none)]> use mysql;
Database changed

MySQL [mysql]> update user set password_expired='N' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MySQL [mysql]> flush privileges;
Query OK, 0 rows affected (0.01 sec)

MySQL [mysql]> quit;
Bye

针对所有用户

1
2
3

[mysqld]  default_password_lifetime = 0

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
mysql -uroot -proot

MySQL5.7 mysql.user表没有password字段改 authentication_string;

一. 创建用户:
命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password';

例子: CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456';

        CREATE USER 'dog2'@'localhost' IDENTIFIED BY '';

PS:username - 你将创建的用户名,

host - 指定该用户在哪个主机上可以登陆,此处的"localhost",是指该用户只能在本地登录,不能在另外一台机器上远程登录,如果想远程登录的话,将"localhost"改为"%",表示在任何一台电脑上都可以登录;也可以指定某台机器可以远程登录;

password - 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器。

二.授权:
命令:GRANT privileges ON databasename.tablename TO 'username'@'host'

PS: privileges - 用户的操作权限,如SELECT , INSERT , UPDATE 等(详细列表见该文最后面).如果要授予所的权限则使用ALL.;databasename - 数据库名,tablename-表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示, 如*.*.

例子: GRANT SELECT, INSERT ON mq.* TO 'dog'@'localhost';

三.创建用户同时授权
mysql> grant all privileges on mq.* to test@localhost identified by '1234';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

PS:必须执行flush privileges; 

否则登录时提示:ERROR 1045 (28000): Access denied for user 'user'@'localhost' (using password: YES ) 

 

四.设置与更改用户密码
命令:SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');

例子: SET PASSWORD FOR 'dog2'@'localhost' = PASSWORD("dog");

五.撤销用户权限
      命令: REVOKE privilege ON databasename.tablename FROM 'username'@'host';

     说明: privilege, databasename, tablename - 同授权部分.

      例子: REVOKE SELECT ON mq.* FROM 'dog2'@'localhost';

PS: 假如你在给用户'dog'@'localhost''授权的时候是这样的(或类似的):GRANT SELECT ON test.user TO 'dog'@'localhost', 则在使用REVOKE SELECT ON *.* FROM 'dog'@'localhost';命令并不能撤销该用户对test数据库中user表的SELECT 操作.相反,如果授权使用的是GRANT SELECT ON *.* TO 'dog'@'localhost';则REVOKE SELECT ON test.user FROM 'dog'@'localhost';命令也不能撤销该用户对test数据库中user表的Select 权限.

      具体信息可以用命令SHOW GRANTS FOR 'dog'@'localhost'; 查看.

六.删除用户
      命令: DROP USER 'username'@'host';

七.查看用户的授权
mysql> show grants for dog@localhost;
+---------------------------------------------+
| Grants for dog@localhost |
+---------------------------------------------+
| GRANT USAGE ON *.* TO 'dog'@'localhost' |
| GRANT INSERT ON `mq`.* TO 'dog'@'localhost' |
+---------------------------------------------+
2 rows in set (0.00 sec)

PS:GRANT USAGE:mysql usage权限就是空权限,默认create user的权限,只能连库,啥也不能干