MySQL权限相关知识总结

29次阅读
没有评论

共计 8055 个字符,预计需要花费 21 分钟才能阅读完成。

本篇内容主要讲解“MySQL 权限相关知识总结”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让丸趣 TV 小编来带大家学习“MySQL 权限相关知识总结”吧!

一.权限表

mysql 中的 3 个权限表:user、db、host

权限表的存取过程是:

  1) 先从 user 表中的 host、user、password 这 3 个字段中判断连接的 IP、用户名、密码是否存在表中,存在则通过身份验证;

2) 通过权限验证,进行权限分配时,按照 userdbtables_privcolumns_priv 的顺序进行分配。即先检查全局权限表

user,如果 user 中对应的权限为 Y,则此用户对所有数据库的权限都为 Y,将不再检查 db,

tables_priv,columns_priv;如果为 N,则到 db 表中检查此用户对应的具体数据库,并得到 db 中为 Y 的权限;如果 db 中为 N,则检

查 tables_priv 中此数据库对应的具体表,取得表中的权限 Y,以此类推。

二.MySQL 各种权限(共 27 个)

(以下操作都是以 root 身份登陆进行 grant 授权,以 p1@localhost 身份登陆执行各种命令。)

1.  usage

连接(登陆)权限,建立一个用户,就会自动授予其 usage 权限(默认授予)。

grant usage on *.* to lsquo;p1 prime;@ rsquo;localhost rsquo;identified by lsquo;123 prime;;

该权限只能用于数据库登陆,不能执行任何操作;且 usage 权限不能被回收,也即 REVOKE 用户并不能删除用户。

2.  select

必须有 select 的权限,才可以使用 select  table

mysql grant select on pyt.* to lsquo;p1 prime;@ rsquo;localhost rsquo;;

mysql select * from shop;

3.  create

必须有 create 的权限,才可以使用 create  table

mysql grant create on pyt.* to lsquo;p1 prime;@ rsquo;localhost rsquo;;

4.  create routine

必须具有 create  routine 的权限,才可以使用 {create |alter|drop} {procedure|function}

mysql grant create routine on pyt.* to lsquo;p1 prime;@ rsquo;localhost rsquo;;

当授予 create routine 时,自动授予 EXECUTE, ALTER ROUTINE 权限给它的创建者:

mysql show grants for lsquo;p1 prime;@ rsquo;localhost rsquo;;

+ mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash;+

Grants  for  p1@localhost

+ mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; ndash;+

| GRANT USAGE ON *.* TO lsquo;p1 prime;@ rsquo;localhost rsquo;IDENTIFIED BY PASSWORD lsquo;*23AE809DDACAF96AF0FD78ED04B6A265E05AA257 prime;|

|  GRANT SELECT, CREATE, CREATE ROUTINE ON `pyt`.* TO lsquo;p1 prime;@ rsquo;localhost rsquo;|

| GRANT EXECUTE, ALTER ROUTINE ON PROCEDURE `pyt`.`pro_shop1` TO lsquo;p1 prime;@ rsquo;localhost rsquo;|

+ mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash; mdash;-+

5.  create temporary tables(注意这里是 tables,不是 table)

必须有 create temporary tables 的权限,才可以使用 create temporary tables.

mysql grant create temporary tables on pyt.* to lsquo;p1 prime;@ rsquo;localhost rsquo;;

[mysql@mydev ~]$ mysql -h localhost -u p1 -p pyt

mysql create temporary table tt1(id int);

6.  create view

  必须有 create view 的权限,才可以使用 create view

  mysql grant create view on pyt.* to lsquo;p1 prime;@ rsquo;localhost rsquo;;

  mysql create view v_shop as select price from shop;

7.  create user

  要使用 CREATE USER,必须拥有 my 的全局 CREATE USER 权限,或拥有 INSERT 权限。

mysql grant create user on *.* to lsquo;p1 prime;@ rsquo;localhost rsquo;;

或:mysql grant insert on *.* to p1@localhost;

8.  insert

  必须有 insert 的权限,才可以使用 insert into hellip;.. values hellip;.

9.  alter

必须有 alter 的权限,才可以使用 alter table

alter table shop modify dealer char(15);

10.  alter routine

必须具有 alter routine 的权限,才可以使用 {alter |drop} {procedure|function}

