什麼是許可權管理
不同的身份, 可以幹不同的事情
新增加的使用者, 許可權很少
我們先新建使用者dog
CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456';
複製程式碼
![image.png](https://i.iter01.com/images/0da122cf272d703019abf00ad6e3872d22ace25444086db7cd28ec0a9d92b19a.png)
![image.png](https://i.iter01.com/images/7649e7a2a9c983052a6a1c62ac63b9e16f85fe6e7c9869e84b9cc9cd1b2cd0b0.png)
![image.png](https://i.iter01.com/images/14ff84b420ab9762d7789cafb12b221d91c4e1b153e2e3c9f2d4b7970f16a06d.png)
![image.png](https://i.iter01.com/images/f3b93dd7277a803b0fa3f1c139060d286ea7b28d1147614db0fa7b4d207eb50f.png)
直接例項
1. 對新建使用者dog在library.reader表上授予select和delete許可權
沒有許可權時, 直接查詢會報錯
> 1142 - SELECT command denied to user 'dog'@'localhost' for table 'reader'
> 時間: 0s
複製程式碼
賦予許可權
use library;
grant select,DELETE on reader to dog@localhost;
複製程式碼
![image.png](https://i.iter01.com/images/ebb6aa6c9ea821019c6ac9e5fa6a554749cfdb265d9b4f32e54c13775f4e5bda.png)
但是insert操作依然報錯, 因為沒有許可權
INSERT INTO `library`.`reader`
( `readerid`, `readername`, `readerpass`, `retypeid`, `readerdate`, `readerstatus` )
VALUES
( '0017', '蘇小東', '123456', 1, '1999-09-09 00:00:00', '有效' );
複製程式碼
> 1142 - INSERT command denied to user 'dog'@'localhost' for table 'reader'
> 時間: 0s
複製程式碼
2. 授予dog在library.reader上的姓名和密碼的update許可權
grant update(readername,readerpass) on library.reader to dog@localhost;
複製程式碼
![image.png](https://i.iter01.com/images/9222d6482f89d4d2038faaab1341f5ed0ccffc18f10b4fb4aace1a2b9e0b651d.png)
3. 授予dog使用者在library資料庫中的所有表的select許可權
grant select on library.* to dog@localhost;
複製程式碼
![image.png](https://i.iter01.com/images/1ee1db587dd9dfe4045aa9c89574b2a230022d86326802062f26ed8bb130cd75.png)
4. 授予dog在library資料庫中所有的表操作許可權
grant all on library.* to dog@localhost;
複製程式碼
![image.png](https://i.iter01.com/images/a3ac56b4918246f148f775888dbb32cb66fc4eb01d6adf2092f14acf5d820f19.png)
5. 授予dog對所有資料庫所有表的操作許可權
![image.png](https://i.iter01.com/images/9832e9a6725d2f9bd2a9af16e6c9328f27925d8b13b4633b62503e0e692e1a01.png)
grant insert, delete, update, select on *.* to dog@localhost;
複製程式碼
![image.png](https://i.iter01.com/images/c29bc5dff67c01834f10efc8309447eaead45a8fdab441b74df72a46cd350c6a.png)
6. 授予dog建立新使用者的權利
mysql> CREATE USER 'cat'@'localhost' IDENTIFIED BY '123456';
1227 - Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation
mysql>
複製程式碼
一開始, dog使用者是沒有建立使用者的許可權的
grant create user on *.* to dog@localhost;
複製程式碼
之後, 可以成功
mysql> CREATE USER 'cat'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql>
複製程式碼
![image.png](https://i.iter01.com/images/a0ef33e6bc3f7a99ce7ab749aff7b80a3cf17d7560e9fcae5ac9777dd63a4832.png)
7. 回收使用者dog在library.reader表上的select許可權
revoke select on library.reader from dog@localhost;
複製程式碼
我不喜歡被人收回許可權, 所以就不演示了...