1. 程式人生 > >Mysql中grant命令詳解

Mysql中grant命令詳解

1.遠端登入mysql

mysql -h ip -u root -p 密碼

2.建立使用者

格式:grant 許可權 on 資料庫.* to 使用者名稱@登入主機 identified by “密碼”;

例1:增加一個test1使用者,密碼為123456,可以在任何主機上登入,並對所有資料庫有查詢,增加,修改和刪除的功能。需要在mysql的root使用者下進行
mysql>grant select,insert,update,delete on *.* to [email protected]”%” identified by “123456″;
mysql>flush privileges;

例2:增加一個test2使用者,密碼為123456,只能在192.168.2.12上登入,並對資料庫student有查詢,增加,修改和刪除的功能。需要在mysql的root使用者下進行
mysql>grant select,insert,update,delete on student.* to [email protected] identified by “123456″;
mysql>flush privileges;

例3:授權使用者test3擁有資料庫student的所有許可權
mysql>grant all privileges on student.* to

[email protected] 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.刪除資料庫和刪除表

mysql>drop database 資料庫名;
mysql>drop table 表名;

6.刪除賬戶及許可權

drop user 使用者名稱@’%’
drop user 使用者名稱@localhost

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

grant 詳細解析如下:

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

MySQL 賦予使用者許可權命令的簡單格式可概括為:

grant 許可權 on 資料庫物件 to 使用者

一、grant 普通資料使用者,查詢、插入、更新、刪除 資料庫中所有表資料的權利。

grant select on testdb.* to [email protected]’%’
grant insert on testdb.* to [email protected]’%’
grant update on testdb.* to [email protected]’%’
grant delete on testdb.* to [email protected]’%’
或者,用一條 MySQL 命令來替代:
grant select, insert, update, delete on testdb.* to [email protected]’%’

二、grant 資料庫開發人員,建立表、索引、檢視、儲存過程、函式。。。等許可權。

grant 建立、修改、刪除 MySQL 資料表結構許可權。
grant create on testdb.* to [email protected]’192.168.0.%’;
grant alter on testdb.* to [email protected]’192.168.0.%’;
grant drop on testdb.* to [email protected]’192.168.0.%’;
grant 操作 MySQL 外來鍵許可權。
grant references on testdb.* to [email protected]’192.168.0.%’;
grant 操作 MySQL 臨時表許可權。
grant create temporary tables on testdb.* to [email protected]’192.168.0.%’;
grant 操作 MySQL 索引許可權。
grant index on testdb.* to [email protected]’192.168.0.%’;
grant 操作 MySQL 檢視、檢視檢視原始碼 許可權。
grant create view on testdb.* to [email protected]’192.168.0.%’;
grant show view on testdb.* to [email protected]’192.168.0.%’;
grant 操作 MySQL 儲存過程、函式 許可權。
grant create routine on testdb.* to [email protected]’192.168.0.%’; — now, can show procedure status
grant alter routine on testdb.* to [email protected]’192.168.0.%’; — now, you can drop a procedure
grant execute on testdb.* to [email protected]’192.168.0.%’;

三、grant 普通 DBA 管理某個 MySQL 資料庫的許可權。

grant all privileges on testdb to [email protected]’localhost’
其中,關鍵字 “privileges” 可以省略。

四、grant 高階 DBA 管理 MySQL 中所有資料庫的許可權。

grant all on *.* to [email protected]’localhost’

五、MySQL grant 許可權,分別可以作用在多個層次上。

1. grant 作用在整個 MySQL 伺服器上:
grant select on *.* to [email protected]; — dba 可以查詢 MySQL 中所有資料庫中的表。
grant all on *.* to [email protected]; — dba 可以管理 MySQL 中的所有資料庫
2. grant 作用在單個數據庫上:
grant select on testdb.* to [email protected]; — dba 可以查詢 testdb 中的表。
3. grant 作用在單個數據表上:
grant select, insert, update, delete on testdb.orders to [email protected];
4. grant 作用在表中的列上:
grant select(id, se, rank) on testdb.apache_log to [email protected];
5. grant 作用在儲存過程、函式上:
grant execute on procedure testdb.pr_add to ‘dba’@’localhost’
grant execute on function testdb.fn_add to ‘dba’@’localhost’

六、檢視 MySQL 使用者許可權

檢視當前使用者(自己)許可權:
show grants;
檢視其他 MySQL 使用者許可權:
show grants for [email protected];

七、撤銷已經賦予給 MySQL 使用者許可權的許可權。

revoke 跟 grant 的語法差不多,只需要把關鍵字 “to” 換成 “from” 即可:
grant all on *.* to [email protected];
revoke all on *.* from [email protected];

八、MySQL grant、revoke 使用者許可權注意事項

