Mysql数据库权限详细解释

<p>一.权限表</p><p> mysql数据库中的3个权限表:user 、db、 host</p><p>权限表的存取过程是:</p><p> 1)先从user表中的host、 user、 password这3个字段中判断连接的IP、用户名、密码是否存在表中,存在则通过身份验证;</p><p> 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,以此类推。</p><p>二.MySQL各种权限(共27个)</p><p>(以下操作都是以root身份登陆进行grant授权,以p1@localhost身份登陆执行各种命令。)</p><p>1. usage</p><p>连接(登陆)权限,建立一个用户,就会自动授予其usage权限(默认授予)。</p><pre class="brush:bash;toolbar:false">mysql&gt;grantusageon*.*to&#39;p1′@&#39;localhost&#39;identifiedby&#39;123′;</pre><p>该权限只能用于数据库登陆,不能执行任何操作;且usage权限不能被回收,也即REVOKE用户并不能删除用户。</p><p>2. select</p><p>必须有select的权限,才可以使用select table</p><pre class="brush:bash;toolbar:false">mysql&gt;grantselectonpyt.*to&#39;p1′@&#39;localhost&#39;; mysql&gt;select*fromshop;</pre><p>3. create</p><p>必须有create的权限,才可以使用create table</p><pre class="brush:bash;toolbar:false">mysql&gt;grantcreateonpyt.*to&#39;p1′@&#39;localhost&#39;;</pre><p>4. create routine</p><p>必须具有create routine的权限,才可以使用{create |alter|drop} {procedure|function}</p><p>mysql&gt; grant create routine on pyt.* to &#39;p1′@&#39;localhost&#39;;</p><p>当授予create routine时,自动授予EXECUTE, ALTER ROUTINE权限给它的创建者:</p><pre class="brush:bash;toolbar:false">mysql&gt;showgrantsfor&#39;p1′@&#39;localhost&#39;; +—————————————————————————+ Grantsforp1@localhost +————————————————————————–+ |GRANTUSAGEON*.*TO&#39;p1′@&#39;localhost&#39;IDENTIFIEDBYPASSWORD&#39;*23AE809DDACAF96AF0FD78ED04B6A265E05AA257′| |GRANTSELECT,CREATE,CREATEROUTINEON`pyt`.*TO&#39;p1′@&#39;localhost&#39;| |GRANTEXECUTE,ALTERROUTINEONPROCEDURE`pyt`.`pro_shop1`TO&#39;p1′@&#39;localhost&#39;| +————————————————————————————-+</pre><p>5. create temporary tables(注意这里是tables,不是table)</p><p>必须有create temporary tables的权限,才可以使用create temporary tables.</p><pre class="brush:bash;toolbar:false">mysql&gt;grantcreatetemporarytablesonpyt.*to&#39;p1′@&#39;localhost&#39;; [mysql@mydev~]$mysql-hlocalhost-up1-ppyt mysql&gt;createtemporarytablett1(idint);</pre><p>6. create view</p><p>必须有create view的权限,才可以使用create view</p><pre class="brush:bash;toolbar:false">mysql&gt;grantcreateviewonpyt.*to&#39;p1′@&#39;localhost&#39;; mysql&gt;createviewv_shopasselectpricefromshop;</pre><p>7. create user</p><p>要使用CREATE USER,必须拥有mysql数据库的全局CREATE USER权限,或拥有INSERT权限。</p><pre class="brush:bash;toolbar:false">mysql&gt;grantcreateuseron*.*to&#39;p1′@&#39;localhost&#39;; 或:mysql&gt;grantinserton*.*top1@localhost;</pre><p>8. insert</p><p>必须有insert的权限,才可以使用insert into ….. values….</p><p>9. alter</p><p>必须有alter的权限,才可以使用alter table</p><pre class="brush:bash;toolbar:false">altertableshopmodifydealerchar(15);</pre><p>10. alter routine</p><p>必须具有alter routine的权限,才可以使用{alter |drop} {procedure|function}</p><pre class="brush:bash;toolbar:false">mysql&gt;grantalterroutineonpyt.*to&#39;p1′@&#39;localhost&#39;; mysql&gt;dropprocedurepro_shop; QueryOK,0rowsaffected(0.00sec) mysql&gt;revokealterroutineonpyt.*from&#39;p1′@&#39;localhost&#39;; [mysql@mydev~]$mysql-hlocalhost-up1-ppyt mysql&gt;dropprocedurepro_shop; ERROR1370(42000):alterroutinecommanddeniedtouser&#39;p1′@&#39;localhost&#39;forroutine&#39;pyt.pro_shop&#39;</pre><p>11. update</p><p>必须有update的权限,才可以使用update table</p><pre class="brush:bash;toolbar:false">mysql&gt;updateshopsetprice=3.5wherearticle=0001anddealer=&#39;A&#39;;</pre><p>12. delete</p><p>必须有delete的权限,才可以使用delete from ….where….(删除表中的记录)</p><p>13. drop</p><p>必须有drop的权限,才可以使用drop database db_name; drop table tab_name;</p><pre class="brush:bash;toolbar:false">dropviewvi_name;dropindexin_name;</pre><p>14. show database</p><p>通过show database只能看到你拥有的某些权限的数据库,除非你拥有全局SHOW DATABASES权限。</p><p>对于p1@localhost用户来说,没有对mysql数据库的权限,所以以此身份登陆查询时,无法看到mysql数据库:</p><pre class="brush:bash;toolbar:false">mysql&gt;showdatabases; +——————–+ |Database| +——————–+ |information_schema| |pyt| |test| +——————–+</pre><p>15. show view</p><p>必须拥有show view权限,才能执行show create view。</p><pre class="brush:bash;toolbar:false">mysql&gt;grantshowviewonpyt.*top1@localho ysql&gt;showcreateviewv_shop;</pre><p>16. index</p><p>必须拥有index权限,才能执行[create |drop] index</p><pre class="brush:bash;toolbar:false">1mysql&gt;grantindexonpyt.*top1@localhost; 2mysql&gt;createindexix_shoponshop(article); 3mysql&gt;dropindexix_shoponshop;</pre><p></p><p>17. excute</p><p></p><p>执行存在的Functions,Procedures</p><pre class="brush:bash;toolbar:false">1mysql&gt;callpro_shop1(0001,@a); 2+———+ 3|article| 4+———+ 5|0001| 6|0001| 7+———+ 8mysql&gt;select@a; 9+——+ 10|@a| 11+——+ 12|2| 13+——+</pre><p></p><p>18. lock tables</p><p>必须拥有lock tables权限,才可以使用lock tables</p><pre class="brush:bash;toolbar:false">1mysql&gt;grantlocktablesonpyt.*top1@localhost; 2mysql&gt;locktablesa1read; 3mysql&gt;unlocktables;</pre><p>19. references</p><p>有了REFERENCES权限,用户就可以将其它表的一个字段作为某一个表的外键约束。<br/></p><p>20. reload</p><p>必须拥有reload权限,才可以执行flush [tables | logs | privileges]</p><pre class="brush:bash;toolbar:false">1mysql&gt;grantreloadonpyt.*top1@localhost; 2ERROR1221(HY000):IncorrectusageofDBGRANTandGLOBALPRIVILEGES 3mysql&gt;grantreloadon*.*to&#39;p1′@&#39;localhost&#39;; 4QueryOK,0rowsaffected(0.00sec) 5mysql&gt;flushtables;</pre><p>21. replication client</p><p>拥有此权限可以查询master server、slave server状态。</p><pre class="brush:bash;toolbar:false">1mysql&gt;showmasterstatus; 2ERROR1227(42000):Accessdenied;youneedtheSUPER,REPLICATIONCLIENTprivilegeforthisoperation 3mysql&gt;grantReplicationclienton*.*top1@localhost; 4或:mysql&gt;grantsuperon*.*top1@localhost; 5mysql&gt;showmasterstatus; 6+——————+———-+————–+——————+ 7|File|Position|Binlog_Do_DB|Binlog_Ignore_DB| 8+——————+———-+————–+——————+ 9|mysql-bin.000006|2111||| 10+——————+———-+————–+——————+ 11mysql&gt;showslavestatus;</pre><p></p><p>22. replication slave</p><p>拥有此权限可以查看从服务器,从主服务器读取二进制日志。<br/></p><pre class="brush:bash;toolbar:false">1mysql&gt;showslavehosts; 2ERROR1227(42000):Accessdenied;youneedtheREPLICATIONSLAVEprivilegeforthisoperation 3mysql&gt;showbinlogevents; 4ERROR1227(42000):Accessdenied;youneedtheREPLICATIONSLAVEprivilegeforthisoperation 5mysql&gt;grantreplicationslaveon*.*top1@localhost; 6mysql&gt;showslavehosts; 7Emptyset(0.00sec) 8mysql&gt;showbinlogevents; 9+—————+——-+—————-+———–+————-+————–+ 10|Log_name|Pos|Event_type|Server_id|End_log_pos|Info|+—————+——-+————–+———–+————-+—————+ 11|mysql-bin.000005|4|Format_desc|1|98|Serverver:5.0.77-log,Binlogver:4||mysql-bin.000005|98|Query|1|197|use`mysql`;createtablea1(iint)engine=myisam| 12……………………………………</pre><p></p><p>23. Shutdown</p><p>关闭MySQL:<br/></p><pre class="brush:bash;toolbar:false">1[mysql@mydev~]$mysqladminshutdown 2重新连接: 3[mysql@mydev~]$mysql 4ERROR2002(HY000):Can&#39;tconnecttolocalMySQLserverthroughsocket&#39;/tmp/mysql.sock&#39;(2) 5[mysql@mydev~]$cd/u01/mysql/bin 6[mysql@mydevbin]$./mysqld_safe&amp; 7[mysql@mydevbin]$mysql</pre><p><br/></p><p>24. grant option</p><p>拥有grant option,就可以将自己拥有的权限授予其他用户(仅限于自己已经拥有的权限)<br/></p><pre class="brush:bash;toolbar:false">1mysql&gt;grantGrantoptiononpyt.*top1@localhost; 2mysql&gt;grantselectonpyt.*top2@localhost;</pre><p>25. file</p><p>拥有file权限才可以执行 select ..into outfile和load data infile…操作,但是不要把file, process, super权限授予管理员以外的账号,这样存在严重的安全隐患。<br/></p><pre class="brush:bash;toolbar:false">1mysql&gt;grantfileon*.*top1@localhost; 2mysql&gt;loaddatainfile&#39;/home/mysql/pet.txt&#39;intotablepet;</pre><p>26. super</p><p>这个权限允许用户终止任何查询;修改全局变量的SET语句;使用CHANGE MASTER,PURGE MASTER LOGS。</p><pre class="brush:bash;toolbar:false">1mysql&gt;grantsuperon*.*top1@localhost; 2mysql&gt;purgemasterlogsbefore&#39;mysql-bin.000006′;</pre><p></p><p>27. process</p><p>通过这个权限,用户可以执行SHOW PROCESSLIST和KILL命令。默认情况下,每个用户都可以执行SHOW PROCESSLIST命令,但是只能查询本用户的进程。</p><pre class="brush:bash;toolbar:false">1mysql&gt;showprocesslist; 2+—-+——+———–+——+———+——+——-+——————+ 3|Id|User|Host|db|Command|Time|State|Info| 4+—-+——+———–+——+———+——+——-+——————+ 5|12|p1|localhost|pyt|Query|0|NULL|showprocesslist| 6+—-+——+———–+——+———+——+——-+——————+</pre><p></p><p>另外,</p><p>管理权限(如 super, process, file等)不能够指定某个数据库,on后面必须跟*.*</p><pre class="brush:sql;toolbar:false">1mysql&gt;grantsuperonpyt.*top1@localhost; 2ERROR1221(HY000):IncorrectusageofDBGRANTandGLOBALPRIVILEGES 3mysql&gt;grantsuperon*.*top1@localhost; 4QueryOK,0rowsaffected(0.01sec)</pre>
RangeTime:0.007381s
RangeMem:215.54 KB
返回顶部 留言