1. 程式人生 > 資料庫 >【MySQL Database】資料庫管理:使用者與許可權

【MySQL Database】資料庫管理:使用者與許可權

建立使用者
mysql> create user 'soe'@'%' identified by 'soe';
Query OK, 0 rows affected (0.02 sec)

修改使用者密碼
mysql> set password for 'soe'@'%'=password('abcd.1234');
Query OK, 0 rows affected (0.03 sec)

查詢使用者許可權
mysql> show grants for 'soe'@'%';
+-----------------------------------------------------------------------------------------------------+
| Grants for soe@%                                                                                   |
+-----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'soe'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
+-----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

刪除使用者
mysql> drop user 'soe'@'%';
Query OK, 0 rows affected (0.07 sec)

許可權級別分為全域性,庫,表,列四個層次,在指定級別授予的許可權也必須在指定的級別撤銷許可權

授予列的許可權
mysql> grant all (w_id) on soe.warehouse to 'soe'@'%';
Query OK, 0 rows affected (0.02 sec

mysql> grant select (w_name) on soe.warehouse to 'soe'@'%';
Query OK, 0 rows affected (0.03 sec)

mysql> grant insert (w_street_1) on soe.warehouse to 'soe'@'%';
Query OK, 0 rows affected (0.04 sec)

mysql> show grants for 'soe'@'%';
+-----------------------------------------------------------------------------------------------------+
| Grants for soe@%                                                                                   |
+-----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'soe'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
| GRANT SELECT (w_id, w_name), INSERT (w_street_1) ON `soe`.`warehouse` TO 'soe'@'%'            |
+-----------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

撤銷授予的列許可權
mysql> revoke select (w_id, w_name), insert (w_street_1) on soe1000.warehouse from 'soe'@'%';
Query OK, 0 rows affected (0.03 sec)

mysql> show grants for 'soe'@'%';
+-----------------------------------------------------------------------------------------------------+
| Grants for soe@%                                                                                   |
+-----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'soe'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
+-----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


授予表的許可權
mysql> grant all on soe.warehouse to 'soe'@'%';
Query OK, 0 rows affected (0.03 sec)

mysql> show grants for 'soe'@'%';
+-----------------------------------------------------------------------------------------------------+
| Grants for soe@%                                                                                   |
+-----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'soe'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
| GRANT ALL PRIVILEGES ON `soe`.`warehouse` TO 'soe'@'%'                                        |
+-----------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

撤銷授予的表許可權
mysql> revoke all on soe.warehouse from 'soe'@'%';
Query OK, 0 rows affected (0.02 sec)

mysql> show grants for 'soe'@'%';
+-----------------------------------------------------------------------------------------------------+
| Grants for soe@%                                                                                   |
+-----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'soe'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
+-----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


授予庫的許可權
mysql> grant all on soe.* to 'soe'@'%';  
Query OK, 0 rows affected (0.02 sec)

mysql> show grants for 'soe'@'%';
+-----------------------------------------------------------------------------------------------------+
| Grants for soe@%                                                                                   |
+-----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'soe'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
| GRANT ALL PRIVILEGES ON `soe`.* TO 'soe'@'%'                                                  |
+-----------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

撤銷授予的庫許可權
mysql> revoke all on soe.* from 'soe'@'%';  
Query OK, 0 rows affected (0.02 sec)

mysql> show grants for 'soe'@'%';
+-----------------------------------------------------------------------------------------------------+
| Grants for soe@%                                                                                   |
+-----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'soe'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
+-----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


授予全域性許可權
mysql> grant all on *.* to 'soe'@'%';  
Query OK, 0 rows affected (0.01 sec)

mysql> show grants for 'soe'@'%';
+--------------------------------------------------------------------------------------------------------------+
| Grants for soe@%                                                                                            |
+--------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'soe'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
+--------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

撤銷授予的全域性許可權
mysql> revoke all on *.* from 'soe'@'%';
Query OK, 0 rows affected (0.03 sec)

mysql>  show grants for 'soe'@'%';
+-----------------------------------------------------------------------------------------------------+
| Grants for soe@%                                                                                   |
+-----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'soe'@'%' IDENTIFIED BY PASSWORD '*D2B995B159B5840C27690FC435C6ACEE7B9E1348' |
+-----------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)