MySQL授權(quán)表使用示例
在我們使用MySQL數(shù)據(jù)庫的過程中,經(jīng)常會遇到MySQL授權(quán)表,下面就為您舉例說明MySQL授權(quán)表的用法,供您參考學(xué)習(xí)之用。
MySQL授權(quán)表使用舉例
grant用于給增加用戶和創(chuàng)建權(quán)限,revoke用于刪除用戶權(quán)限。
下面是一些用grant增加用戶和創(chuàng)建權(quán)限的例子:
mysql> grant all privileges on *.* to test@localhost identified by 'test' with grant option;
這句增加一個本地具有所有權(quán)限的test用戶(超級用戶),密碼是test。ON子句中的*.*意味著"所有數(shù)據(jù)庫、所有表"。with grant option表示它具有g(shù)rant權(quán)限。
mysql> grant select,insert,update,delete,create,drop privileges on test.* to test1@'192.168.1.0/255.255.255.0' identified by 'test';
這句是增加了一個test1用戶,口令是test,但是它只能從C類子網(wǎng)192.168.1連接,對test庫有select,insert,update,delete,create,drop操作權(quán)限。
用grant語句創(chuàng)建權(quán)限是不需要再手工刷新授權(quán)表的,因為它已經(jīng)自動刷新了。
給用戶創(chuàng)建權(quán)限還可以通過直接修改授權(quán)表:
mysql> insert into user
values("localhost","test",password("test"),"Y","Y","Y","Y","Y","Y","Y","Y","Y","Y","Y","Y","Y","Y");
mysql> flush privileges;
這兩句和上面***句grant的效果是一樣的,也是增加了一個本地的test超級用戶。我們看到用grant方便多了,而且還不需flush privileges。
m
ysql> insert into user (host,user,password) values("192.168.1.0/255.255.255.0","test1",PASSWORD("test")); mysql> insert into db values("192.168.1.0/255.255.255.0","test","test1","Y","Y","Y","Y","Y","Y","N","N","N","N") mysql> flush privileges;
這三句和上面第二句grant的效果也是一樣的,也是增加了一個只能從C類子網(wǎng)192.168.1連接,對test庫有 select,insert,update,delete,create,drop操作權(quán)限的test1用戶,口令是test。要取消一個用戶的權(quán)限,使用revoke語句。revoke的語法非常類似于grant語句,除了to用from取代并且沒有identified by和with grant option子句,下面是用revoke刪除用戶權(quán)限的例子:
mysql> revoke all on test.* from test1@'192.168.1.0/255.255.255.0';
這句revoke就撤消了上面第二句grant創(chuàng)建的權(quán)限,但是test1用戶并沒有被刪除,必須手工從user表刪除:
mysql> delete from user where user='test1';
mysql> flush privileges;
這樣,test1用戶就徹底刪除了。
這些只是MySQL授權(quán)表的簡單使用,更多詳細的資料請見MySQL提供的手冊。
【編輯推薦】
MySQL MyISAM表結(jié)構(gòu)的恢復(fù)