(互聯網文檔整理)
MySQL的用戶管理,指的是哪個用戶可以連接服務器,從哪裏連接,連接後能做什麼.
MySQL中grant語句創建mysql用戶並指定其權限,而revoke語句刪除權限。兩條語句實現了mysql數據庫的用戶管理,並提供與直接操作這些表的內容不同的另一種方法。
create和revoke語句影響4個表:授權表內容
user 能連接服務器的用戶以及他們擁有的任何全局權限
db 數據庫級權限
tables_priv 表級權限
columns_priv 列級權限
還有第5個授權表(host),但它不受grant和revoke的影響。
當你對一個用戶發出一條grant語句時,在user表中爲該用戶創建一條記錄。
如果語句指定任何全局權限(管理權限或適用於所有數據庫的權限),這些也記錄在user表中。如果你指定數據庫、表和列級權限,他們被分別記錄在db、tables_priv和columns_priv表中。
本篇將以實例的形式講解創建刪除MySQL用戶權限...
1.創建用戶並授權
grant語句的語法:
grant privileges (columns) on what to 'user'@'%'
identified by 'password' with grant option
要使用該句型,需確定字段有:
privileges 權限指定符權限允許的操作
alter 修改表和索引
create 創建數據庫和表
delete 刪除表中已有的記錄
drop 拋棄(刪除)數據庫和表
index 創建或拋棄索引
insert 向表中插入新行
reference 未用
select 檢索表中的記錄
update 修改現存表記錄
file 讀或寫服務器上的文件
process 查看服務器中執行的線程信息或殺死線程
reload 重載授權表或清空日誌、主機緩存或表緩存。
shutdown 關閉服務器
all 所有;all privileges同義詞
usage 特殊的“無權限”權限
以上權限分三組:
第一組:適用於數據庫、表和列如:alter create delete drop index insert select update
第二組:數管理權限 它們允許用戶影響服務器的操作 需嚴格地授權 如:file process reload shut*
第三組:權限特殊 all意味着“所有權限” uasge意味着無權限,即創建用戶,但不授予權限
columns
權限運用的列(可選)並且你只能設置列特定的權限。如果命令有多於一個列,應該用逗號分開它們。
what
權限運用的級別。權限可以是全局,定數據庫或特定表.
user
權限授予的用戶,由一個用戶名和主機名組成,許兩個同名用戶從不同地方連接.缺省:mysql用戶
password
賦予用戶的口令(可選),如果你對用戶沒有指定identified by子句,該用戶口令不變.
用identified by時,口令字符串用改用口令的字面含義,grant將爲你編碼口令.
注:set password使用password()函數
with grant option
用戶可以授予權限通過grant語句授權給其它用戶(可選)
實例講解:
注:以下連接區域,用戶名及密碼應加'',如 'huaying'@'koowo.com'
grant all on db_book.* to 'huaying'@'koowo.com' identified by 'yeelion' 只能在本地連接
grant all on db_book.* to 'huaying'@'vpn.koowo.com' identified by 'yeeliong' 允許從此域連接
grant all on db_book.* to 'huaying'@'%' identified by 'yeelion' 允許從任何主機連接
注:"%"字符起通配符作用,與like模式匹配的含義相同,yeelion爲口令.
grant all on db_book.* to 'huaying'@%.koowo.com identified by 'yeelion';
允許huaying從koowo.com域的任何主機連接
grant all on db_book.* to 'huaying'@'192.168.1.189' identified by 'yeelion';
grant all on db_book.* to 'huaying'@'192.168.1.%' identified by 'yeelion'
grant all on db_book.* to 'huaying'@'192.168.1.0/17' identified by 'yeelion'
允許從單IP 段IP或一子網IP登陸
注:有時 用戶@IP 需用引號 如'huaying'@'192.168.1.0/17'
grant all on *.* to 'huaying'@'localhost' identified by 'yeelion' with grant option
添加超級用戶huaying 可在本地登陸做任何操作.
grant reload on *.* to 'huaying'@'localhost' identified by 'yeelion' 只賦予reload權限
grant all on db_book to 'huaying'@'koowo.com' indetified by 'yeelion' 所有權限
grant select on db_book to 'huaying'@'%' indetified by 'yeelion' 只讀權限
grant select,insert,delete,update on db_book to 'huaying'@'koowo.com' indetified by 'yeelion'
只有select,insert,delete,update的權限
grant select on db_book.storybook to 'huaying'@'localhost' indetified by 'yeelion' 只對表
grant update (name) on db_book.storybook to 'huaying'@'localhost' 只對表的name列 密碼不變
grant update (id,name,author) on db_book.storybook to 'huaying'@'localhost' 只對表的多列
grant all on book.* to ''@'koowo.com' 允許koowo.com域中的所有用戶使用庫book
grant all on book.* to 'huaying'@'%.koowo.com' indetified by 'yeelion' with grant option
允許huaying對庫book所有表的管理員授權.
flush privileges; 重載授權表 操作爲更新,使之生效。
2.查看撤權並刪除用戶權限。
show grants; 查看當前用戶(自己)權限
show grants for db_user@localhost; 查看其他 MySQL 用戶權限:
revoke 跟 grant 的語法差不多,只需要把關鍵字 “to” 換成 “from” 即可:
to用from取代,沒有indetifed by和with grant option子句. 如下:
revoke privileges (columns) on what from user
user:必須匹配原來grant語句的你想撤權的用戶的user部分。
privileges:不需匹配,可以用grant語句授權,然後用revoke語句只撤銷部分權限。
revoke語句只刪權限不刪用戶,撤銷了所有權限後user表中用戶記錄保留,用戶仍然可以連接服務器.
要完全刪除一個用戶必須用一條delete語句明確從user表中刪除用戶記錄:
delete from user where user='huaying'
flush privileges; 重載授權表
注:使