MySQL資料庫授權的兩種方式
方法一:通過grant指令建立使用者并授權
grant指令簡單文法如下:
grant all privileges on dbname.* to username@localhost identified by 'passwd';
清單說明如下:
<a href="https://s1.51cto.com/wyfs02/M02/9E/00/wKioL1mJxPvStj7RAAD3znk_EEc526.png" target="_blank"></a>
舉例:建立zd使用者,對test庫具備所有權限,允許從localhost主機登陸管理資料庫,密碼為123456。
首先,檢視下目前資料庫使用者情況:
mysql> select user,host from mysql.user;
mysql> grant all on test.* to zd@localhost identified by '123456';
最後,檢視目前資料庫使用者情況:
<a href="https://s4.51cto.com/wyfs02/M00/9E/00/wKioL1mJyXXhpSuOAAAhwTQ7x2k667.png" target="_blank"></a>
檢視授權使用者具體權限:
mysql> show grants for zd@localhost;(或者mysql> show grants for zd@localhost\G)
<a href="https://s3.51cto.com/wyfs02/M01/9E/01/wKiom1mJyx2SlGBNAAAfkg_--YE777.png" target="_blank"></a>
方法二:create和grant配合法
首先建立使用者username及密碼passwd,授權主機localhost。
文法:create user username@localhost identified by 'passwd';
如:建立使用者www及密碼123456,授權主機localhost。
mysql> create user www@localhost identified by '123456';
然後授權localhost主機上通過使用者username管理dbname資料庫的所有權限,無需密碼。
文法:grant all on dbname.* to username@localhost;
如:授權localhost主機上www管理test資料庫的所有權限。
mysql> grant all on test.* to zd@localhost;
檢視目前使用者資訊:
<a href="https://s2.51cto.com/wyfs02/M02/9E/01/wKioL1mJ1RaivgaDAAAj-Gt5HNU039.png" target="_blank"></a>
檢視www具體權限:
mysql> show grants for www@localhost;(或者mysql> show grants for www@localhost\G)
<a href="https://s3.51cto.com/wyfs02/M01/9E/01/wKioL1mJ1g6DKUYRAAAX_r_ag98406.png" target="_blank"></a>
本文轉自品鑒初心51CTO部落格,原文連結:http://blog.51cto.com/wutengfei/1954646,如需轉載請自行聯系原作者