MySQL赋予用户权限的命令的简单格式为两种:
- grant 权限 on 数据库对象 to 用户
- grant 权限 on 数据库对象 to 用户 identified by "密码"
示例:
用户名:ad,密码:ad_pass,登陆ip:192.168.0.10
用户在所有登陆ip的权限
- grant all on *.* to ad@‘%’ identified by "ad_pass";
开放管理MySQL中所有数据库的权限
- grant all on *.* to ad@'192.168.0.10' identified by "ad_pass";
开放管理MySQL中具体数据库(test)的权限
- grant all privileges on test to ad@'192.168.0.10' identified by "ad_pass";
或
- grant all on test to ad@'192.168.0.10' identified by "ad_pass";
开放管理MySQL中具体数据库中的表(test.table1)的权限
- grant all on test.table1 to ad@'192.168.0.10' identified by "ad_pass"
开放管理MySQL中具体数据库的表(test.table1)的部分列的权限
- grant select(id,se,rank) on test.table1 to ad@'192.168.0.10' identified by "ad_pass";
开放管理(增删改查)操作指令
- grant select,insert,update,delete on test.* to ad@'192.168.0.10' identified by "ad_pass";
回收权限
- revoke all on *.* from ad@localhost;
查看MySQL用户权限
- show grants;
- show grants for ad@localhost;
grant命令拓展知识:
一、grant 普通数据用户,查询、插入、更新、删除 数据库中所有表数据的权利
- grant select on testdb.* to common_user@'%'
- grant insert on testdb.* to common_user@'%'
- grant update on testdb.* to common_user@'%'
- grant delete on testdb.* to common_user@'%'
或者,用一条 MySQL 命令来替代:
- grant select, insert, update, delete on testdb.* to common_user@'%'
二、grant 数据库开发人员,创建表、索引、视图、存储过程、函数......等权限。
grant 创建、修改、删除 MySQL 数据表结构权限。
- grant create on testdb.* to developer@'192.168.0.%';
- grant alter on testdb.* to developer@'192.168.0.%';
- grant drop on testdb.* to developer@'192.168.0.%';
grant 操作 MySQL 外键权限。
- grant references on testdb.* to developer@'192.168.0.%';
grant 操作 MySQL 临时表权限。
- grant create temporary tables on testdb.* to developer@'192.168.0.%';
grant 操作 MySQL 索引权限。
- grant index 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.%'; -- 现在可以显示程序状态
- grant alter routine on testdb.* to developer@'192.168.0.%'; -- 现在,你可以放一个程序
- grant execute on testdb.* to developer@'192.168.0.%';
三、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;
这里在给一个用户授权多张表时,可以多次执行以上语句。例如:
- grant select(user_id,username) on smp.users to mo_user@'%' identified by '123345';
- grant select on smp.mo_sms to mo_user@'%' 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'
2017年10月09日 17:00:09 沙发
我远程连接mysql一直连接不上,搞了两天也没有成功,用了下面的方法才成功。
远程登录权限:
上面授权是允许myuser用户,从任何机器都能访问mysql服务器。
%代表任何客户端,也可以是localhost,或者是某一ip地址。
with grant option表示它具有grant权限。