mysql添加用户和用户权限命令:grant

MySQL赋予用户权限的命令的简单格式为两种:

  1. grant 权限 on 数据库对象 to 用户
  2. grant 权限 on 数据库对象 to 用户 identified by "密码"

 

示例:

用户名:ad,密码:ad_pass,登陆ip:192.168.0.10

 

用户在所有登陆ip的权限

  1. grant all on *.* to ad@‘%’ identified by "ad_pass";

 

开放管理MySQL中所有数据库的权限

  1. grant all on *.* to ad@'192.168.0.10' identified by "ad_pass";

 

开放管理MySQL中具体数据库(test)的权限

  1. grant all privileges on test to ad@'192.168.0.10' identified by "ad_pass";

  1. grant all on test to ad@'192.168.0.10' identified by "ad_pass";

 

开放管理MySQL中具体数据库中的表(test.table1)的权限

  1. grant all on test.table1 to ad@'192.168.0.10' identified by "ad_pass"

 

开放管理MySQL中具体数据库的表(test.table1)的部分列的权限

  1. grant select(id,se,rank) on test.table1 to ad@'192.168.0.10' identified by "ad_pass";

 

开放管理(增删改查)操作指令

  1. grant select,insert,update,delete on test.* to ad@'192.168.0.10' identified by "ad_pass";

 

回收权限

  1. revoke all on *.* from ad@localhost;

 

查看MySQL用户权限

  1. show grants;
  2. show grants for ad@localhost;

 

mysql命令grant

 

grant命令拓展知识:

 

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

  1. grant select on testdb.* to common_user@'%'
  2. grant insert on testdb.* to common_user@'%'
  3. grant update on testdb.* to common_user@'%'
  4. grant delete on testdb.* to common_user@'%'

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

  1. grant selectinsertupdatedelete on testdb.* to common_user@'%'

 

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

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

  1. grant create on testdb.* to developer@'192.168.0.%';
  2. grant alter  on testdb.* to developer@'192.168.0.%';
  3. grant drop   on testdb.* to developer@'192.168.0.%';

 

grant 操作 MySQL 外键权限。

  1. grant references on testdb.* to developer@'192.168.0.%';

 

grant 操作 MySQL 临时表权限。

  1. grant create temporary tables on testdb.* to developer@'192.168.0.%';

 

grant 操作 MySQL 索引权限。

  1. grant index on testdb.* to developer@'192.168.0.%';

 

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

  1. grant create view on testdb.* to developer@'192.168.0.%';
  2. grant show   view on testdb.* to developer@'192.168.0.%';

 

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

  1. grant create routine on testdb.* to developer@'192.168.0.%'; -- 现在可以显示程序状态
  2. grant alter  routine on testdb.* to developer@'192.168.0.%'; -- 现在,你可以放一个程序
  3. grant execute        on testdb.* to developer@'192.168.0.%';

 

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

  1. grant all privileges on testdb to dba@'localhost'

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

 

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

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

 

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

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

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

 

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

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

 

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

  1. grant selectinsertupdatedelete on testdb.orders to dba@localhost;

 

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

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

 

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

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

 

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

  1. grant execute on procedure testdb.pr_add to 'dba'@'localhost'
  2. grant execute on function testdb.fn_add to 'dba'@'localhost'
    A+
发布日期:2017年10月08日 18:41:10  所属分类:MySQL
最后更新时间:2017-10-08 19:06:44
付杰
  • ¥ 199.0元
  • 市场价:499.0元
  • ¥ 79.0元
  • 市场价:129.0元
  • ¥ 89.0元
  • 市场价:129.0元
  • ¥ 59.8元
  • 市场价:99.8元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:1   其中:访客  0   博主  0

  1. 头像 PHP灰色 1

    我远程连接mysql一直连接不上,搞了两天也没有成功,用了下面的方法才成功。
    远程登录权限:

    1. mysql> GRANT ALL PRIVILEGES ON *.* TO ‘myuser’@’%’ IDENTIFIED BY ‘mypassword’ WITH GRANT OPTION;  
    2. mysql> FLUSH PRIVILEGES;  

    上面授权是允许myuser用户,从任何机器都能访问mysql服务器。
    %代表任何客户端,也可以是localhost,或者是某一ip地址。
    with grant option表示它具有grant权限。