mysql 管理员常用命令

1、创建用户

create user admin@localhost identified by 'password';

2、赋权

grant privileges ON database.table TO 'username'[@'host'] [with grant 
option]
grant命令说明:
  • priveleges (权限列表),可以是all,表⽰所有权限,也可以是select、update等权限,多个权限之间⽤逗号分开。
  • ON ⽤来指定权限针对哪些库和表,格式为数据库.表名 ,点号前⾯⽤来指定数据库名,点号后⾯⽤来指定表名,*.* 表⽰所有数据库所有表。
  • TO 表⽰将权限赋予某个⽤户, 格式为username@host,@前⾯为⽤户名,@后⾯接限制的主机,可以是IP、IP段、域名以及%,%表⽰任何地⽅。
  • WITH GRANT OPTION 这个选项表⽰该⽤户可以将⾃⼰拥有的权限授权给别⼈。

备注:可以使⽤GRANT重复给⽤户添加权限,权限叠加,⽐如你先给⽤户添加⼀个select权限,然后又给⽤户添加⼀个insert权限,那么该⽤户就同时拥有了select和insert权限。

示例:

grant all on *.* to 'test1'@‘%’;
说明:给test1授权可以操作所有库所有权限,相当于dba

3、修改密码

⽅式1:通过管理员修改密码
SET PASSWORD FOR '⽤户名'@'主机' = PASSWORD('密码');
⽅式2:create user ⽤户名[@主机名] [identi>ied by '密码'];
set password = password('密码');
⽅式3:通过修改mysql.user表修改密码
use mysql;
update user set authentication_string = password('321') where user ='test1' and host = '%';
flush privileges;
注意:
  通过表的⽅式修改之后,需要执⾏flush privileges;才能对⽤户⽣效。5.7中user表中的authentication_string字段表⽰密码,⽼的⼀些版本中密码字段是password。

4、查看用户列表

use mysql;
select user,host from user;

5、查看用户权限

show grants for 'username'@'hostIP'
 ## 查看当前用户权限
  show grants;

6、撤销用户权限

revoke privileges ON database.table FROM '⽤户名'[@'主机'];
可以先通过show grants命令查询⼀下⽤户对于的权限,然后使⽤revoke命令撤销⽤户
对应的权限,⽰例:
mysql> show grants for 'test1'@'localhost';
+--------------------------------------------------------------------+
| Grants for test1@localhost |
+--------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test1'@'localhost' |
| GRANT SELECT (host, user) ON `mysql`.`user` TO 'test1'@'localhost' |
+--------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> revoke select(host) on mysql.user from test1@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'test1'@'localhost';
+--------------------------------------------------------------+
| Grants for test1@localhost |
+--------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'test1'@'localhost' |
| GRANT SELECT (user) ON `mysql`.`user` TO 'test1'@'localhost' |
+--------------------------------------------------------------+
2 rows in set (0.00 sec)
上⾯我们先通过grants命令查看test1的权限,然后调⽤revoke命令撤销对mysql.user表host字段的查询权限,最后又通过grants命令查看了test1的权限,和预期结果⼀致。

7、删除用户

方法1:

 drop user test1@localhost;

方法2:

delete from user where user='⽤户名' and host='主机';
flush privileges;

 

posted @ 2023-01-29 14:33  高佳丰  阅读(69)  评论(0编辑  收藏  举报