1. grant, revoke 使用者許可權後,該使用者只有重新連線 MySQL 資料庫,許可權才能生效。
2. 如果想讓授權的使用者,也可以將這些許可權 grant 給其他使用者,需要選項 “grant option“
grant select on testdb.* to [email protected] with grant option;
這個特性一般用不到。實際中,資料庫許可權最好由 DBA 來統一管理。
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.建立使用者並授權

grant語句的語法:
grant privileges (columns) on what to user identified by “password” with grant option
要使用該句型,需確定欄位有:

privileges 許可權指定符許可權允許的操作
alter 修改表和索引
create 建立資料庫和表
delete 刪除表中已有的記錄
drop 拋棄(刪除)資料庫和表
index 建立或拋棄索引
insert 向表中插入新行
reference 未用
select 檢索表中的記錄
update 修改現存表記錄
file 讀或寫伺服器上的檔案
process 檢視伺服器中執行的執行緒資訊或殺死執行緒
reload 過載授權表或清空日誌、主機快取或表快取。
shutdown 關閉伺服器
all 所有;

all privileges同義詞
usage 特殊的“無許可權”許可權

以上許可權分三組:
第一組:適用於資料庫、表和列如:alter create delete drop index insert select update
第二組:數管理許可權 它們允許使用者影響伺服器的操作 需嚴格地授權 如:file process reload shut*
第三組:許可權特殊 all意味著“所有許可權” uasge意味著無許可權,即建立使用者,但不授予許可權
columns
許可權運用的列(可選)並且你只能設定列特定的許可權。如果命令有多於一個列,應該用逗號分開它們。
what
許可權運用的級別。許可權可以是全域性,定資料庫或特定表.
user
許可權授予的使用者,由一個使用者名稱和主機名組成,許兩個同名使用者從不同地方連線.預設:mysql使用者password
賦予使用者的口令(可選),如果你對使用者沒有指定identified by子句,該使用者口令不變.
用identified by時,口令字串用改用口令的字面含義,grant將為你編碼口令.
注:set password使用password()函式
with grant option
使用者可以授予許可權通過grant語句授權給其它使用者(可選)

例項講解:
grant all on db_book.* to [email protected] identified by “yeelion”   只能在本地連線
grant all on db_book.* to [email protected] identified by “yeeliong”  允許從此域連線
grant all on db_book.* to [email protected]% identified by “yeelion”   允許從任何主機連線 注:”%”字元起萬用字元作用,與like模式匹配的含義相同。
grant all on db_book.* to [email protected]%.koowo.com identified by “yeelion”;  允許huaying從koowo.com域的任何主機連線
grant all on db_book.* to [email protected] identified by “yeelion”
grant all on db_book.* to [email protected]% identified by “yeelion”
grant all on db_book.* to [email protected]/17 identified by “yeelion”
允許從單IP 段IP或一子網IP登陸
注:有時 使用者@IP 需用引號 如”[email protected]/17″
grant all on *.* to [email protected] identified by “yeelion” with grant option
新增超級使用者huaying 可在本地登陸做任何操作.
grant reload on *.* to [email protected] identified by “yeelion” 只賦予reload許可權
grant all on db_book to [email protected] indetified by “yeelion” 所有許可權
grant select on db_book to [email protected]% indetified by “yeelion” 只讀許可權
grant select,insert,delete,update on db_book to [email protected] indetified by “yeelion”
只有select,insert,delete,update的許可權
grant select on db_book.storybook to [email protected] indetified by “yeelion” 只對表
grant update (name) on db_book.storybook to [email protected] 只對表的name列 密碼不變
grant update (id,name,author) on db_book.storybook to [email protected] 只對表的多列
grant all on book.* to “”@koowo.com 允許koowo.com域中的所有使用者使用庫book

grant all on book.* to [email protected]%.koowo.com indetified by “yeelion” with grant option
允許huaying對庫book所有表的管理員授權.

2.撤權並刪除使用者

revoke的語法類似於grant語句
to用from取代,沒有indetifed by和with grant option子句. 如下:
revoke privileges (columns) on what from user
user:必須匹配原來grant語句的你想撤權的使用者的user部分。
privileges:不需匹配,可以用grant語句授權,然後用revoke語句只撤銷部分許可權。
revoke語句只刪許可權不刪使用者,撤銷了所有許可權後user表中使用者記錄保留,使用者仍然可以連線伺服器.
要完全刪除一個使用者必須用一條delete語句明確從user表中刪除使用者記錄:
delete from user where user=”huaying”
flush privileges; 過載授權表
注:使用grant和revoke語句時,表自動過載,而你直接修改授權表時不是.

例項:

1.建立資料庫
CREATE DATABASE  `fypay` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

2.為建立的資料庫增加使用者fypay
grant create,select,insert,update,delete,drop,alter on fypay.* to [email protected]”%” identified by “testfpay”;

3.刪除fypay使用者
delete from user where user=”fypay”
drop user [email protected]

4.重新整理資料庫
flush privileges;