一、登录 root 用户,一般只有 root 用户具有最高权限
1、登入
mysql -uroot -proot
二、授权等操作
1、切换库
use mysql;
2、查询用户
select User,Host from user;
3、创建用户xxx,%为不限IP登录,密码为xxx123
create user 'xxx'@'%' identified by 'xxx123';
4、更新密码
set password = 'xxx123';
或
alter user xxx@'localhost' identified with mysql_native_password by 'xxx123';
5、授权,将root用户授予最高权限,无所不能
grant all privileges on *.* to 'root'@'localhost' with grant option;
或
grant all privileges on *.* to 'root'@'%' with grant option;
6、授权特定权限,示例(为xxx用户授予所有库和表的 CURD 权限)
grant select, update, delete, insert on *.* to 'xxx'@'%' with grant option;
7、开放所有IP访问xxx用户权
update user set host='%' where user='xxx' and host='localhost' limit 1;
8、查看xxx用户的授权信息
show grants for 'xxx'@'%';
9、撤销权限
#撤销xxx用户的所有权限
revoke all privileges from xxx;
#撤销用户xxx对数据库 db_name 的所有操作权限
revoke all privileges on db_name.* from 'xxx'@'%';
10、删除xxx用户
drop user 'xxx'@'%';
11、刷新授权信息,使之生效
flush privileges;
说明:
(1):all privileges 所有权限
(2):这里的 all privileges 可以替换为 select, update, delete, insert, drop, create 等
(3):*.* 左右星号分别为数据库和数据表
(4):''@'' 艾特符号左右分别代表(用户名)和(访问权限),%为不限制IP访问
(5):with grant option 表示该用户可以给其他用户授权,但是不能超过该用户的权限