mysql grant alter routine on pyt.* to lsquo;p1 prime;@ rsquo;localhost lsquo;;

mysql drop procedure pro_shop;

Query OK, 0 rows affected (0.00 sec)

mysql revoke alter routine on pyt.* from lsquo;p1 prime;@ rsquo;localhost rsquo;;

[mysql@mydev ~]$ mysql -h localhost -u p1 -p pyt

mysql drop procedure pro_shop;

ERROR 1370 (42000): alter routine command denied to user lsquo;p1 prime;@ rsquo;localhost rsquo;for routine lsquo;pyt.pro_shop rsquo;

11.  update

必须有 update 的权限,才可以使用 update table

mysql update shop set price=3.5 where article=0001 and dealer= rsquo;A

12.  delete

必须有 delete 的权限,才可以使用 delete from hellip;.where hellip;.(删除表中的记录)

13.  drop

  必须有 drop 的权限,才可以使用 drop database db_name;  drop table tab_name;

  drop view vi_name;  drop index in_name;

14.  show database

  通过 show database 只能看到你拥有的某些权限的数据库,除非你拥有全局 SHOW DATABASES 权限。

  对于 p1@localhost 用户来说,没有对 mysql 数据库的权限,所以以此身份登陆查询时,无法看到 mysql 数据库:

mysql show databases;

+ mdash; mdash; mdash; mdash; mdash; mdash; ndash;+

| Database |

+ mdash; mdash; mdash; mdash; mdash; mdash; ndash;+

| information_schema|

| pyt  |

| test  |

+ mdash; mdash; mdash; mdash; mdash; mdash; ndash;+

15.  show view

  必须拥有 show view 权限,才能执行 show create view。

  mysql grant show view on pyt.* to p1@localhost;

  mysql show create view v_shop;

16.  index

  必须拥有 index 权限,才能执行 [create |drop] index

  mysql grant index on pyt.* to p1@localhost;

  mysql create index ix_shop on shop(article);

  mysql drop index ix_shop on shop;

17.  excute

  执行存在的 Functions,Procedures

  mysql call pro_shop1(0001,@a);

+ mdash; mdash; mdash;+

| article |

+ mdash; mdash; mdash;+

| 0001 |

| 0001 |

+ mdash; mdash; mdash;+

  mysql select @a;

+ mdash; mdash;+

| @a |

+ mdash; mdash;+

| 2 |

+ mdash; mdash;+

18.  lock tables

  必须拥有 lock tables 权限,才可以使用 lock tables

mysql grant lock tables on pyt.* to p1@localhost;

mysql lock tables a1 read;

mysql unlock tables;

19.  references

  有了 REFERENCES 权限,用户就可以将其它表的一个字段作为某一个表的外键约束。

20.  reload

  必须拥有 reload 权限,才可以执行 flush [tables | logs | privileges]

mysql grant reload on pyt.* to p1@localhost;

ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES

mysql grant reload on *.* to lsquo;p1 prime;@ rsquo;localhost rsquo;;

Query OK, 0 rows affected (0.00 sec)

mysql flush tables;

21.  replication client

  拥有此权限可以查询 master server、slave server 状态。

  mysql show master status;

ERROR 1227 (42000): Access denied; you need the SUPER,REPLICATION CLIENT privilege for this operation

  mysql grant Replication client on *.* to p1@localhost;

  或:mysql grant super on *.* to p1@localhost;

  mysql show master status;

+ mdash; mdash; mdash; mdash; mdash; mdash;+ mdash; mdash; mdash;-+ mdash; mdash; mdash; mdash; ndash;+ mdash; mdash; mdash; mdash; mdash; mdash;+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+ mdash; mdash; mdash; mdash; mdash; mdash;+ mdash; mdash; mdash;-+ mdash; mdash; mdash; mdash; ndash;+ mdash; mdash; mdash; mdash; mdash; mdash;+

| mysql-bin.000006 | 2111 |  |  |

+ mdash; mdash; mdash; mdash; mdash; mdash;+ mdash; mdash; mdash;-+ mdash; mdash; mdash; mdash; ndash;+ mdash; mdash; mdash; mdash; mdash; mdash;+

mysql show slave status;

22.  replication slave

  拥有此权限可以查看从服务器,从主服务器读取二进制日志。

  mysql show slave hosts;

ERROR 1227 (42000): Access denied; you need the REPLICATION SLAVE privilege for this operation

