MySQL授权管理
前言
安装MySQL后,给数据库分配用户和授权。
一、初始化后数据库配置
mysql> use mysql;
mysql> desc user;
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root"; //为root添加远程连接的能力
mysql> update user set Password = password('123456') where User='root'; //设置root用户密码
mysql> select Host,User,Password from user where User='root';
mysql> flush privileges;
mysql> exit
配置应用用户
查看权限信息
(1)查看mysql的所有用户及其权限:
select * from mysql.user\G;
(2)查看当前mysql用户权限:
show grants;
(3)查看某个用户的权限:
show grants for username@host; #用户名@主机
show grants for test@localhost;
(4)查看所有用户
select host,user from mysql.user;
配置权限信息
(1)Mysql用户创建:
create user '用户名'@'主机' identified by '密码';
create user 'test'@'%' identified by '123456';
flush privileges; #创建完用户及权限后,需要使用该命令刷新权限
(2) Mysql用户删除:
drop user '用户名'@'主机';
(3) Mysql用户权限授予:
grant 权限列表 on 数据库名.数据表名 to ‘用户名’@’主机’ identified by ‘密码’ with grant option; #数据库名.*代表库下所有的表
grant all privileges on *.* to 'test'@'%' identified by 'test098' with grant option;
flush privileges;
可使用“*”表示所有数据库或所有数据表,“%”表示任何主机地址。
(4)Mysql用户权限回收:
revoke 权限列表 on 数据库名.数据表名 from 用户名@主机;
revoke drop on *.* from test@localhost;#回收drop权限
(5)对账户重命名:
rename user '旧用户名'@'旧主机' to '新用户名'@'新主机';
rename user 'test'@'localhost' to 'test1'@'localhost';
(6)Mysql用户密码修改:
- 使用set password命令
set password for '用户名'@'主机' = password('新密码'); #需要执行刷新权限
flush privileges;
set password for 'test'@'localhost' = password('123456');
flush privileges;
- 使用set password命令
grant select on 数据库名.数据表名 to 用户名@主机 identified by '新密码' with grant option;
grant select on test.user to test@localhost identified by '111111' with grant option;
- 运行mysqladmin脚本文件。
(1)用户尚无密码:
mysqladmin -u 用户名 password 新密码;
(2)用户已有密码:
mysqladmin -u 用户名 -p password 新密码;
总结
MySQL权限管理相关操作:
创建用户,删除用户,用户授权;
权限操作后要刷新 才生效