MySQL授权命令grant的使用方法

本文实例,运行于 MySQL 5.0 及以上版本。

MySQL 赋予用户权限命令的简单格式可概括为:

grant 权限 on 数据库对象 to 用户

一、grant 普通数据用户,查询、插入、更新、删除 数据库中所有表数据的权利。

grant select on testdb.* to [email protected]‘%‘
grant insert on testdb.* to [email protected]‘%‘
grant update on testdb.* to [email protected]‘%‘
grant delete on testdb.* to [email protected]‘%‘

或者,用一条 MySQL 命令来替代:

grant select, insert, update, delete on testdb.* to [email protected]‘%‘

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

grant 创建、修改、删除 MySQL 数据表结构权限。

grant create on testdb.* to [email protected]‘192.168.0.%‘;
grant alter on testdb.* to [email protected]‘192.168.0.%‘;
grant drop on testdb.* to [email protected]‘192.168.0.%‘;

grant 操作 MySQL 外键权限。

grant references on testdb.* to [email protected]‘192.168.0.%‘;

grant 操作 MySQL 临时表权限。

grant create temporary tables on testdb.* to [email protected]‘192.168.0.%‘;

grant 操作 MySQL 索引权限。

grant index on testdb.* to [email protected]‘192.168.0.%‘;

grant 操作 MySQL 视图、查看视图源代码 权限。

grant create view on testdb.* to [email protected]‘192.168.0.%‘;
grant show view on testdb.* to [email protected]‘192.168.0.%‘;

grant 操作 MySQL 存储过程、函数 权限。

grant create routine on testdb.* to [email protected]‘192.168.0.%‘; -- now, can show procedure status
grant alter routine on testdb.* to [email protected]‘192.168.0.%‘; -- now, you can drop a procedure
grant execute on testdb.* to [email protected]‘192.168.0.%‘;

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

grant all privileges on testdb to [email protected]‘localhost‘

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

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

grant all on *.* to [email protected]‘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;

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

grant select(user_id,username) on smp.users to [email protected]‘%‘ identified by ‘123345‘;
grant select on smp.mo_sms to [email protected]‘%‘ identified by ‘123345‘;

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

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

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

grant execute on procedure testdb.pr_add to ‘dba‘@‘localhost‘
grant execute on function testdb.fn_add 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;

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

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

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

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

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

时间: 2024-08-03 21:49:35

MySQL授权命令grant的使用方法的相关文章

2.MySQL授权命令grant的使用方法

本文实例,运行于 MySQL 5.0 及以上版本. MySQL 赋予用户权限命令的简单格式可概括为: grant 权限 on 数据库对象 to 用户 一.grant 普通数据用户,查询.插入.更新.删除 数据库中所有表数据的权利. grant select on testdb.* to [email protected]'%'grant insert on testdb.* to [email protected]'%'grant update on testdb.* to [email pro

mysql 授权命令

MySQL清空数据库的操作:truncate table tablename; MySQL 赋予用户权限命令的简单格式可概括为:grant 权限 on 数据库对象 to 用户  一.grant 普通数据用户,查询.插入.更新.删除 数据库中所有表数据的权利. 1 grant select on testdb.* to [email protected]'%' grant insert on testdb.* to [email protected]'%' grant update on test

mysql授权(grant)

转自:http://chenling1018.blog.163.com/blog/static/14802542010320112355598/ MySQL的权限系统围绕着两个概念: 认证->确定用户是否允许连接数据库服务器 授权->确定用户是否拥有足够的权限执行查询请求等. 如果认证不成功的话,哪么授权肯定是无法进行的. revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 表 GRANT和REVOKE管理的权限 权限 描述 ALL PRIVILEGES

mysql 创建用户命令-grant

我们在使用mysql的过程中,经常需要对用户授权(添加,修改,删除),在mysql当中有三种方式实现 分别是 INSERT USER表的方法.CREATE USER的方法.GRANT的方法.今天主要看一下grant方法是如何实现的 分两种情况,第一种先使用create user命令创建用户,然后grant授权:第二种直接使用grant创建并授权:我们先看第一种如何实现 查看用户权限 show grants for 你的用户 比如:show grants for [email protected]

mysql 的常用命令及常见问题解决方法

运行sql C:\Users\Martin>mysql -uroot -pyang cdm_db <d:/cdm_db.sql 运行sql mysql>source /tmp/terminal.sql; mysql忘记密码: mysqladmin -uroot flush-privileges password "newpassword" mysql的select into file命令 SELECT a,b,a+b INTO OUTFILE '/tmp/result

命令行模式下 MYSQL导入导出.sql文件的方法

一.MYSQL的命令行模式的设置:桌面->我的电脑->属性->环境变量->新建->PATH=“:path\mysql\bin;”其中path为MYSQL的安装路径.二.简单的介绍一下命令行进入MYSQL的方法:1.C:\>mysql -h hostname -u username -p  .C:\>mysql -h localhost -u root-p按ENTER键,等待然后输入密码.这里hostname为服务器的名称,如localhost,username为M

解决MySQL不允许从远程访问的方法

开启 MySQL 的远程登陆帐号有两大步: 1.确定服务器上的防火墙没有阻止 3306 端口. MySQL 默认的端口是 3306 ,需要确定防火墙没有阻止 3306 端口,否则远程是无法通过 3306 端口连接到 MySQL 的. 如果您在安装 MySQL 时指定了其他端口,请在防火墙中开启您指定的 MySQL 使用的端口号. 如果不知道怎样设置您的服务器上的防火墙,请向您的服务器管理员咨询. 2.增加允许远程连接 MySQL 用户并授权. 1)首先以 root 帐户登陆 MySQL 在 Wi

MongoDB与Mysql常用命令解释

原文 本文旨在介绍MongoDB,Mysql的常用命令:将MongoDB 和传统的关系型数据库的常用命令对照起来学习,更加便于记忆和理解. mongodb与mysql命令对比 MongoDB是由数据库(database/repository).集合(collection).文档对象(document)三个层次组成.MongoDB中集合对应关系型数据库里的表,但是集合中没有列.行和关系的概念,这体现了模式自由的特点. 传统的关系数据库一般由数据库(database).表(table).记录(rec

MYSQL常用命令集合(转载)

文章出处:http://www.cnblogs.com/q1ng/p/4474501.html 1.导出整个数据库mysqldump -u 用户名 -p --default-character-set=latin1 数据库名 > 导出的文件名(数据库默认编码是latin1)mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql2.导出一个表mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名mysqldump -u wcnc -p