十年網站開發(fā)經驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網站問題一站解決
這期內容當中小編將會給大家?guī)碛嘘P怎么分析MySQL 權限管理,文章內容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
成都創(chuàng)新互聯(lián)長期為上千客戶提供的網站建設服務,團隊從業(yè)經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網生態(tài)環(huán)境。為洛浦企業(yè)提供專業(yè)的做網站、成都網站建設,洛浦網站改版等技術服務。擁有十余年豐富建站經驗和眾多成功案例,為您定制開發(fā)。
Mysql 賬戶權限信息存儲在 mysql 數(shù)據(jù)庫 user、db、host、table_priv、colunms_priv和procs_priv表中,在Mysql 啟動時服務器將這些數(shù)據(jù)庫表內容讀入內存中。
Mysql 權限層級主要分為: 服務器、數(shù)據(jù)庫、表、列。按權限的使用環(huán)境則主要分為:普通權限、管理員權限、特殊權限,其中普通權限主要應用于應用程序鏈接數(shù)據(jù)庫,管理員權限則主要用于服務器管理。
mysql 權限列表詳解:
分類 | 權限 | 描述 | 應用層級 |
普通權限 (應用程序) | CREATE | 允許用戶創(chuàng)建數(shù)據(jù)庫或表 | 數(shù)據(jù)庫、表或索引 |
DROP | 允許用戶刪除數(shù)據(jù)庫或表 | 數(shù)據(jù)庫或表 | |
GRANT OPTION | 允許用戶授予權限 | 數(shù)據(jù)庫、表或保存的程序 | |
ALTER | 允許用戶改變表結構 | 表 | |
DELETE | 允許用戶刪除現(xiàn)存表的行 | 表 | |
INDEX | 允許用戶創(chuàng)建、修改表索引 | 表 | |
INSERT | 允許用戶在表中插入新的記錄 | 表 | |
SELECT | 允許用戶查看表記錄 | 表 | |
UPDATE | 允許用戶修改表中現(xiàn)有的記錄 | 表 | |
CREATE VIEW | 允許用戶創(chuàng)建視圖 | 視圖 | |
SHOW VIEW | 允許用戶查看視圖創(chuàng)建語句 | 視圖 | |
ALTER ROUTINE | 允許用戶修改存儲過程、函數(shù) | 保存的程序 | |
CREATE ROUTINE | 允許用戶創(chuàng)建存儲過程、函數(shù) | 保存的程序 | |
EXECUTE | 允許用戶允許以創(chuàng)建的子程序 | 保存的程序 | |
管理員 | FILE | 允許用戶使用select…into outfile、load data infile 將數(shù)據(jù)從文件讀入表或從表讀入文件 | 服務器主機上的文件訪問 |
CREATE TEMPORARY TABLES | 允許用戶創(chuàng)建臨時表 | 服務器管理 | |
LOCK TABLES | 允許用戶使用LOCK TABLES | 服務器管理 | |
CREATE USER | 允許用戶使用CREATE USER,DORP USER,RENAME USER,REVOKE ALL PRIVILEGES | 服務器管理 | |
PROCESS | 允許用戶使用show processlist 查看線程 | 服務器管理 | |
RELOAD | 允許用戶使用flush 、重載授權表、清空授權、主機、日志等 | 服務器管理 | |
REPLICATION CLIENT | 允許用戶詢問從屬服務器或主機服務器地址 | 服務器管理 | |
REPLICATION SLAVE | 用于主從復制性從屬服務器(從主服務器中讀取二進制日志文件) | 服務器管理 | |
SHOW DATABASES | 允許使用show databases 查看所有的數(shù)據(jù)庫列表,沒有這個權限,用戶只能看到擁有權限的數(shù)據(jù)庫 | 服務器管理 | |
SHUTDOWN | 允許使用mysqladmin shutdown 關閉mysql服務器 | 服務器管理 | |
SUPER | 允許使用change master,kill,purge master logs 和set global 語句,mysqladmin debug 命令,當數(shù)據(jù)庫達到max_connections 允許連接一次 | 服務器管理 | |
特殊 | all\all perivileges | 授予所有權限 | 服務器管理 |
usage | 僅允許用戶登錄,但不授予權限 | 服務器管理 |
MySQL 賦予用戶權限命令的簡單格式可概括為:
grant 權限 on 數(shù)據(jù)庫對象 to 用戶
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 創(chuàng)建、修改、刪除 MySQL 數(shù)據(jù)表結構權限。
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 存儲過程、函數(shù) 權限。
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 all privileges on testdb to dba@'localhost'
其中,關鍵字 “privileges” 可以省略。
grant all on *.* to dba@'localhost'
1. grant 作用在整個 MySQL 服務器上:
grant select on *.* to dba@localhost; -- dba 可以查詢 MySQL 中所有數(shù)據(jù)庫中的表。 grant all on *.* to dba@localhost; -- dba 可以管理 MySQL 中的所有數(shù)據(jù)庫
2. grant 作用在單個數(shù)據(jù)庫上:
grant select on testdb.* to dba@localhost; -- dba 可以查詢 testdb 中的表。
3. grant 作用在單個數(shù)據(jù)表上:
grant select, insert, update, delete on testdb.orders to dba@localhost;
4. grant 作用在表中的列上:
grant select(id, se, rank) on testdb.apache_log to dba@localhost;
5. grant 作用在存儲過程、函數(shù)上:
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;
revoke 跟 grant 的語法差不多,只需要把關鍵字 “to” 換成 “from” 即可:
grant all on *.* to dba@localhost; revoke all on *.* from dba@localhost;
1. grant, revoke 用戶權限后,該用戶只有重新連接 MySQL 數(shù)據(jù)庫,權限才能生效。
2. 如果想讓授權的用戶,也可以將這些權限 grant 給其他用戶,需要選項 “grant option“
grant select on testdb.* to dba@localhost with grant option;
這個特性一般用不到。實際中,數(shù)據(jù)庫權限最好由 DBA 來統(tǒng)一管理。
----------------------------------------------------------------------------------------------
授權命令GRANT 語句的語法如下:
GRANT privileges (columns)
ON what
TO user IDENTIFIEDBY "password"
WITH GRANT OPTION
對用戶授權
mysql>grant rights on database.* to user@host identified by "pass";
例1:
增加一個用戶test1密碼為abc,讓他可以在任何主機上登錄,并對所有數(shù)據(jù)庫有查詢、插入、修改、刪除的權限。
grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";
ON 子句中*.* 說明符的意思是“所有數(shù)據(jù)庫,所有的表”
例2:
增加一個用戶test2密碼為abc, 讓他只可以在localhost上登錄,并可以對數(shù)據(jù)庫mydb進行查詢、插入、修改、刪除的操作。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";
例子3
增加一個用戶custom,他能從主機localhost、server.domain和whitehouse.gov連接。他只想要從 localhost存取bankaccount數(shù)據(jù)庫,從whitehouse.gov存取expenses數(shù)據(jù)庫和從所有3臺主機存取customer 數(shù)據(jù)庫。他想要從所有3臺主機上使用口令stupid。
為了使用GRANT語句設置個用戶的權限,運行這些命令:
shell> mysql --user=root mysql
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
ON bankaccount.* TO custom@localhost IDENTIFIED BY 'stupid';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
ON expenses.* TO custom@whitehouse.gov IDENTIFIED BY 'stupid';
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
ON customer.* TO custom@'%' IDENTIFIED BY 'stupid';
==============================================
權限信息用user、db、host、tables_priv和columns_priv表被存儲在mysql數(shù)據(jù)庫中(即在名為mysql的數(shù)據(jù)庫中)。
權限 列 Context
select Select_priv 表
insert Insert_priv 表
update Update_priv 表
delete Delete_priv 表
index Index_priv 表
alter Alter_priv 表
create Create_priv 數(shù)據(jù)庫、表或索引
drop Drop_priv 數(shù)據(jù)庫或表
grant Grant_priv 數(shù)據(jù)庫或表
references References_priv 數(shù)據(jù)庫或表
reload Reload_priv 服務器管理
shutdown Shutdown_priv 服務器管理
process Process_priv 服務器管理
file File_priv 在服務器上的文件存取
1.select、insert、update和delete權限 允許你在一個數(shù)據(jù)庫現(xiàn)有的表上實施操作,是基本權限
2.alter權限允許你使用ALTER TABLE
3.create和drop權限允許你創(chuàng)建新的數(shù)據(jù)庫和表,或拋棄(刪除)現(xiàn)存的數(shù)據(jù)庫和表 如果你將mysql數(shù)據(jù)庫的drop權限授予一個用戶,該用戶能拋棄存儲了MySQL存取權限的數(shù)據(jù)庫!
4.grant權限允許你把你自己擁有的那些權限授給其他的用戶。
你不能明顯地指定一個給定用戶應該被拒絕存取。即,你不能明顯地匹配一個用戶并且然后拒絕連接。你不能指定一個用戶有權創(chuàng)建立或拋棄一個數(shù)據(jù)庫中的表,也不能創(chuàng)建或拋棄數(shù)據(jù)庫本身。 可以同時列出許多被授予的單個權限。
例如,如果想讓用戶能讀取和修改已有表的內容,但又不允許創(chuàng)建新表或刪除表,可按如下授權:
GRANT SELECT,INSERT,DELETE,UPDATE ON samp_db.* TO 'user'@'%' IDENTIFIEDBY "pass"
以上是我從別的地方拷貝過來后稍作修改的文字,下面自己寫一些需要注意的東西。
上述就是小編為大家分享的怎么分析Mysql 權限管理了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。