mysql 創(chuàng)建一個用戶谴古,指定一個數(shù)據(jù)庫

1.遠(yuǎn)程登錄mysql

mysql -h ip -u root -p 密碼

2.創(chuàng)建用戶

格式:grant 權(quán)限 on 數(shù)據(jù)庫.* to 用戶名@登錄主機(jī) identified by “密碼”;

例1:增加一個test1用戶无蜂,密碼為123456,可以在任何主機(jī)上登錄,并對所有數(shù)據(jù)庫有查詢,增加氧苍,修改和刪除的功能夜矗。需要在mysql的root用戶下進(jìn)行

mysql>grant select,insert,update,delete on *.* to test1@”%” identified by “123456″泛范;

mysql>flush privileges;

例2:增加一個test2用戶,密碼為123456紊撕,只能在192.168.2.12上登錄罢荡,并對數(shù)據(jù)庫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ù)庫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ù)庫和刪除表

mysql>drop database 數(shù)據(jù)庫名;

mysql>drop table 表名;

6.刪除賬戶及權(quán)限

drop user 用戶名@’%’

drop user 用戶名@localhost

**************************************************************************************

grant 詳細(xì)解析如下:

**************************************************************************************

MySQL 賦予用戶權(quán)限命令的簡單格式可概括為:

grant 權(quán)限 on 數(shù)據(jù)庫對象 to 用戶

一、grant 普通數(shù)據(jù)用戶浪南,查詢笼才、插入、更新络凿、刪除 數(shù)據(jù)庫中所有表數(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 命令來替代:

grant select, insert, update, delete on testdb.* to common_user@’%’

二、grant 數(shù)據(jù)庫開發(fā)人員摔踱,創(chuàng)建表虐先、索引、視圖派敷、存儲過程蛹批、函數(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 臨時表權(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 存儲過程、函數(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 管理某個 MySQL 數(shù)據(jù)庫的權(quán)限。

grant all privileges on testdb to dba@’localhost’

其中昌腰,關(guān)鍵字 “privileges” 可以省略开伏。

四、grant 高級 DBA 管理 MySQL 中所有數(shù)據(jù)庫的權(quán)限遭商。

grant all on *.* to dba@’localhost’

五固灵、MySQL grant 權(quán)限,分別可以作用在多個層次上劫流。

1. grant 作用在整個 MySQL 服務(wù)器上:

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’

六仍秤、查看 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 的語法差不多诗力,只需要把關(guān)鍵字 “to” 換成 “from” 即可:

grant all on *.* to dba@localhost;

revoke all on *.* from dba@localhost;

八、MySQL grant我抠、revoke 用戶權(quán)限注意事項

1. grant, revoke 用戶權(quán)限后苇本,該用戶只有重新連接 MySQL 數(shù)據(jù)庫导坟,權(quán)限才能生效。

2. 如果想讓授權(quán)的用戶圈澈,也可以將這些權(quán)限 grant 給其他用戶惫周,需要選項 “grant option“

grant select on testdb.* to dba@localhost with grant option;

這個特性一般用不到。實(shí)際中康栈,數(shù)據(jù)庫權(quán)限最好由 DBA 來統(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語句的語法:

grant privileges (columns) on what to user identified by “password” with grant option

要使用該句型,需確定字段有:

privileges 權(quán)限指定符權(quán)限允許的操作

alter 修改表和索引

create 創(chuàng)建數(shù)據(jù)庫和表

delete 刪除表中已有的記錄

drop 拋棄(刪除)數(shù)據(jù)庫和表

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 特殊的“無權(quán)限”權(quán)限

以上權(quán)限分三組:

第一組:適用于數(shù)據(jù)庫悬荣、表和列如:alter create delete drop index insert select update

第二組:數(shù)管理權(quán)限 它們允許用戶影響服務(wù)器的操作 需嚴(yán)格地授權(quán) 如:file process reload shut*

第三組:權(quán)限特殊 all意味著“所有權(quán)限” uasge意味著無權(quán)限菠秒,即創(chuàng)建用戶,但不授予權(quán)限

columns

權(quán)限運(yùn)用的列(可選)并且你只能設(shè)置列特定的權(quán)限氯迂。如果命令有多于一個列践叠,應(yīng)該用逗號分開它們。

what

權(quán)限運(yùn)用的級別嚼蚀。權(quán)限可以是全局,定數(shù)據(jù)庫或特定表.

user

權(quán)限授予的用戶禁灼,由一個用戶名和主機(jī)名組成,許兩個同名用戶從不同地方連接.缺省:mysql用戶password

賦予用戶的口令(可選),如果你對用戶沒有指定identified by子句,該用戶口令不變.

用identified by時,口令字符串用改用口令的字面含義,grant將為你編碼口令.

注:set password使用password()函數(shù)

with grant option

用戶可以授予權(quán)限通過grant語句授權(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登陸

注:有時 用戶@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權(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” 只對表

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所有表的管理員授權(quán).

2.撤權(quán)并刪除用戶

revoke的語法類似于grant語句

to用from取代,沒有indetifed by和with grant option子句. 如下:

revoke privileges (columns) on what from user

user:必須匹配原來grant語句的你想撤權(quán)的用戶的user部分。

privileges:不需匹配,可以用grant語句授權(quán),然后用revoke語句只撤銷部分權(quán)限导帝。

revoke語句只刪權(quán)限不刪用戶,撤銷了所有權(quán)限后user表中用戶記錄保留,用戶仍然可以連接服務(wù)器.

要完全刪除一個用戶必須用一條delete語句明確從user表中刪除用戶記錄:

delete from user where user=”huaying”

flush privileges; 重載授權(quán)表

注:使用grant和revoke語句時守谓,表自動重載,而你直接修改授權(quán)表時不是.

實(shí)例:

1.創(chuàng)建數(shù)據(jù)庫

CREATE DATABASE? `fypay` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

2.為創(chuàng)建的數(shù)據(jù)庫增加用戶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ù)庫

flush privileges;?

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末您单,一起剝皮案震驚了整個濱河市斋荞,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌睹限,老刑警劉巖譬猫,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件讯檐,死亡現(xiàn)場離奇詭異羡疗,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)别洪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進(jìn)店門叨恨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人挖垛,你說我怎么就攤上這事痒钝”牛” “怎么了?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵送矩,是天一觀的道長蚕甥。 經(jīng)常有香客問我,道長栋荸,這世上最難降的妖魔是什么菇怀? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮晌块,結(jié)果婚禮上爱沟,老公的妹妹穿的比我還像新娘。我一直安慰自己匆背,他們只是感情好呼伸,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著钝尸,像睡著了一般括享。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上珍促,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天奶浦,我揣著相機(jī)與錄音,去河邊找鬼踢星。 笑死澳叉,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的沐悦。 我是一名探鬼主播成洗,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼藏否!你這毒婦竟也來了瓶殃?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤副签,失蹤者是張志新(化名)和其女友劉穎遥椿,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體淆储,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡冠场,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了本砰。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片碴裙。...
    茶點(diǎn)故事閱讀 40,127評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出舔株,到底是詐尸還是另有隱情莺琳,我是刑警寧澤,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布载慈,位于F島的核電站惭等,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏办铡。R本人自食惡果不足惜咕缎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望料扰。 院中可真熱鬧凭豪,春花似錦、人聲如沸晒杈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽拯钻。三九已至帖努,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間粪般,已是汗流浹背拼余。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留亩歹,地道東北人匙监。 一個月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像小作,于是被迫代替她去往敵國和親亭姥。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,066評論 2 355

推薦閱讀更多精彩內(nèi)容