mysql 授权与权力收回_mysql常用命令,授权和撤销操作

**##常见mysql常用命令,授权和撤销操作,赶紧收藏**

**--记得点赞关注哟,谢谢啦--**

**一.用户授权命令:**

grant 权限 to 数据库名.数据库表 to 用户

权限:insert 增,delete 删, select 查, update 改, create 创建, drop 删除........

```sql

例:grant select on user to xhk

表示将user数据库的查询权限授给xhk用户

```

用户后面可以加 @'ip地址' identified by '密码'

```sql

例如:grant all on *.* to xhk@'127.0.0.1' identify by '123456'

```

表示将所有数据库的所有权限授权给xhk用户,允许xhk用户在127.0.0.1这个IP进行远程登陆,并设置用户的密码为123456

```sql

刷新权限:flush privileges

//授权完之后要刷新权限才可以生效

```

**二.授权限一般分为另种情况下:一种是普通用户,另一种是数据库开发人员、运维人员**

普通用户一般就是增删查改:

```sql

grant select on test.* to xhk@'%'

//表示将test数据库下所有表的查询权限授给xhK用户

```

```sql

grant insert on test.user to xhk@'%'

//表示将test数据库下的user表的查询权限授给xhK用户

```

```sql

grant update on test.* to xhk@'%';

grant delete on test.* to xhk@'%' ;

```

数据库开发人员创建表、索引、视图、存储过程、函数等权限:

```sql

grant create on test.* to xhk@'192.168.0.%';

grant alter on test.* to xhk@'192.168.0.%';

grant drop on test.* to xhk@'192.168.0.%';

//192.168.0.%表示允许远程连接的 IP 地址,如果想不限制链接的 IP 则设置为“%”即可

```

**三、grant 普通 DBA 管理某个 MySQL 数据库的权限**

```sql

grant all privileges on testdb to dba@'localhost'

```

其中,关键字 “privileges” 可以省略。

四、grant 高级 DBA 管理 MySQL 中所有数据库的权限:

```sql

grant all on *.* to dba@'localhost'

```

五、MySQL grant 权限,分别可以作用在多个层次上

1. grant 作用在整个 MySQL 服务器上:

```sql

grant select on *.* to dba@localhost; -- dba 可以查询 MySQL 中所有数据库中的表。

```

```sql

grant all on *.* to dba@localhost; -- dba 可以管理 MySQL 中的所有数据库

```

2. grant 作用在单个数据库上:

```sql

grant select on testdb.* to dba@localhost; -- dba 可以查询 testdb 中的表。

```

3. grant 作用在单个数据表上:

```sql

grant select, insert, update, delete on testdb.orders to dba@localhost;

```

这里在给一个用户授权多张表时,可以多次执行以上语句。例如:

```sql

grant select(user_id,username) on smp.users to mo_user@'%' identified by '123345';

```

```sql

grant select on smp.mo_sms to mo_user@'%' identified by '123345';

```

4. grant 作用在表中的列上:

```sql

grant select(id, se, rank) on testdb.apache_log to dba@localhost;

```

5. grant 作用在存储过程、函数上:

```sql

grant execute on procedure testdb.pr_add to 'dba'@'localhost' ;

grant execute on function testdb.fn_add to 'dba'@'localhost' ;

```

六、查看 MySQL 用户权限

查看当前用户(自己)权限:

```sql

show grants;

```

查看其他 MySQL 用户权限:

```sql

show grants for dba@localhost;

```

七、撤销已经赋予给 MySQL 用户权限的权限。

revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:

```sql

grant all on *.* to dba@localhost;

```

```sql

revoke all on *.* from dba@localhost;

```

八、MySQL grant、revoke 用户权限注意事项

1. grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。

2. 如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 “grant option“

```sql

grant select on testdb.* to dba@localhost with grant option;

```

```sql

grant select on testdb.* to dba@localhost with grant option;

```

这个特性一般用不到。实际中,数据库权限最好由 DBA 来统一管理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值