mysql 用戶管理及數(shù)據(jù)權(quán)限問(wèn)題總結(jié)

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ù)
  1. grant 作用在單個(gè)數(shù)據(jù)庫(kù)上:
    grant select on testdb.* to dba@localhost;— dba 可以查詢 testdb 中的表膜赃。
  2. grant 作用在單個(gè)數(shù)據(jù)表上:
    grant select, insert, update, delete on testdb.orders to dba@localhost;
  3. grant 作用在表中的列上:
    grant select(id, se, rank) on testdb.apache_log to dba@localhost;
  4. 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)一管理蛋哭。

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;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末鸡挠,一起剝皮案震驚了整個(gè)濱河市辉饱,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌拣展,老刑警劉巖彭沼,帶你破解...
    沈念sama閱讀 222,183評(píng)論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異备埃,居然都是意外死亡姓惑,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)按脚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)于毙,“玉大人,你說(shuō)我怎么就攤上這事辅搬⊥冢” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,766評(píng)論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)烂翰。 經(jīng)常有香客問(wèn)我夯缺,道長(zhǎng),這世上最難降的妖魔是什么甘耿? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,854評(píng)論 1 299
  • 正文 為了忘掉前任踊兜,我火速辦了婚禮,結(jié)果婚禮上佳恬,老公的妹妹穿的比我還像新娘捏境。我一直安慰自己,他們只是感情好毁葱,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布垫言。 她就那樣靜靜地躺著,像睡著了一般倾剿。 火紅的嫁衣襯著肌膚如雪筷频。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,457評(píng)論 1 311
  • 那天前痘,我揣著相機(jī)與錄音凛捏,去河邊找鬼。 笑死芹缔,一個(gè)胖子當(dāng)著我的面吹牛坯癣,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播最欠,決...
    沈念sama閱讀 40,999評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼示罗,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了芝硬?” 一聲冷哼從身側(cè)響起鹉勒,我...
    開(kāi)封第一講書(shū)人閱讀 39,914評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎吵取,沒(méi)想到半個(gè)月后禽额,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,465評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡皮官,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評(píng)論 3 342
  • 正文 我和宋清朗相戀三年脯倒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片捺氢。...
    茶點(diǎn)故事閱讀 40,675評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡藻丢,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出摄乒,到底是詐尸還是另有隱情悠反,我是刑警寧澤残黑,帶...
    沈念sama閱讀 36,354評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站斋否,受9級(jí)特大地震影響梨水,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜茵臭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評(píng)論 3 335
  • 文/蒙蒙 一疫诽、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧旦委,春花似錦奇徒、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,514評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至查辩,卻和暖如春胖笛,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背宜肉。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,616評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留翎碑,地道東北人谬返。 一個(gè)月前我還...
    沈念sama閱讀 49,091評(píng)論 3 378
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像日杈,于是被迫代替她去往敵國(guó)和親遣铝。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評(píng)論 2 360

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