Mysql权限管理grant命令使笔记
时间:2024-01-18 02:08:28
MySQL 赋予用户权限命令的简单格式可概括为:
grant 权限 on 数据库对象 to 用户 [identified by '密码']
最常用的,弄主从同步的时候,给从库的slave用户设置拥有所有权限,权限all
仅允许其从192.168.0.2登录,并限定使用密码 funsion (密码要用 单/双引号 括起来)
grant all on *.* to slave@192.168.0.2 identified by 'funsion';
执行完毕后,记得用 FLUSH PRIVILEGES; 刷新一下权限
一、grant 普通数据用户,查询、插入、更新、删除 数据库中所有表数据的权利。
grant select, insert, update, delete on testdb.* to common_user@'%'
二、grant 数据库开发人员,创建表、索引、视图、存储过程、函数.....等权限。
grant create, alter, drop on testdb.* to developer@'192.168.0.%';
grant 操作 MySQL 外键权限。
grant references on testdb.* to developer@'192.168.0.%';
grant 操作 MySQL 索引权限。
grant index on testdb.* to developer@'192.168.0.%';
给所有IP开放权限:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
grant 操作 MySQL 临时表权限。
grant create temporary tables on testdb.* to developer@'192.168.0.%';
grant 操作 MySQL 视图、查看视图源代码 权限。
grant create view on testdb.* to developer@'192.168.0.%';
grant show view on testdb.* to developer@'192.168.0.%';
grant 操作 MySQL 存储过程、函数 权限。
grant create routine on testdb.* to developer@'192.168.0.%'; -- now, can show procedure status
grant alter routine on testdb.* to developer@'192.168.0.%'; -- now, you can drop a procedure
grant execute on testdb.* to developer@'192.168.0.%';
执行完毕后,记得用 FLUSH PRIVILEGES; 刷新一下权限
三、grant 普通 DBA 管理某个 MySQL 数据库的权限。
grant all privileges on testdb to dba@'localhost'
其中,关键字 privileges 可以省略。
四、grant 高级 DBA 管理 MySQL 中所有数据库的权限。
grant all on *.* to dba@'localhost'
五、MySQL grant 权限,分别可以作用在多个层次上。
1. grant 作用在整个 MySQL 服务器上:
grant select on *.* to dba@localhost; -- dba 可以查询 MySQL 中所有数据库中的表。
grant all on *.* to dba@localhost; -- dba 可以管理 MySQL 中的所有数据库
2. grant 作用在单个数据库上:
grant select on testdb.* to dba@localhost; -- dba 可以查询 testdb 中的表。
3. grant 作用在单个数据表上:
grant select, insert, update, delete on testdb.orders to dba@localhost;
六、查看 MySQL 用户权限
查看当前用户(自己)权限:
show grants;
查看其他 MySQL 用户权限:
show grants for dba@localhost;
七、撤销已经赋予给 MySQL 用户权限的权限。
revoke 跟 grant 的语法差不多,只需要把关键字 to 换成 from 即可:
grant all on *.* to dba@localhost;
revoke all on *.* from dba@localhost;
# ************************************* 常见问题解决方案 ************************************** #
遇到 SELECT command denied to user '用户名'@'主机名' for table '表名' 这种错误,解决方法是需要把吧后面的表名授权,即是要你授权核心数据库也要。
如遇到的是SELECT command denied to user 'my'@'%' for table 'proc',是调用存储过程的时候出现,原以为只要把指定的数据库授权就行了,什么存储过程、函数等都不用再管了,谁知道也要把数据库
mysql的proc表授权
mysql授权表共有5个表:user、db、host、tables_priv和columns_priv。
授权表的内容有如下用途:
[user 表]
user表列出可以连接服务器的用户及其口令,并且它指定他们有哪种全局(超级用户)权限。在user表启用的任何权限均是全局权限,并适用于所有数据库。例如,如果你启用了DELETE权限,在这里列出的用户可以从任何表中删除记录,所以在你这样做之前要认真考虑。
[db 表]
db表列出数据库,而用户有权限访问它们。在这里指定的权限适用于一个数据库中的所有表。
[host 表]
host表与db表结合使用在一个较好层次上控制特定主机对数据库的访问权限,这可能比单独使用db好些。这个表不受GRANT和REVOKE语句的影响,所以,你可能发觉你根本不是用它。
[tables_priv 表]
tables_priv表指定表级权限,在这里指定的一个权限适用于一个表的所有列。
[columns_priv 表]
columns_priv表指定列级权限。这里指定的权限适用于一个表的特定列。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决MySQL Varchar 类型尾部空格的问题
Django框架静态文件使用/中间件/禁用ip功能实例详解
![](https://img.aspxhome.com/file/2023/6/71346_0s.png)
python防止栈溢出的实例讲解
python安装sklearn模块的方法详解
![](https://img.aspxhome.com/file/2023/9/61969_0s.jpg)
Asp与JS的数组和字符串下标介绍
Python中itertools的用法详解
vue检测对象和数组的变化分析
详解php用static方法的原因
基于Django OneToOneField和ForeignKey的区别详解
Go微服务项目配置文件的定义和读取示例详解
![](https://img.aspxhome.com/file/2023/6/89016_0s.png)
knockoutjs动态加载外部的file作为component中的template数据源的实现方法
![](https://img.aspxhome.com/file/2023/3/137063_0s.png)
如何彻底解决python NameError:name '__file__' is not defined
![](https://img.aspxhome.com/file/2023/7/124187_0s.png)
Python答题卡识别并给出分数的实现代码
![](https://img.aspxhome.com/file/2023/3/124753_0s.jpg)
使用Python3内置文档高效学习以及官方中文文档
使用Python制作微信跳一跳辅助
![](https://img.aspxhome.com/file/2023/6/121086_0s.png)
oracle 会话 死锁 执行sql 执行job的方法
python实现12306火车票查询器
![](https://img.aspxhome.com/file/2023/8/126968_0s.jpg)
Python 计算机视觉编程进阶之OpenCV 图像锐化及边缘检测
![](https://img.aspxhome.com/file/2023/8/105308_0s.png)
PyQt5每天必学之带有标签的复选框
![](https://img.aspxhome.com/file/2023/4/87974_0s.png)