15 用户管理

如果我们只能使用root用户,这样存在安全隐患。这时,就需要使用mysql的用户管理

张三只能操纵mytest这个库,李四只能操纵msg这个库,如果给他们root账户,就可以操纵所有库,风险太大

用户

用户信息

用户都存储在系统数据库mysql的user表中

mysql> use mysql;

Database changed

mysql> select host,user,authentication_string from user;

--可以通过desc user初步查看一下表结构

字段解释:

host:表示这个用户可以从哪个主机登录,如果是localhost,表示只能从本机登录

user:用户名

authentication_string:用户密码通过password函数加密过后的

*_priv:用户拥有的权限

创建用户

语法:

create user '用户名'@'登录主机/ip' identified by '密码';

案例:

mysql> create user 'tmp'@'localhost' identified by '12345678';

Query OK, 0 rows affected (0.06 sec)

mysql> select user,host,authentication_string from user;

-- 此时便可以使用新账号新密码进行登陆

--备注:可能实际在设置密码的时候,因为mysql本身的认证等级比较高,一些简单的密码无法设置,会爆出如下报错:-- ERROR 1819 (HY000): Your password does not satisfy the current policy requirements-- 解决方案:https://blog.csdn.net/zhanaolu4821/article/details/93622812--查看密码设置相关要求:SHOW VARIABLES LIKE 'validate_password%';

关于新增用户这里,需要大家注意,不要轻易添加一个可以从任意地方登陆的user。

删除用户

语法:

drop user '用户名'@'主机名'

示例:

select user,host,authentication_string from user;

mysql> drop user whb; --尝试删除

ERROR 1396 (HY000): Operation DROP USER failed for 'temp'@'%' -- <= 直接给个用户名,不能删除,它

默认是%,表示所有地方可以登陆的用户

mysql> drop user 'temp'@'localhost'; --删除用户

Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,authentication_string from user;

修改用户密码

语法:

  • 自己改自己密码

set password=password('新的密码');

--自己下来试试

  • root用户修改指定用户的密码

set password for '用户名'@'主机名'=password('新的密码');
mysql> select host,user, authentication_string from user;

mysql> set password for 'whb'@'localhost'=password('87654321');

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select host,user, authentication_string from user;

数据库的权限

mysql数据库提供的权限列表:

给用户权限

刚创建的用户没有任何权限,需要给用户授权

grant 权限列表 on 库.对象名 to '用户名'@'登录位置' indentified by '密码'

说明:

权限列表,多个权限用逗号分开

grant select on ...

grant select, delete, create on ...

grant all privileges on ... -- 表示赋予该用户在该对象上的所有权限

*.*:代表本系统中的所有数据库的所有对象(表,视图,存储过程等)

库.*:表示某个数据库中的所有数据对象(表,视图,存储过程等)

identified by可选,如果用户存在,赋予权限的同时修改密码,如果不存在就创建用户

--使用root账号 --终端A

mysql> show databases;

mysql> use test;

Database changed

mysql> show tables;

--给用户tmp赋予test数据库下所有文件的select权限

mysql> grant select on test.* to 'tmp'@'localhost';

Query OK, 0 rows affected (0.01 sec)

--使用tmp账号

--终端B

mysql> show databases;

--暂停等root用户给tmp赋完权之后,在查看

mysql> show databases;

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> show tables;

mysql> select * from account;

--没有删除权限

mysql> delete from account;

ERROR 1142 (42000): DELETE command denied to user 'whb'@'localhost' for table 'account'

备注:特定用户现有查看权限

mysql> show grants for 'tmp'@'%';

mysql> show grants for 'root'@'%';

注意:如果发现赋权限后,没有生效,执行如下指令:

flush privileges;

回收权限

语法:

revoke 权限列表 on 库.对象名 from '用户名'@'登录位置';

示例:

-- 回收tmp对test数据库的所有权限 --root身份,终端A

mysql> revoke all on test.* from 'tmp'@'localhost';

Query OK, 0 rows affected (0.00 sec) --tmp身份,终端B

mysql> show databases;

mysql> show databases;

相关推荐
Csvn21 分钟前
📊 SQL 入门 Day 8:集合操作 — 用 SQL 做数学里的"并交差"
后端·sql
Miao121311 小时前
某海外住宿平台如何在大规模场景下实现指标一致性:Minerva 指标平台实践
大数据·数据库·人工智能
CodexDave1 小时前
MySQL事务隔离级别与MVCC机制解析
前端·数据库·mysql·nginx·性能优化·负载均衡
碎光拾影2 小时前
ARM交叉工具链各工具作用及IMX6ULL平台LED+蜂鸣器裸机程序实现
java·开发语言·数据库
曾阿伦2 小时前
MongoDB 查询语句备忘手册
数据库·mongodb
一叶龙洲2 小时前
wslg打开Ubuntu24.04默认打开图形界面
linux·服务器·数据库·ubuntu
CCPC不拿奖不改名2 小时前
大模型推理架构与开源生态知识整理
数据库·windows·python·架构·langchain·开源·github
爬也要爬着前进3 小时前
redis主从搭建
数据库·redis·缓存
@Mike@4 小时前
02-数据库学习笔记(SQL引擎)
数据库·笔记·学习
踏月的造梦星球5 小时前
达梦数据库执行计划与性能分析入门:EXPLAIN、AUTOTRACE 与 ET
服务器·数据库·oracle