1.遠(yuǎn)程登錄mysql
mysql -h ip -u root -p 密碼
2.創(chuàng)建用戶
格式:grant 權(quán)限 on 數(shù)據(jù)庫(kù).* to 用戶名@登錄主機(jī) identified by “密碼”;
例1:增加一個(gè)test1用戶,密碼為123456,可以在任何主機(jī)上登錄,并對(duì)所有數(shù)據(jù)庫(kù)有查詢拷邢,增加,修改和刪除的功能疙教。需要在mysql的root用戶下進(jìn)行
mysql>grant select,insert,update,delete on *.* to test1@”%” identified by “123456″候引;
mysql>flush privileges;
例2:增加一個(gè)test2用戶邓尤,密碼為123456权谁,只能在192.168.2.12上登錄剩檀,并對(duì)數(shù)據(jù)庫(kù)student有查詢,增加旺芽,修改和刪除的功能谨朝。需要在mysql的root用戶下進(jìn)行
mysql>grant select,insert,update,delete on student.* to test2@192.168.2.12 identified by “123456″;
mysql>flush privileges;
例3:授權(quán)用戶test3擁有數(shù)據(jù)庫(kù)student的所有權(quán)限
mysql>grant all privileges on student.* to test3@localhost identified by ’123456′;
mysql>flush privileges;
3.修改用戶密碼
mysql>update mysql.user set password=password(’123456′) where User=’test1′ and Host=’localhost’;
mysql>flush privileges;
4.刪除用戶
mysql>delete from user where user=’test2′ and host=’localhost’;
mysql>flush privileges;
5.刪除數(shù)據(jù)庫(kù)和刪除表
mysql>drop database 數(shù)據(jù)庫(kù)名;
mysql>drop table 表名;
6.刪除賬戶及權(quán)限
drop user 用戶名@’%’
drop user 用戶名@localhost
**************************************************************************************
grant 詳細(xì)解析如下:
**************************************************************************************
MySQL 賦予用戶權(quán)限命令的簡(jiǎn)單格式可概括為:
grant 權(quán)限 on 數(shù)據(jù)庫(kù)對(duì)象 to 用戶
一卤妒、grant 普通數(shù)據(jù)用戶,查詢舔庶、插入狐史、更新、刪除 數(shù)據(jù)庫(kù)中所有表數(shù)據(jù)的權(quán)利。
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 命令來(lái)替代:
grant select, insert, update, delete on testdb.* to common_user@’%’
二、grant 數(shù)據(jù)庫(kù)開(kāi)發(fā)人員并级,創(chuàng)建表逐哈、索引、視圖便贵、存儲(chǔ)過(guò)程菠镇、函數(shù)。承璃。利耍。等權(quán)限。
grant 創(chuàng)建盔粹、修改隘梨、刪除 MySQL 數(shù)據(jù)表結(jié)構(gòu)權(quán)限。
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 外鍵權(quán)限舷嗡。
grant references on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 臨時(shí)表權(quán)限轴猎。
grant create temporary tables on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 索引權(quán)限。
grant index on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 視圖进萄、查看視圖源代碼 權(quán)限捻脖。
grant create view on testdb.* to developer@’192.168.0.%’;
grant show view on testdb.* to developer@’192.168.0.%’;
grant 操作 MySQL 存儲(chǔ)過(guò)程、函數(shù) 權(quán)限中鼠。
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 管理某個(gè) MySQL 數(shù)據(jù)庫(kù)的權(quán)限。
grant all privileges on testdb to dba@’localhost’
其中兜蠕,關(guān)鍵字 “privileges” 可以省略扰肌。
四、grant 高級(jí) DBA 管理 MySQL 中所有數(shù)據(jù)庫(kù)的權(quán)限熊杨。
grant all on *.* to dba@’localhost’
五曙旭、MySQL grant 權(quán)限,分別可以作用在多個(gè)層次上晶府。
1. grant 作用在整個(gè) MySQL 服務(wù)器上:
grant select on *.* to dba@localhost; — dba 可以查詢 MySQL 中所有數(shù)據(jù)庫(kù)中的表桂躏。
grant all on *.* to dba@localhost; — dba 可以管理 MySQL 中的所有數(shù)據(jù)庫(kù)
2. grant 作用在單個(gè)數(shù)據(jù)庫(kù)上:
grant select on testdb.* to dba@localhost; — dba 可以查詢 testdb 中的表。
3. grant 作用在單個(gè)數(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 作用在存儲(chǔ)過(guò)程川陆、函數(shù)上:
grant execute on procedure testdb.pr_add to ‘dba’@’localhost’
grant execute on function testdb.fn_add to ‘dba’@’localhost’
六剂习、查看 MySQL 用戶權(quán)限
查看當(dāng)前用戶(自己)權(quán)限:
show grants;
查看其他 MySQL 用戶權(quán)限:
show grants for dba@localhost;
七、撤銷已經(jīng)賦予給 MySQL 用戶權(quán)限的權(quán)限。
revoke 跟 grant 的語(yǔ)法差不多鳞绕,只需要把關(guān)鍵字 “to” 換成 “from” 即可:
grant all on *.* to dba@localhost;
revoke all on *.* from dba@localhost;
八失仁、MySQL grant、revoke 用戶權(quán)限注意事項(xiàng)
1. grant, revoke 用戶權(quán)限后们何,該用戶只有重新連接 MySQL 數(shù)據(jù)庫(kù)萄焦,權(quán)限才能生效。
2. 如果想讓授權(quán)的用戶冤竹,也可以將這些權(quán)限 grant 給其他用戶拂封,需要選項(xiàng) “grant option“
grant select on testdb.* to dba@localhost with grant option;
這個(gè)特性一般用不到。實(shí)際中鹦蠕,數(shù)據(jù)庫(kù)權(quán)限最好由 DBA 來(lái)統(tǒng)一管理冒签。
Category: Post
You can follow any responses to this entry via RSS.
Comments are currently closed, but you can trackback from your own site.
=========================================================================
1.創(chuàng)建用戶并授權(quán)
grant語(yǔ)句的語(yǔ)法:
grant privileges (columns) on what to user identified by “password” with grant option
要使用該句型,需確定字段有:
privileges 權(quán)限指定符權(quán)限允許的操作
alter 修改表和索引
create 創(chuàng)建數(shù)據(jù)庫(kù)和表
delete 刪除表中已有的記錄
drop 拋棄(刪除)數(shù)據(jù)庫(kù)和表
index 創(chuàng)建或拋棄索引
insert 向表中插入新行
reference 未用
select 檢索表中的記錄
update 修改現(xiàn)存表記錄
file 讀或?qū)懛?wù)器上的文件
process 查看服務(wù)器中執(zhí)行的線程信息或殺死線程
reload 重載授權(quán)表或清空日志钟病、主機(jī)緩存或表緩存萧恕。
shutdown 關(guān)閉服務(wù)器
all 所有;
all privileges同義詞
usage 特殊的“無(wú)權(quán)限”權(quán)限
以上權(quán)限分三組:
第一組:適用于數(shù)據(jù)庫(kù)档悠、表和列如:alter create delete drop index insert select update
第二組:數(shù)管理權(quán)限 它們?cè)试S用戶影響服務(wù)器的操作 需嚴(yán)格地授權(quán) 如:file process reload shut*
第三組:權(quán)限特殊 all意味著“所有權(quán)限” uasge意味著無(wú)權(quán)限廊鸥,即創(chuàng)建用戶,但不授予權(quán)限
columns
權(quán)限運(yùn)用的列(可選)并且你只能設(shè)置列特定的權(quán)限辖所。如果命令有多于一個(gè)列惰说,應(yīng)該用逗號(hào)分開(kāi)它們。
what
權(quán)限運(yùn)用的級(jí)別缘回。權(quán)限可以是全局,定數(shù)據(jù)庫(kù)或特定表.
user
權(quán)限授予的用戶吆视,由一個(gè)用戶名和主機(jī)名組成,許兩個(gè)同名用戶從不同地方連接.缺省:mysql用戶password
賦予用戶的口令(可選),如果你對(duì)用戶沒(méi)有指定identified by子句,該用戶口令不變.
用identified by時(shí),口令字符串用改用口令的字面含義,grant將為你編碼口令.
注:set password使用password()函數(shù)
with grant option
用戶可以授予權(quán)限通過(guò)grant語(yǔ)句授權(quán)給其它用戶(可選)
實(shí)例講解:
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”?? 允許從任何主機(jī)連接 注:”%”字符起通配符作用酥宴,與like模式匹配的含義相同啦吧。
grant all on db_book.* to huaying@%.koowo.com identified by “yeelion”;? 允許huaying從koowo.com域的任何主機(jī)連接
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或一子網(wǎng)IP登陸
注:有時(shí) 用戶@IP 需用引號(hào) 如”huaying@192.168.1.0/17″
grant all on *.* to huaying@localhost identified by “yeelion” with grant option
添加超級(jí)用戶huaying 可在本地登陸做任何操作.
grant reload on *.* to huaying@localhost identified by “yeelion” 只賦予reload權(quán)限
grant all on db_book to huaying@koowo.com indetified by “yeelion” 所有權(quán)限
grant select on db_book to huaying@% indetified by “yeelion” 只讀權(quán)限
grant select,insert,delete,update on db_book to huaying@koowo.com indetified by “yeelion”
只有select,insert,delete,update的權(quán)限
grant select on db_book.storybook to huaying@localhost indetified by “yeelion” 只對(duì)表
grant update (name) on db_book.storybook to huaying@localhost 只對(duì)表的name列 密碼不變
grant update (id,name,author) on db_book.storybook to huaying@localhost 只對(duì)表的多列
grant all on book.* to “”@koowo.com 允許koowo.com域中的所有用戶使用庫(kù)book
grant all on book.* to huaying@%.koowo.com indetified by “yeelion” with grant option
允許huaying對(duì)庫(kù)book所有表的管理員授權(quán).
2.撤權(quán)并刪除用戶
revoke的語(yǔ)法類似于grant語(yǔ)句
to用from取代,沒(méi)有indetifed by和with grant option子句. 如下:
revoke privileges (columns) on what from user
user:必須匹配原來(lái)grant語(yǔ)句的你想撤權(quán)的用戶的user部分。
privileges:不需匹配,可以用grant語(yǔ)句授權(quán),然后用revoke語(yǔ)句只撤銷部分權(quán)限拙寡。
revoke語(yǔ)句只刪權(quán)限不刪用戶,撤銷了所有權(quán)限后user表中用戶記錄保留,用戶仍然可以連接服務(wù)器.
要完全刪除一個(gè)用戶必須用一條delete語(yǔ)句明確從user表中刪除用戶記錄:
delete from user where user=”huaying”
flush privileges; 重載授權(quán)表
注:使用grant和revoke語(yǔ)句時(shí)授滓,表自動(dòng)重載,而你直接修改授權(quán)表時(shí)不是.
實(shí)例:
1.創(chuàng)建數(shù)據(jù)庫(kù)
CREATE DATABASE? `fypay` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
2.為創(chuàng)建的數(shù)據(jù)庫(kù)增加用戶fypay
grant create,select,insert,update,delete,drop,alter on fypay.* to fypay@”%” identified by “testfpay”;
3.刪除fypay用戶
delete from user where user=”fypay”
drop user fypay@localhost
4.刷新數(shù)據(jù)庫(kù)
flush privileges;?