1. 查看权限
- -- 如果host值不是%, 就要加上host值,下面查看bkpuser用户权限(6个权限, 限本地连接)
- SHOW GRANTS FOR bkpuser@localhost;

- -- 如果host值是%, 就只要输入用户名,下面查看z1用户权限(显示所有权限,不限ip连接)
- SHOW GRANTS FOR z1;

2. 更改权限
可以进行权限的新增和回收,使用grant来新增,使用revoke来回收。在前面52章节中也有介绍revoke的使用。 也可以直接对user,db,tables_priv,columns_prive 四个权限表进行更新,这个在上篇中也有讲到。
- -- 例1:
- CREATE USER 'z2'@'localhost' IDENTIFIED BY '123456';
- -- 赋给z2@localhost用户在所有数据库上的所有表的select 权限
- GRANT SELECT ON *.* TO 'z2'@'localhost'
- -- 查看权限
- SHOW GRANTS FOR z2@localhost;

- --例2: 使用grant 来新增权限,新增一个insert权限,并和已有的select 权限进行合并
- GRANT SELECT,INSERT ON *.* TO 'z2'@'localhost'

- --例3: 使用revoke语句可以回收已经赋予的权限
- -- 语法如下:
- REVOKE
- priv_type [(column_list)]
- [, priv_type [(column_list)]] ...
- ON [object_type] priv_level
- FROM user [, user] ...
- -- 回收insert权限,注意像usage登录权限是不能回收的。因为revoke并不能删除用户
- REVOKE INSERT ON *.* FROM 'z2'@'localhost';

更多revoke语法的了解,请参考官方文档:https://dev.mysql.com/doc/refman/5.7/en/revoke.html
3. 修改密码
修改密码有四种方法, (1)是使用mysqladmin命令,(2)是执行set password,(3)是使用grant。(4)是修改 user表。
- -- (1)使用mysqladmin来修改,如果host是%,则-h 的host为空
- [root@hsr ~]# mysqladmin -u z1 -h '' password '123456' -p
- Enter password:
- mysqladmin: [Warning] Using a password on the command line interface can be insecure.
- Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
翻译是:在命令行界面上使用密码是不安全的。警告:由于密码将以纯文本形式发送到服务器,因此使用ssl连接以确保密码安全。
修改密码后验证:使用客户端sqlyog来连接时,原有密码654321连接错误,改成123456 新密码连接成功。
- -- (2) set password
- SET PASSWORD FOR 'z1'@'%'=PASSWORD('654321')
- Warning Code : 1287
- 'SET PASSWORD FOR <user> = PASSWORD('<plaintext_password>')' is deprecated and will be removed in a future release.
Please use SET PASSWORD FOR <user> = '<plaintext_password>' instead
翻译是:1 个警告,该方法已弃用,将在将来的版本中删除
修改密码后验证:使用客户端sqlyog来连接时,密码改成654321连接成功
- -- (3) 使用grant来修改密码
- GRANT USAGE ON *.* TO 'z1'@'%' IDENTIFIED BY '123456'
- Using GRANT statement to modify existing user's properties other than privileges is deprecated and will be removed in future release.
Use ALTER USER statement for this operation.
翻译是:使用GRANT语句修改现有用户的属性而不是特权是不赞成的,并将在以后的版本中删除。对这个操作使用ALTER USER语句。
修改密码后验证:使用客户端sqlyog来连接时,密码改成123456连接成功。
- --(4) 修改 user表
- UPDATE mysql.`user` SET authentication_string=PASSWORD('654321') WHERE `Host`='%' AND `User`='z1'
- Warning Code : 1681
- 'PASSWORD' is deprecated and will be removed in a future release.
- -- 记住要刷新才生效。
- FLUSH PRIVILEGES
修改密码后验证:使用客户端sqlyog来连接时,密码改成654321连接成功。
修改密码最后总结:2,4 案例在未来版本中会去掉,3案例在未来版本中使用alter user。 以后推荐使用mysqladmin和alter user 来修改密码。
4. 删除账号
要彻底删除账号,同样也有两种方法:(1) drop user命令 (2) 修改权限表。
- --例如删除z2用户
- DROP USER 'z2'@'localhost'
- -- 查看权限
- SHOW GRANTS FOR z2@localhost;
查看权限显示:There is no such grant defined for user 'z2' on host 'localhost'。再查看user表也没有了z2用户的信息了。第二种方法删除,直接删除user表的记录就行了,不在演示。