mysql8.0以上添加用户与授权


mysql8+有新的安全要求,不能像之前的版本那样一次性创建用户并授权需要先创建用户,再进行授权操作

创建用户:

use mysql;  //mysql用户表在 mysql.user表中。

create user 'username'@'localhost' identified by 'pass123'; //创建用户命令 create user 'username'@'%' identified by 'pass123'; 

username:自己创建的用户名;

'%' :哪台主机上可以登录mysql,%是通配符指的是任意IP,也可以指定具体的IP,或者localhost代表本机才可以登录。

查看新创建用户username的权限

select * from user where user = 'username'\G; 可以查看新创建的用户;

show grants for username@localhost; //show grants for 'username'@'localhost';

mysql> show grants for 'username'@'localhost';
+----------------------------------------------+
| Grants for username@localhost |
+----------------------------------------------+
| GRANT USAGE ON *.* TO `username`@`localhost` |
+----------------------------------------------+

为username@localhost用户赋予超级用户权限:

grant all privileges on *.* to 'username'@'localhost' with grant option;

grant:授权

all privileges:所有的权限

on *.*:在哪个数据库的那个表

to username@localhost:对哪个用户的哪个主机

with grant option: 是不是 将username用户自己本身的权限赋给其他账户

用 grant给一些用户添加权限:

普通用户权限添加如下:

grant usage,select,insert,update,delete,create temporary tables,execute on jikedb.* to username@localhost; //此时没有with grant option 表示不给其他用户赋权限

flush privileges;

usage:无权限,当你想创建一个没有权限的用户时候,指定usage

show:的权限

view:视图的权限(mysql8.0+赋权限出错)ERROR 3619 (HY000): Illegal privilege level specified for VIEW

create temporary tables:创建临时表的权限

excute:执行的权限

收回权限的命令:

revoke delete on jikedb.* from username@localhost; //意思是收回username@localhost下jikedb库所有的表的删除操作

新创建的用户username@localhost 要想使用,登录后需要修改密码如下:

 

 

删除用户:

drop user username@localhost; //username,localhost加不加引号都可以


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM