MYSQL 8用戶及角色管理

Mysql 8創建用戶的操作已經不支持grant的同時創建用戶的方式,需先創建用戶再進行授權,下面這種操作將會報錯

mysql> grant all on *.* to 'test'@'%' identified by '123456';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by '123456'' at line 1

Mysql 8用戶管理

-- 查看幫助
help create user;

-- 創建用戶,僅限本地訪問
create user 'username'@'localhost' identified by 'password';

-- 創建用戶,所有外網都可以訪問
create user 'username'@'%' identified by 'password';

-- 以mysql_native_password加密方式創建用戶
CREATE USER 'username'@'host' IDENTIFIED WITH mysql_native_password BY 'password';

-- 創建帶過期時間的用戶
CREATE USER `test`@`%` IDENTIFIED BY 'test' PASSWORD EXPIRE INTERVAL 90 DAY;

-- 創建一個帶賬戶鎖的用戶
CREATE USER 'username'@'host' IDENTIFIED BY 'password' ACCOUNT LOCK;

-- 刷新權限
flush privileges;

-- 修改密碼
Alter user 'test'@'%' identified by '123456';

-- 修改密碼爲永不過期
ALTER USER 'test'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;

-- 修改密碼並指定加密規則爲mysql_native_password
ALTER USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

-- 刷新權限
flush privileges;

-- 鎖定用戶
ALTER USER 'test'@'%' ACCOUNT LOCK;

-- 用戶解鎖
ALTER USER 'test'@'%' ACCOUNT UNLOCK;

-- 刪除用戶
DROP USER 'username'@'host';

Mysql 8授權(兩個*的含義,第一個*表示數據庫,第二個*表示數據庫的表)

-- 查看授權
show grants;

-- 錯誤的寫法
GRANT ALL PRIVILEGES ON *.*  'test'@'%' identified by ‘123456'-- 授權(所有庫所有權限)
grant all privileges on *.* to 'test'@'%';

-- 授權(某個庫所有權限)
grant all privileges on `test`.* to 'test'@'%';

-- 單獨授予某種權限
GRANT SELECT ON oilsystem.input TO 'test'@'%';

-- 刷新權限
FLUSH PRIVILEGES;

-- 撤銷權限
REVOKE all privileges ON databasename.tablename FROM 'username'@'host';

MYSQL權限
在這裏插入圖片描述
在這裏插入圖片描述

MySQL8.0用戶密碼管理

-- 密碼過期時間管理
-- 要全局建立自動密碼到期策略,請使用default_password_lifetime系統變量。其默認值爲0,禁用自動密碼過期。
-- 如果值default_password_lifetime正整數N,則表示允許的密碼生存期,以便密碼必須每天更改N。在my.cnf文件中加入以下配置
[mysqld]
default_password_lifetime=180

-- 使用默認密碼策略
CREATE USER 'test'@'%' PASSWORD EXPIRE DEFAULT;

-- 禁止重複使用最近6個密碼或密碼超過365天的任何密碼,在my.cnf文件中加入以下配置
[mysqld]
password_history=6
password_reuse_interval=365

MySQL8.0角色管理

-- 創建角色(例子:創建3個角色)
CREATE ROLE 'test_all', 'test_read', 'test_write';

-- 爲角色分配權限(某個庫)
GRANT ALL ON `testdb`.* TO 'test_all';
GRANT SELECT ON `testdb`.* TO 'test_read';
GRANT INSERT, UPDATE, DELETE ON `testdb`.* TO 'test_write';

-- 爲角色分配權限(所有庫)
GRANT ALL ON *.* TO 'test_all';
GRANT SELECT ON *.* TO 'test_read';
GRANT INSERT, UPDATE, DELETE ON *.* TO 'test_write';

-- 爲用戶分配角色
GRANT 'test_all' TO 'test'@'%';
GRANT 'test_read' TO 'read_user1'@'%', 'read_user2'@'%';
GRANT 'test_write', 'app_write' TO 'rw_user1'@'%';

-- 查看用戶權限
SHOW GRANTS FOR 'test'@'%'\G;

-- 要顯示角色權限,添加一個 USING來顯示
SHOW GRANTS FOR 'read_user1'@'localhost' USING 'test_write';

-- 撤消角色或角色權限
REVOKE role FROM user;

-- REVOKE可以用於角色修改角色權限。這不僅影響角色本身權限,還影響任何授予該角色的用戶權限。假設想臨時讓所有用戶只讀,使用REVOKE從該app_write角色中撤消修改權限 
REVOKE INSERT, UPDATE, DELETE ON `testdb`.* FROM 'test_write';

-- 刪除角色
DROP ROLE 'test_read', 'test_write';

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章