用户权限管理主要有以下作用:
1. 可以限制用户访问哪些库、哪些表
2. 可以限制用户对哪些表执行SELECT、CREATE、DELETE、DELETE、ALTER等操作
3. 可以限制用户登录的IP或域名
4. 可以限制用户自己的权限是否可以授权给别的用户
一、用户授权
grant all privileges on *.* to 'yangxin'@'%' identified by 'yangxin123456' with grant option;
- all privileges:表示将所有权限授予给用户。也可指定具体的权限,如:SELECT、CREATE、DROP等。
- on:表示这些权限对哪些数据库和表生效,格式:数据库名.表名,这里写“*”表示所有数据库,所有表。如果我要指定将权限应用到test库的user表中,可以这么写:test.user
- to:将权限授予哪个用户。格式:”用户名”@”登录IP或域名”。%表示没有限制,在任何主机都可以登录。比如:”yangxin”@”192.168.0.%”,表示yangxin这个用户只能在192.168.0IP段登录
- identified by:指定用户的登录密码
- with grant option:表示允许用户将自己的权限授权给其它用户【不允许,则不需要加此命令】
二、刷新权限
对用户做了权限变更之后,一定记得重新加载一下权限,将权限信息从内存中写入数据库
flush privileges;
三、查看用户权限
show grants for 'yangxin'@'localhost';
四、回收权限
删除yangxin这个用户的create权限,该用户将不能创建数据库和表。
revoke create on *.* from 'yangxin@localhost';
flush privileges;
五、删除用户
drop user 'yangxin'@'localhost';
六、用户重命名
rename user 'test3'@'%' to 'test1'@'%';
七、修改密码
# mysql5.7之前
update user set password=password('123456') where user='root';
# mysql5.7之后
update user set authentication_string=password('123456') where user='root';
flush privileges;
八、忘记密码
1> 添加登录跳过权限检查配置
修改my.cnf,在mysqld配置节点添加skip-grant-tables配置
[mysqld]
skip-grant-tables
2> 重新启动mysql服务