MySQL 8.x 创建用户并授权

一、登入 root 用户,一般只有 root 用户具有最高权限

1、登入

mysql -uroot -proot

MySQL 8.x 创建用户并授权_第1张图片

2、切换至 mysql 数据库

use mysql

MySQL 8.x 创建用户并授权_第2张图片

3、创建用户

create user 'hkl'@'%' identified by 'test@1234';

创建 hkl 用户,%为不限IP登录,密码为test@1234

4、刷新权限,每次权限变更都要刷新特权

flush privileges;

5、连接测试
MySQL 8.x 创建用户并授权_第3张图片

 

发现只有一个 information_schema 数据库,所以还需要为该用户授权 

二、授权

1、给 hkl 用户授予所有权限 all privileges

语法:grant 权限名 on 数据库.数据表 to '用户名'@'访问权限' with grant option;
示例1:grant all privileges on *.* to 'root'@'%' with grant option;
示例2:grant select,insert,update on test_db_one.* to 'hkl'@'%' with grant option;
示例3:grant select,insert,update,delete,create,drop,index,alter on nacos.* to 'hkl'@'%' with grant option;

grant all privileges on test_db_one.* to 'hkl'@'%' with grant option;

 说明:
(1):all privileges 所有权限
(2):这里的 all privileges 可以替换为 select, update, delete, insert, drop, create等
(3):*.* 左右星号分别为数据库和数据表
(4):''@'' 艾特符号左右分别代表用户名和访问权限,%为不限制远程IP访问
(5):with grant option 表示该用户可以给其他用户赋权,但是不能超过该用户的权限

2、查看用户权限,show grants for 'hkl'@'%';
MySQL 8.x 创建用户并授权_第4张图片

三、撤销用户的权限

1、撤销

revoke all privileges on test_db_one.* from 'hkl'@'%';
或
revoke all privileges,grant option from 'hkl'@'%';

撤销用户 hkl 对数据库 test_db_one 的所有操作权限

2、 删除用户

drop user 'hkl'@'%';

你可能感兴趣的:(MySQL,Oracle,PostgreSQL,数据库,mysql)