MySQL數據庫用戶、角色、授權


 

權限包括  insert   delete   update   select   all privileges 

登錄MySQL

> mysql -uroot -p
Enter password: ****

 

1. 添加用戶

mysql> insert into mysql.user(host,user,password) values('%', 'xiaoming', password('xiaoming123'));
mysql> flush privileges;

現在可以使用帳號(xiaoming,xiaoming123)登錄MySQL了。但是只有默認權限,僅能操作兩個數據庫(information_schema和test)

 

2. 授權

grant <權限列表> on <關系> to <用戶/角色>

等價於

grant <權限列表> on <關系> to <用戶/角色>@'%' 

表示授權給來自所有主機該用戶

 @<主機>  用來指定用戶登錄的主機

grant insert on school.* to xiaoming

此時,用戶xiaoming就擁有了對數據庫school的insert權限。

mysql> show databases; ---授權前 +--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.00 sec)

mysql> show databases; --- 授權后 +--------------------+
| Database           |
+--------------------+
| information_schema |
| school             |
| test               |
+--------------------+
3 rows in set (0.00 sec)

mysql> use school;
Database changed
mysql> insert into student(name,score) values('xiaoming',60);
Query OK, 1 row affected (0.08 sec)

mysql> select * from student;
ERROR 1142 (42000): SELECT command denied to user 'xiaoming'@'10.0.2.2' for table 'student'

 

3. 添加用戶並授權

grant <權限> on <關系> to <用戶> identified by <密碼> [with grant options]

mysql> grant select on school.* to xiaoqiang@'%' identified by 'xiaoqiang123';
mysql> flush privileges;

 with grant options  參數表示被授權的用戶可以將權限再授權給其他用戶。

 

4. 查看用戶權限

 show grants for <用戶/角色>

mysql> show grants for xiaoqiang;
+----------------------------------------------------------------------------------------------------------+
| Grants for xiaoqiang@%                                                                                   |
+----------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'xiaoqiang'@'%' IDENTIFIED BY PASSWORD '*CFE5D9A8AB243F294A8D3C5B7F2B6BDCF7F71DB5' |
| GRANT SELECT ON `school`.* TO 'xiaoqiang'@'%'                                                            |
+----------------------------------------------------------------------------------------------------------+
2 rows in set

其中的 “usage” 表示默認權限

 

5. 回收權限

revoke <權限> on <關系> from <用戶/角色>

mysql> revoke select on school.* from xiaoqiang;
mysql> flush privileges;

在用戶下次登錄后生效

 

默認是級聯回收,即:會將該用戶的該權限以及該用戶授予給其他用戶的該權限全部回收。

revoke <權限> on <關系> from <用戶/角色> restrict

使用 restrict 防止級聯回收。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM