共計 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 網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!