mysql show binlog events;

ERROR 1227 (42000): Access denied; you need the REPLICATION SLAVE privilege for this operation

mysql grant replication slave on *.* to p1@localhost;

mysql show slave hosts;

Empty set (0.00 sec)

mysql show binlog events;

+ mdash; mdash; mdash; mdash; mdash;+ mdash; mdash;-+ mdash; mdash; mdash; mdash; mdash;-+ mdash; mdash; mdash; ndash;+ mdash; mdash; mdash; mdash;-+ mdash; mdash; mdash; mdash; ndash;+

| Log_name | Pos | Event_type | Server_id| End_log_pos|Info |

+ mdash; mdash; mdash; mdash; mdash;+ mdash; mdash;-+ mdash; mdash; mdash; mdash; ndash;+ mdash; mdash; mdash; ndash;+ mdash; mdash; mdash; mdash;-+ mdash; mdash; mdash; mdash; mdash;+

| mysql-bin.000005 | 4 | Format_desc | 1 | 98 | Server ver: 5.0.77-log,

Binlog ver: 4 | |mysql-bin.000005|98|Query|1|197|use `mysql`; create

table a1(i int)engine=myisam|

hellip; hellip; hellip; hellip; hellip; hellip; hellip; hellip; hellip; hellip; hellip; hellip; hellip; hellip;

23.  Shutdown

  关闭 MySQL:

  [mysql@mydev ~]$ mysqladmin shutdown

  重新连接:

  [mysql@mydev ~]$ mysql

ERROR 2002 (HY000): Can rsquo;t connect to local MySQL server through socket lsquo;/tmp/mysql.sock rsquo; (2)

[mysql@mydev ~]$ cd /u01/mysql/bin

[mysql@mydev bin]$ ./mysqld_safe

[mysql@mydev bin]$ mysql

24.  grant option

  拥有 grant option,就可以将自己拥有的权限授予其他用户(仅限于自己已经拥有的权限)

  mysql grant Grant option on pyt.* to p1@localhost;

  mysql grant select on pyt.* to p2@localhost;

25.  file

  拥有 file 权限才可以执行 select ..into outfile 和 load data infile hellip; 操作,但是不要把 file, process, super 权限授予管理员以外的账号,这样存在严重的安全隐患。

  mysql grant file on *.* to p1@localhost;

  mysql load data infile lsquo;/home/mysql/pet.txt rsquo; into table pet;

26.  super

  这个权限允许用户终止任何查询;修改全局变量的 SET 语句;使用 CHANGE MASTER,PURGE MASTER LOGS。

  mysql grant super on *.* to p1@localhost;

  mysql purge master logs before lsquo;mysql-bin.000006 prime;;

27.  process

  通过这个权限,用户可以执行 SHOW PROCESSLIST 和 KILL 命令。默认情况下,每个用户都可以执行 SHOW PROCESSLIST 命令,但是只能查询本用户的进程。

  mysql show processlist;

+ mdash;-+ mdash; mdash;+ mdash; mdash; mdash; ndash;+ mdash; mdash;+ mdash; mdash; mdash;+ mdash; mdash;+ mdash; mdash;-+ mdash; mdash; mdash; mdash; mdash; mdash;+

| Id | User | Host  | db  | Command | Time | State | Info  |

+ mdash;-+ mdash; mdash;+ mdash; mdash; mdash; ndash;+ mdash; mdash;+ mdash; mdash; mdash;+ mdash; mdash;+ mdash; mdash;-+ mdash; mdash; mdash; mdash; mdash; mdash;+

| 12 | p1 | localhost | pyt  | Query |  0 | NULL | show processlist |

+ mdash;-+ mdash; mdash;+ mdash; mdash; mdash; ndash;+ mdash; mdash;+ mdash; mdash; mdash;+ mdash; mdash;+ mdash; mdash;-+ mdash; mdash; mdash; mdash; mdash; mdash;+

另外,

管理权限(如 super,process,file 等)不能够指定某个数据库,on 后面必须跟 *.*

mysql grant super on pyt.* to p1@localhost;

ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES

mysql grant super on *.* to p1@localhost;

Query OK, 0 rows affected (0.01 sec)

到此,相信大家对“MySQL 权限相关知识总结”有了更深的了解,不妨来实际操作一番吧!这里是丸趣 TV 网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-28发表,共计8055字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)