相關知識:
1、創建用戶:
(1)創建名為user1的用戶,密碼為user1coder,只能在localhost登錄。
CREATE USER 'user1'@'localhost' IDENTIFIED BY 'user1coder';
(2)創建名為user2,密碼為123的用戶,可以通過任何ip連接數據庫。
create user 'user2'@'%' identified by '123'
(3)刪除用戶
DROP USER 'user1'@'localhost';
(4)查看用戶
select user,host from mysql.user;
2、權限管理
- all 所有權限(不包括授權權限)
- Delete權限代表允許刪除行數據的權限
- Insert權限代表是否允許在表里插入數據
- Select權限代表允許從表中查看數據,某些不查詢表數據的select執行則不需要此權限,如Select 1+1, Select PI()+2;而且select權限在執行update/delete語句中含有where條件的情況下也是需要的
- Update權限代表允許修改表中的數據的權限
- Usage權限是創建一個用戶之后的默認權限,其本身代表連接登錄權限
(1)查看權限
show grants for 'user1'@'localhost' ;
(2)收回權限
- revoke 權限列表 on 對象列表 from 用戶列表
- revoke all on mydb.* from 'user1'@'localhost'; //回收所有權限
- revoke select,delete on *.* from 'user1'@'%'; //回收部分權限
3、庫名.表名
- *.* 所有數據庫下的所有表
- 數據庫.* 指定數據庫下的所有表
- 數據庫名稱.表名稱 指定數據庫的指定表
- select(col1名稱),insert(col1名稱,col2名稱) on mydb.mytable 只能對mydb.mytable第一列有讀權限,第一第二列有插入權限
任務描述:
注:grant命令執行后如果數據庫中沒有對應的角色會自動創建(授權命令)
(1)數據庫維護人員(1人):可對訂單數據庫進行任何操作。
賬號名稱:system_dbowner,允許任何ip通過此用戶連接數據庫,密碼為usercode1
grant all on 訂單數據庫.* to 'system_dbowner'@'%' identified by 'usercode1';
(2)數據錄入人員(2人):可對訂單數據庫中所有表進行插入、刪除、更新操作,不能創建與修改表結構及其它授權等操作。
賬號名稱:datarecorder1, datarecorder2,允許任何ip通過此用戶連接數據庫,密碼為usercode1
grant insert,delete,update,select on 訂單數據庫.* to 'datarecorder1'@'%' identified by 'usercode1'; grant insert,delete,update,select on 訂單數據庫.* to 'datarecorder2'@'%' identified by 'usercode1';
(3)訂單管理人員(2人):能對訂單數據庫中的訂單表和項目表進行插入、刪除、更新操作,其它表僅能查詢。不能創建與修改表結構及其它授權等操作。
賬號名稱:order_1,order_2,允許任何ip通過此用戶連接數據庫,密碼為usercode1
grant select on 訂單數據庫.* to 'order_1'@'%' identified by 'usercode1';--授予查詢所有表格的權限 grant insert,delete,update on 訂單數據庫.訂單 to 'order_1'@'%' identified by 'usercode1'; grant insert,delete,update on 訂單數據庫.訂貨項目 to 'order_1'@'%' identified by 'usercode1'; grant select on 訂單數據庫.* to 'order_2'@'%' identified by 'usercode2';--授予查詢所有表格的權限 grant insert,delete,update on 訂單數據庫.訂單 to 'order_2'@'%' identified by 'usercode1'; grant insert,delete,update on 訂單數據庫.訂貨項目 to 'order_2'@'%' identified by 'usercode1';
(4)客戶管理人員(2人):能對訂單數據庫中的代理商表和客戶表進行插入、刪除、更新,其它表僅能查詢。不能創建與修改表結構及其它授權等操作。
賬號名稱:customer_1, customer_2,允許任何ip通過此用戶連接數據庫,密碼為usercode1
grant select on 訂單數據庫.* to 'customer_1'@'%' identified by 'usercode1';--授予查詢所有表格的權限 grant insert,delete,update on 訂單數據庫.代理商 to 'customer_1'@'%' identified by 'usercode1'; grant insert,delete,update on 訂單數據庫.客戶 to 'customer_1'@'%' identified by 'usercode1'; grant select on 訂單數據庫.* to 'customer_2'@'%' identified by 'usercode1';--授予查詢所有表格的權限 grant insert,delete,update on 訂單數據庫.客戶 to 'customer_2'@'%' identified by 'usercode1'; grant insert,delete,update on 訂單數據庫.代理商 to 'customer_2'@'%' identified by 'usercode1';