一、创建用户
1、查看所有用户和主机名
select user,host from mysql.user
2、创建用户
create user 用户名@'主机名' identified by '密码'
3、修改用户名和主机
rename user '用户名'@'主机名' to '用户名'@'主机名'
4、删除用户
drop user '用户名'@'主机名'
5、创建的用户设置密码有效期为30天
create user 用户名@'主机名' identified by '密码' password expire interval 30 day
6、用户密码立即失效(永不过期)
alter user 用户名@'主机名' password expire (never)
二、授权管理
1、grant 某个权限 on 数据库.表名 to ‘用户名’@‘主机名’ , localhost 为本机,% 为任意主机均可连接,如:
grant select on mysql.test to 'root'@'localhost'
2、给用户授予全部权限(含再授予其他用户的权限)
grant all privileges on *.* to '用户名'@'主机名' (with grant option)
3、查看某用户的所有权限
show grants for '用户名'@'主机名'
4、撤销用户的所有权限
revoke all privileges , grant option from '用户名'@'主机名'
5、 撤销用户的某个权限
revoke 某个权限 on 数据库.表名 from '用户名'@'主机名'
6、通过角色授权
(1)创建角色
create role 角色名
(2)给角色授予权限
grant 某个权限 on 数据库.表名 to 角色
grant 某个权限 on *.* to 角色
(3)给用户授予角色权限
grant 角色 to '用户名'@'主机名'