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用户密码管理
[mysqld
]
default_password_lifetime
=180
CREATE USER 'test'@'%' PASSWORD EXPIRE
DEFAULT;
[mysqld
]
password_history
=6
password_reuse_interval
=365
MySQL8.0角色管理
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'
;
转载请注明原文地址: https://mac.8miu.com/read-515914.html