MySQL8.0 新特性
创建用户及授权
MySQL8.0 创建用户和用户授权的命令需要分开执行:
--创建
create user 'andy'@'%' identified by 'andy@2019';
--授权
grant all privileges on *.* to 'andy'@'%'
在MySQL5.7中只需要使用 grant all 语句就可以创建用户及授权:
grant all privileges on *.* to 'andy'@'%' identified by 'andy@2019';
认证插件更新
MySQL 8.0 中默认的身份认证插件是 caching_sha2_password,替代了之前的 mysql_native_passsword
查看认证插件:
show variables like 'default_authentication_plugin%'
或
select user,host,plugin from mysql.user
如果在mysql8.0 中仍然想使用原来认证插件
方式一:
可以在配置文件中进行修改
查看配置文件:
more /etc/my.cnf
将配置文件中被注释掉的 default_authentication_plugin=mysql_native_password 放开注释,重启服务即可
方式二:
alter user 'andy'@'%' identified with mysql_native_password by 'andy@2019'
密码管理
MySQL8.0 允许限制重复使用之前使用过的密码
password_history = 3 表示最近使用过的密码不能重复使用三次(默认是0)
password_reuse_interval = 90 表示最近90天内不用使用重复使用过的密码(默认是0)
password_require_current = ON 表示修改用户密码时,需要提供当前用户的密码(默认是OFF)
查看相关参数 :
show variables like 'password%'
设置密码重用
vi /etc/my.cnf
在配置文件中配置上面的即可参数
方式一: 设置全局密码重用策略
--persist 永久,通过persist设置之后服务重启也是存在的
set persist password_history=6
-- 查看配置
--mysql8.0是通过自动增加配置文件来实现持久化
more /var/lib/mysql/mysql-auto.cnf
方式二: 根据某个不同的用户设置自定义的密码重用策略
alter user 'andy'@'%' password history 5
select user,host,Password_reuse_history from mysql.user
desc mysql.password_history
--修改密码
alter useruser() identified by 'andy@2020' replace 'andy@2022'
角色管理(MySQL8.0新增的功能)
--创建角色
create role 'write_role'
--授权
grant insert,update,delete on testdb.* to 'write_role'
--创建用户
create user 'user1' identified by 'andy@123'
grant select on testdb.* to 'write_role'
--启用角色
set role 'write_role'
--设置默认的角色
set default role 'write_role' to 'user1'
select * from mysql