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