【转】MySQL GRANT REVOKE用法

原文地址:http://chenling1018.blog.163.com/blog/static/14802542010320112355598

MySQL的权限系统围绕着两个概念:

认证->确定用户是否允许连接数据库服务器

授权->确定用户是否拥有足够的权限执行查询请求等。

如果认证不成功的话,哪么授权肯定是无法进行的。

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

表 GRANT和REVOKE管理的权限


权限


描述


ALL PRIVILEGES


影响除WITH GRANT OPTION之外的所有权限


ALTER


影响ALTER TABLE命令的使用


ALTER ROUTINE


影响创建存储例程的能力


CREATE


影响CREATE TABLE命令的使用


CREATE ROUTINE


影响更改和弃用存储例程的能力


CREATE TEMPORARY TABLES


影响CREATE TEMPORARY TABLE命令的使用


CREATE USER


影响创建、弃用;重命名和撤销用户权限的能力


CREATE VIEW


影响CREATE VIEW命令的使用


DELETE


影响DELETE命令的使用


DROP


影响DROP TABLE命令的使用


EXECUTE


影响用户运行存储过程的能力


EVENT


影响执行事件的能力(从MySQL 5.1.6开始)


FILE


影响SELECT INTO OUTFILE和LOAD DATA INFILE的使用


GRANT OPTION


影响用户委派权限的能力


INDEX


影响CREATE INDEX和DROP INDEX命令的使用


INSERT


影响INSERT命令的使用


LOCK TABLES


影响LOCK TABLES命令的使用


PROCESS


影响SHOW PROCESSLIST命令的使用


REFERENCES


未来MySQL特性的占位符


RELOAD


影响FLUSH命令集的使用


REPLICATION CLIENT


影响用户查询从服务器和主服务器位置的能力

(续)


权限


描述


REPLICATION SLAVE


复制从服务器所需的权限


SELECT


影响SELECT命令的使用


SHOW DATABASES


影响SHOW DATABASES命令的使用


SHOW VIEW


影响SHOW CREATE VIEW命令的使用


SHUTDOWN


影响SHUTDOWN命令的使用


SUPER


影响管理员级命令的使用,如CHANGE、MASTER、KILL thread、mysqladmin debug、PURGE MASTER LOGS和SET GLOBAL


TRIGGER


影响执行触发器的能力(从MySQL5.1.6开始)


UPDATE


影响UPDATE命令的使用


USAGE


只连接,不授予权限

1>.改表法。

你的帐号不允许从远程登陆MySql服务器,只能在localhost。

解决办法:

在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改称"%"

(1). mysql -u root -pvmwaremysql>use mysql;

(2). mysql>update user set host = ‘%‘ where user = ‘root‘;

(3). mysql>select host, user from user;

2>. 授权法。例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话。

(1). GRANT ALL PRIVILEGES ON *.* TO ‘myuser‘@‘%‘ IDENTIFIED BY ‘mypassword‘ WITH GRANT OPTION;

(2). FLUSH PRIVILEGES;

如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器,并使用mypassword作为密码

(1). GRANT ALL PRIVILEGES ON *.* TO ‘myuser‘@‘192.168.1.3‘ IDENTIFIED BY ‘mypassword‘ WITH GRANT OPTION;

(2). FLUSH PRIVILEGES;

如果你想允许用户myuser从ip为192.168.1.6的主机连接到mysql服务器的dk数据库,并使用mypassword作为密码

(1). GRANT ALL PRIVILEGES ON dk.* TO ‘myuser‘@‘192.168.1.3‘ IDENTIFIED BY ‘mypassword‘ WITH GRANT OPTION;

(3). FLUSH PRIVILEGES;

注意:授权后必须FLUSH PRIVILEGES;否则无法立即生效。

另外一种方法:

3>.在安装mysql的机器上运行:

1、d:\mysql\bin\>mysql -h localhost -u root

//这样应该可以进入MySQL服务器

2、mysql>GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ WITH GRANT OPTION

//赋予任何主机访问数据的权限

3、mysql>FLUSH PRIVILEGES

//修改生效

4、mysql>EXIT

//退出MySQL服务器

这样就可以在其它任何的主机上以root身份登录啦!

4>.查看 MySQL 用户权限

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

show grants;

查看其他 MySQL 用户权限:

show grants for [email protected];

5>.撤销已经赋予给 MySQL 用户权限的权限。

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

grant all on *.* to [email protected];

revoke all on *.* from [email protected];

6>.MySQL grant、revoke 用户权限注意事项

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

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

grant select on testdb.* to [email protected] with grant option;

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

