当前位置:网站首页>Mysql-怎么添加用户和设置权限?
Mysql-怎么添加用户和设置权限?
2022-07-28 06:33:00 【怪 咖@】
DCL英文全称是Data Control Language(数据控制语言),用来管理数据库用户、控制数据库的访问权限。
一、管理用户
用户相关命令:
查询用户:select * from mysql.user;
密码也存在了这张表,但是是加密过的,如果忘记密码了,可以通过配置设置成免密登录,然后再进行修改密码。

其中 Host代表当前用户访问的主机, 如果为localhost, 仅代表只能够在当前本机访问,是不可以远程访问的。 User代表的是访问该数据库的用户名。在MySQL中需要通过Host和User来唯一标识一个用户。主机名可以使用 % ,代表的就是任何一个主机都能访问。
往往linux当中安装mysql,然后使用 其他主机的客户端 连mysql连不上,就是因为Host配置的是localhost这个原因导致的!
- 创建用户:
CREATE USER '用户名'@'主机名' IDENTIFIED BY '密码'; - 修改用户密码:
ALTER USER '用户名'@'主机名' IDENTIFIED WITH mysql_native_password BY '新密码' ; - 删除用户:
DROP USER '用户名'@'主机名' ;
注意事项:
- 在MySQL中需要通过
用户名@主机名的方式,来唯一标识一个用户。 - 主机名可以使用 % 通配。
命令使用示例:
#创建用户aaa, 只能够在当前主机localhost访问, 密码123456;
create user 'aaa'@'localhost' identified by '123456';
#创建用户bbb, 可以在任意主机访问该数据库, 密码123456;
create user 'bbb'@'%' identified by '123456';
#修改用户bbb的访问密码为1234;
alter user 'bbb'@'%' identified with mysql_native_password by '1234';
#删除 [email protected] 用户
drop user 'aaa'@'localhost';
二、权限控制
MySQL中定义了很多种权限,但是常用的就以下几种:

上述只是简单罗列了常见的几种权限描述,其他权限描述及含义,可以直接参考官网:
https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html
命令:
- 查询权限:
SHOW GRANTS FOR '用户名'@'主机名' ; - 授予权限:
GRANT 权限列表 ON 数据库名.表名 TO '用户名'@'主机名'; - 撤销权限:
REVOKE 权限列表 ON 数据库名.表名 FROM '用户名'@'主机名';
注意事项:
- 多个权限之间,使用逗号分隔
- 授权时,
数据库名和表名可以使用 * 进行通配,代表所有。
命令使用示例:
#查询 'bbb'@'%' 用户的权限
show grants for 'bbb'@'%';
#授予 'bbb'@'%' 用户itcast数据库所有表的所有操作权限
grant all on itcast.* to 'bbb'@'%';
#撤销 'bbb'@'%' 用户的itcast数据库的所有权限
revoke all on itcast.* from 'bbb'@'%';
MySQL用户数据和权限有修改后,希望在"不重启MySQL服务"的情况下直接生效,那么就需要执行这个命令。通常是在修改ROOT帐号的设置后,怕重启后无法再登录进来,那么直接flush之后就可以看权限设置是否生效。而不必冒太大风险。
flush privileges;
边栏推荐
- Protobuf basic grammar summary
- One key switch circuit
- Exception handling in SQL Server
- CAT1 4G+以太网开发板232数据通过4G模块TCP发到服务器
- protobuf 基本语法总结
- 二维数组及操作
- uniapp上下滑屏切换支持视频和图片轮播实现,类似抖音效果
- [book club issue 13] Chapter 2 notes on the packaging format and coding format of video files
- MCU IO port controls 12V voltage on and off, MOS and triode circuit
- [event registration] cloud native technology exchange meetup, see you in Guangzhou on August 6
猜你喜欢

MCU IO port controls 12V voltage on and off, MOS and triode circuit

【活动报名】云原生技术交流 Meetup,8 月 6 日广州见

“蔚来杯“2022牛客暑期多校训练营2补题记录(DGHJKL)

QT uses semaphores to control threads (qsemaphore)

Protobuf basic grammar summary

Find out whether the number exists from the matrix

MPLS -- multi protocol label switching technology
![[Qt5] a method of multi window parameter transmission (using custom signal slot) and case code download](/img/6d/870add6179f0e3a2f9b719f79594f3.png)
[Qt5] a method of multi window parameter transmission (using custom signal slot) and case code download

Some experience of gd32 using Hal Library of ST and Gd official library

EMC EMI磁珠的特性
随机推荐
There are two Kafka topics that need to write data intact to MySQL King through Flink. Scheme 1: write two f's
QT 怎么删除布局里的所有控件?
c语言中函数的介绍(血书20000字!!!!)
Technology sharing | common proxy tools for interface testing
opencv+paddle orc 识别图片提取表格信息
PostgreSQL is the world's most advanced open source relational database
Rk3568 development board installation system startup
Record a MYCAT connection and solve the problems of communications link failure
DCL singleton mode
Is the salary of test / development programmers unbalanced? Busy life, all kinds of job hopping
Five screens, VR, projection, "Wei Xiaoli" rolled up on the intelligent cockpit
Prescan quick start to master the track editing path of Lecture 16
JS card cascading style image switching JS special effect
XSS知识点和20字符短域名绕过
Deluxe H5 Tetris game source code
“蔚来杯“2022牛客暑期多校训练营2补题记录(DGHJKL)
[reprint] man Rsync translation (Chinese Manual of Rsync command)
How to build the protection system of class protection technology of 2022 series of ISO compliance (Part I)
Some experience of gd32 using Hal Library of ST and Gd official library
XSS knowledge points and 20 character short domain name bypass