mysql8创建、删除用户以及授权、消权操作详解
作者:火红_ 时间:2024-01-13 22:46:34
1、登录mysql
mysql -uroot -p
2、先查询都有哪些用户
select host,user from mysql.user;
红色箭头是主管理员,黄色箭头是mysql系统自带的,不要动它。蓝色箭头是子用户,这个是我以前匹配的,现在删掉,我们重新来。
3、删掉用户:
drop user '用户名'@'主机名';
drop user 'wyy'@'192.168.0.105';
4、创建用户
create user '用户名'@'允许那个主机链接' identified by '密码';
create user 'wyy'@'192.168.0.105' identified by 'wyy18222';
只允许192.168.0.105的主机链接
备注:
Mysql8.0 默认采用 caching-sha2-password 加密,有可能旧的客户端不支持,可改为 mysql_native_password;
create user 'test'@'%' identified with mysql_native_password BY '密码';
百分号%;表示任何ip地址都可以链接
create user ‘wyy’@‘192.168.0.105’ identified by ‘wyy18222’;这个是只能192.168.0.105的链接。
5、修改密码
Alter user '用户名'@'主机名' identified by '新密码';
alter user 'wyy'@'192.168.0.105' identified by '123';
6、授权
给用户授权所有权限
grant all privileges on *.* to '用户名'@'主机名' with grant option;
grant all privileges on *.* to 'wyy'@'192.168.0.105' with grant option;
grant:授权、授予
privileges:权限,特权
第一个星号:表示所有数据库
第二个星号:表示所有表
with grant option:表示该用户可以给其他用户赋予权限,但不能超过该用户的权限。这个不加也行。
例如:如果wyy只有select、update权限,没有insert、delete权限,给另一个用户授权时,只能授予它select、update权限,不能授予insert、delete权限。
给用户授权个别权限
all privileges 可换成 select,update,insert,delete,drop,create 等操作
grant select,insert,update,delete on *.* to '用户名'@'主机名';
给用户授权指定权限
给用户授予指定的数据库权限
grant all privileges on 数据库 . * to 'wyy'@'192.168.0.105';
grant all privileges on xrs . * to 'wyy'@'192.168.0.105';
将数据库名为xrs的所有权限赋予wyy
给用户授予指定的表权限
grant all privileges on 数据库 . 指定表名 to 'wyy'@'192.168.0.105';
将某个数据库下的某个表的权限赋予wyy
注意:
网上有的直接创建并赋权:
grant all privileges * . * to ‘要创建的用户’@‘localhost’ identified by ‘自定义密码’;
我在mysql8试了不行(8版本以下还没试过),要先创建用户再进行赋权,不能同时进行
7、刷新权限
flush privileges;
新设置用户或更改密码后需用flush privileges刷新MySQL的系统权限相关表,
否则会出现拒绝访问
还有一种方法,就是重新启动mysql服务器,来使新设置生效。­
8、查看用户授权
show grants for 'wyy'@'192.168.0.105';
9、撤销用户授权(销权)
revoke all privileges on *.* from 'wyy'@'192.168.0.105';
用户有什么权限就撤什么权限
补充:mysql8.0 创建用户和授权用户遇到的坑
创建用户:
create user userName@localhost identified with mysql_native_password by 'password';(with mysql_native_password 如果没有这个,Navicat将无法登陆提示:2059 - authentication plugin...错误,因为Navicat不支持最新数据库默认的加密方式);
授权用户:
GRANT
ALL PRIVILEGES
ON databaseName.*
TO userName@'ip';(注意这点跟以往数据库都不一样,无需后面跟着IDENTIFIED BY 'password';否则将提示ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 'password'' at line 5)
来源:https://blog.csdn.net/qq_31676483/article/details/123416502
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 星号(*)的多种用途
python groupby函数实现分组后选取最值
![](https://img.aspxhome.com/file/2023/7/69267_0s.png)
python实现视频压缩功能
![](https://img.aspxhome.com/file/2023/0/90850_0s.png)
如何利用Pyecharts可视化微信好友
![](https://img.aspxhome.com/file/2023/0/86450_0s.gif)
ASP GetRef 函数指针试探
mysql中的日期相减的天数函数
vue element-ui实现动态面包屑导航
MySQL实现字段或字符串拼接的三种方式总结
![](https://img.aspxhome.com/file/2023/4/124064_0s.png)
Django程序的优化技巧
PyQt5的PyQtGraph实践系列3之实时数据更新绘制图形
![](https://img.aspxhome.com/file/2023/5/87565_0s.png)
python实现自动化之文件合并
![](https://img.aspxhome.com/file/2023/3/115753_0s.jpg)
Oracle 随机数
Django处理多用户类型的方法介绍
![](https://img.aspxhome.com/file/2023/3/85033_0s.png)
oracle 数据库连接分析
深入解读Python解析XML的几种方式
![](https://img.aspxhome.com/file/2023/6/114786_0s.jpg)
使用python matplotlib contour画等高线图的详细过程讲解
![](https://img.aspxhome.com/file/2023/5/75245_0s.png)
Python Flask框架实现Proteus仿真Arduino与网页数据交互
![](https://img.aspxhome.com/file/2023/0/89080_0s.png)
帮你六步改善SQL Server安全规划全攻略
mysql unix准换时间格式查找指定日期数据代码
PyQt实现界面翻转切换效果
![](https://img.aspxhome.com/file/2023/3/117933_0s.gif)