首頁  >  文章  >  資料庫  >  MySQL授權指令grant的使用方法詳解

MySQL授權指令grant的使用方法詳解

怪我咯
怪我咯原創
2017-06-23 13:43:563336瀏覽

本文實例,運行於 MySQL 5.0 以上版本。

MySQL 賦予使用者權限指令的簡單格式可概括為:

#grant 權限on 資料庫物件 to 用戶

#一、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.%'; -- now, can show procedure statusgrant alter  routine on testdb.* to developer@'
192.168.0.%'; -- now, you can drop a proceduregrant 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
'


查看目前使用者(自己)權限:show grants;#查看其他MySQL 使用者權限:##show grants for dba@localhost;
#七、撤銷已經賦予給MySQL 使用者權限的權限。
revoke 跟grant 的語法差不多,只需要把關鍵字「to」 換成「from」 即可:
grant
 
all
on
*
.

*

to

  dba

@localhost

;
revoke all on *.* from dba@localhost;

######################c、MySQL grant、revoke 使用者權限注意事項######1. grant, revoke 使用者權限後,使用者只有重新連接MySQL 資料庫,權限才能生效。 ######2. 如果想要授權的用戶,也可以將這些權限grant 給其他用戶,需要選項「grant option「############grant### ### select### ###on### testdb.###*### ###to### dba###@localhost### ###with### ###grant### ###option###;########################################## #####################這個特性一般用不到。實際中,資料庫權限最好由 DBA 來統一管理。 ###

以上是MySQL授權指令grant的使用方法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn