linux下mysql操作

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;?

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末肆糕,一起剝皮案震驚了整個(gè)濱河市般堆,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌诚啃,老刑警劉巖淮摔,帶你破解...
    沈念sama閱讀 221,576評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異始赎,居然都是意外死亡和橙,警方通過(guò)查閱死者的電腦和手機(jī)仔燕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)魔招,“玉大人晰搀,你說(shuō)我怎么就攤上這事∑桶伲” “怎么了厕隧?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,017評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)俄周。 經(jīng)常有香客問(wèn)我,道長(zhǎng)髓迎,這世上最難降的妖魔是什么峦朗? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,626評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮排龄,結(jié)果婚禮上波势,老公的妹妹穿的比我還像新娘。我一直安慰自己橄维,他們只是感情好尺铣,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,625評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著争舞,像睡著了一般凛忿。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上竞川,一...
    開(kāi)封第一講書(shū)人閱讀 52,255評(píng)論 1 308
  • 那天店溢,我揣著相機(jī)與錄音,去河邊找鬼委乌。 笑死床牧,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的遭贸。 我是一名探鬼主播戈咳,決...
    沈念sama閱讀 40,825評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼壕吹!你這毒婦竟也來(lái)了著蛙?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,729評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤算利,失蹤者是張志新(化名)和其女友劉穎册踩,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體效拭,經(jīng)...
    沈念sama閱讀 46,271評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡暂吉,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,363評(píng)論 3 340
  • 正文 我和宋清朗相戀三年胖秒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片慕的。...
    茶點(diǎn)故事閱讀 40,498評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡阎肝,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出肮街,到底是詐尸還是另有隱情风题,我是刑警寧澤,帶...
    沈念sama閱讀 36,183評(píng)論 5 350
  • 正文 年R本政府宣布嫉父,位于F島的核電站沛硅,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏绕辖。R本人自食惡果不足惜摇肌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,867評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望仪际。 院中可真熱鬧围小,春花似錦、人聲如沸树碱。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,338評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)成榜。三九已至框舔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間伦连,已是汗流浹背雨饺。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,458評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留惑淳,地道東北人额港。 一個(gè)月前我還...
    沈念sama閱讀 48,906評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像歧焦,于是被迫代替她去往敵國(guó)和親移斩。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,507評(píng)論 2 359

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