7>.用户和权限管理命令:

create user :用于创建新的用户账户(从5.0版本开始有这个命令),在创建这个用户的时候不分配任何权限,

需要在创建之后通过grant命令来给改用户分配相应的权限。

eg:create user [email protected] identified by ‘123456‘;

grant select on mydb.* to [email protected];

drop user:删除一个用户账户(注意在4.1.1版本之前只能删除没有任何权限的账户,5.0.2之后可以删除任何账户)

eg:drop user guest;

rename user:可以实现重命名一个用户账号。

grant:用于管理访问权限,也就是给用户账号授权。当然它同样可以创建一个新的用户账户。

eg:grant select, insert, update, delete on new_db.* to [email protected]‘%‘ identified by ‘88888888‘;

grant 权限 on 数据库.表 to 用户 @ 访问方式 identified by 密码

grant select on mydb.* to [email protected] identified by ‘123456‘;

BTW:如果需要一个空密码或者无密码的账户,必须先用Create User命令,然后通过

grant来分配权限。如果如下操作:

grant all privileges on mydb.* to [email protected]‘%‘ ;而在数据库user表中没有先创建visitor

用户,则会发生1133错误"Can‘t find any matching row in the user table"。

注:grant只能创有密码的账户。

revoke:删除一个账户,具体查看MySQL的文档。

8>.mysql中可以给你一个用户授予如select,insert,update,delete等其中的一个或者多个权限,主要使用grant命令,用法格式为:

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]’%’

9>.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.%’;

10>.grant 普通 DBA 管理某个 MySQL 数据库的权限。

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

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

11>.grant 高级 DBA 管理 MySQL 中所有数据库的权限。

grant all on *.* to [email protected]’localhost’

12>.MySQL grant 权限,分别可以作用在多个层次上。

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

grant select on *.* to [email protected]; -- dba 可以查询 MySQL 中所有数据库中的表。

grant all on *.* to [email protected]; -- dba 可以管理 MySQL 中的所有数据库

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

grant select on testdb.* to [email protected]; -- dba 可以查询 testdb 中的表。

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

grant select, insert, update, delete on testdb.orders to [email protected];

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

grant select(id, se, rank) on testdb.apache_log to [email protected];

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

grant execute on procedure testdb.pr_add to ’dba’@’localhost’

grant execute on function testdb.fn_add to ’dba’@’localhost’

注意:修改完权限以后一定要刷新服务,或者重启服务,刷新服务用:FLUSH PRIVILEGES。

时间: 2024-07-28 21:52:31

【转】MySQL GRANT REVOKE用法的相关文章

BUG:mysql 5.6 Invalid use of GRANT/REVOKE command breaks replication

bug描述:当使用revoke,grant命令收回,授予权限出错时(语法错误除外),会导致主从同步异常终止.如: mysql> revoke select on *.* from aaa; ERROR 1141 (42000): There is no such grant defined for user 'aaa' on host '%'     该用户没有该权限却执行了回收该权限 mysql> GRANT FILE ON test.* TO [email protected]; ERRO

mysql grant命令详解

grant 权限 on 对象 to 用户 一.grant 普通数据用户,查询.插入.更新.删除 数据库中所有表数据的权利. grant select on testdb.* to grant insert on testdb.* to grant update on testdb.* to grant delete on testdb.* to 或者,用一条 MySQL 命令来替代: grant select, insert, update, delete on testdb.* to 二.gr

Mysql grant权限管理

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 PRIVILEGE

转: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 grant用户权限设置

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

Mysql Grant 用户权限总结(详细编)

MySQL 具体有哪些权限呢?相信好多人已经不记得了,此文章将解决你的问题,请看下图: MySQL权限图: 很明显总共27个权限:下面是具体的权限介绍: 一.权限表 mysql数据库中的3个权限表:user .db. host 权限表的存取过程是: 1)先从user表中的host. user. password这3个字段中判断连接的IP.用户名.密码是否存在表中,存在则通过身份验证: 2)通过权限验证,进行权限分配时,按照useràdbàtables_privàcolumns_priv的顺序进行

mysql grant授权

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 

Mysql grant权限详解

MySQL的 Grant命令权限分配 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]

MYSQL grant 与 修改用户密码

MYSQL 创建用户,并授权,修改密码. 创建用户并将所有权限,授权给LAW create user [email protected]: grant all privileges on *.* to [email protected] identified by "";   ----OK 创建本地用户空密码,并授予相关选择,更新等权限,且只能作用于mysql数据库. create user [email protected]; grant select,update,insert o