mysql授權表使用舉例

grant用於給增加用戶和創建權限,revoke用於刪除用戶權限。

下面是一些用grant增加用戶和創建權限的例子:

mysql> grant all privileges on *.* to test@localhost identified by 'test' with grant option;

這句增加一個本地具有所有權限的test用戶(超級用戶),密碼是test。ON子句中的*.*意味着" 所有數據庫、所有表"。with grant option表示它具有grant權限。

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類子網192.168.1連接,對test庫有 select,insert,update,delete,create,drop操作權限。

用grant語句創建權限是不需要再手工 刷新授權表的,因爲它已經自動刷新了。

給用戶創建權限還可以通過直接修改授權表:

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。

mysql> 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類 子網192.168.1連接,對test庫有select,insert,update,delete,create,drop操作權限的test1用 戶,口令是test。要取消一個用戶的權限,使用revoke語句。revoke的語法非常類似於grant語句,除了to用from取代並且沒有 identified by和with grant option子句,下面是用revoke刪除用戶權限的例子:

mysql> revoke all on test.* from test1@'192.168.1.0/255.255.255.0';

這句 revoke就撤消了上面第二句grant創建的權限,但是test1用戶並沒有被刪除,必須手工從user表刪除:

mysql> delete from user where user='test1';

mysql> flush privileges;

這樣,test1用戶就徹底刪除了。

發佈了52 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章