MySQL如何使用授權指令grant

coldplay.xixi
發布: 2020-07-11 17:33:58
轉載
2988 人瀏覽過

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

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

grant 权限 on 数据库对象 to 用户
登入後複製

相關學習推薦:mysql影片教學

一、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 status
grant alter routine on testdb.* to developer@'192.168.0.%'; -- now, you can drop a procedure
grant 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'
登入後複製

#2、查看MySQL 使用者權限

查看目前使用者(自己)權限:

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中文網其他相關文章!

相關標籤:
來源:jb51.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!