【MySQL】MySQL用户管理

文章目录

一、用户

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

1.用户信息

我们安装mysql之后,会自动创建一个mysql的数据库。MySQL中的用户,都存储在系统数据库mysql的user表中

我们可以查询如下信息:

mysql 复制代码
select host,user,authentication_string from user;

mysql> select host,user,authentication_string from user;
+-----------+---------------+-------------------------------------------+
| host | user | authentication_string |
+-----------+---------------+-------------------------------------------+
| localhost | root | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+---------------+-------------------------------------------+
--可以通过desc user初步查看一下表结构

字段解释:

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

user: 用户名

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

*_priv: 用户拥有的权限

2.创建用户

语法:

mysql 复制代码
create user '用户名'@'登陆主机/ip' identified by '密码';

案例:

mysql 复制代码
mysql> create user 'hdp'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,authentication_string from user;
+---------------+-----------+-------------------------------------------+
| user          | host      | authentication_string                     |
+---------------+-----------+-------------------------------------------+
| root          | localhost | *5ADB87D1C6448A109DCC4D61C8C6DD5637B0683B |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| hdp           | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+---------------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)

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

备注:可能实际在设置密码的时候,因为mysql本身的认证等级比较高,一些简单的密码无法设置,会爆出如下报错:

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

解决方案:https://blog.csdn.net/zhanaolu4821/article/details/93622812

查看密码设置相关要求:

mysql 复制代码
show variables like 'validate_password';
SHOW VARIABLES LIKE 'validate_password%';

登录主机设置为%的时候表示可以在任意主机登录

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

3.删除用户

语法:

mysql 复制代码
drop user '用户名'@'主机名'
mysql 复制代码
mysql> select user,host,authentication_string from user;
+---------------+-----------+-------------------------------------------+
| user          | host      | authentication_string                     |
+---------------+-----------+-------------------------------------------+
| root          | localhost | *5ADB87D1C6448A109DCC4D61C8C6DD5637B0683B |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| hdp           | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+---------------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> drop user hdp;
ERROR 1396 (HY000): Operation DROP USER failed for 'hdp'@'%' -直接给个用户名,不能删除
mysql> drop user 'hdp'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,authentication_string from user;
+---------------+-----------+-------------------------------------------+
| user          | host      | authentication_string                     |
+---------------+-----------+-------------------------------------------+
| root          | localhost | *5ADB87D1C6448A109DCC4D61C8C6DD5637B0683B |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+---------------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)

直接给个用户名,不能进行删除,而是应该使用用户名+主机名

mysql 复制代码
drop user 'hdp'@'localhost';

4.修改用户密码

自己改自己密码

mysql 复制代码
set password=password('新的密码');

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

mysql 复制代码
set password for '用户名'@'主机名'=password('新的密码');

示例:

mysql 复制代码
mysql> create user 'hdp'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,authentication_string from user;
+---------------+-----------+-------------------------------------------+
| user          | host      | authentication_string                     |
+---------------+-----------+-------------------------------------------+
| root          | localhost | *5ADB87D1C6448A109DCC4D61C8C6DD5637B0683B |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| hdp           | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+---------------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)

mysql> set password for 'hdp'@'localhost'=password('654321');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select user,host,authentication_string from user;
+---------------+-----------+-------------------------------------------+
| user          | host      | authentication_string                     |
+---------------+-----------+-------------------------------------------+
| root          | localhost | *5ADB87D1C6448A109DCC4D61C8C6DD5637B0683B |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| mysql.sys     | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| hdp           | localhost | *2A032F7C5BA932872F0F045E0CF6B53CF702F2C5 |
+---------------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)

二、数据库的权限

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

1.给用户授权

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

语法:

mysql 复制代码
grant 权限列表 on 库.对象名 to '用户名'@'登陆位置' [identified by '密码']

说明:

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

mysql 复制代码
grant select on ...
grant select, delete, create on ....
grant all [privileges] on ... -- 表示赋予该用户在该对象上的所有权限

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

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

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

使用root账号

mysql 复制代码
mysql> show databases;
+----------------------+
| Database             |
+----------------------+
| information_schema   |
| README_TO_RECOVER_A  |
| README_TO_RECOVER_SZ |
| db_test              |
| mysql                |
| mysql_learning       |
| performance_schema   |
| scott                |
| sys                  |
+----------------------+
9 rows in set (0.00 sec)

mysql> use scott;
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;
+-----------------+
| Tables_in_scott |
+-----------------+
| dept            |
| emp             |
| salgrade        |
+-----------------+
3 rows in set (0.00 sec)

给用户hdp赋予scott数据库下所有文件的select权限

mysql 复制代码
mysql> grant select on scott.* to 'hdp'@'localhost';
Query OK, 0 rows affected (0.00 sec)

使用hdp账号

mysql 复制代码
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| scott              |
+--------------------+
2 rows in set (0.00 sec)

mysql> use scott;
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> select * from dept;
+--------+------------+----------+
| deptno | dname      | loc      |
+--------+------------+----------+
|     10 | ACCOUNTING | NEW YORK |
|     20 | RESEARCH   | DALLAS   |
|     30 | SALES      | CHICAGO  |
|     40 | OPERATIONS | BOSTON   |
+--------+------------+----------+
4 rows in set (0.02 sec)

没有删除权限

mysql 复制代码
mysql> delete from dept;
ERROR 1142 (42000): DELETE command denied to user 'hdp'@'localhost' for table 'dept'

特定用户现有查看权限

mysql 复制代码
how grants for 'hdp'@'localhost';
mysql 复制代码
mysql> show grants for 'hdp'@'localhost';
+------------------------------------------------+
| Grants for hdp@localhost                       |
+------------------------------------------------+
| GRANT USAGE ON *.* TO 'hdp'@'localhost'        |
| GRANT SELECT ON `scott`.* TO 'hdp'@'localhost' |
+------------------------------------------------+
2 rows in set (0.00 sec)

mysql> show grants for 'root'@'localhost';
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)

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

mysql 复制代码
flush privileges;

2.回收权限

语法:

mysql 复制代码
revoke 权限列表 on 库.对象名 from '用户名'@'登陆位置';

root身份回收hdp对scott数据库的所有权限

mysql 复制代码
mysql> revoke all on scott.* from 'hdp'@'localhost';
Query OK, 0 rows affected (0.00 sec)

hdp身份

mysql 复制代码
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
相关推荐
拭心4 小时前
Google 提供的 Android 端上大模型组件:MediaPipe LLM 介绍
android
White_Mountain5 小时前
在Ubuntu中配置mysql,并允许外部访问数据库
数据库·mysql·ubuntu
老王笔记5 小时前
GTID下复制问题和解决
mysql
带电的小王7 小时前
WhisperKit: Android 端测试 Whisper -- Android手机(Qualcomm GPU)部署音频大模型
android·智能手机·whisper·qualcomm
Lojarro7 小时前
【Spring】Spring框架之-AOP
java·mysql·spring
梦想平凡7 小时前
PHP 微信棋牌开发全解析:高级教程
android·数据库·oracle
TianyaOAO7 小时前
mysql的事务控制和数据库的备份和恢复
数据库·mysql
Ewen Seong7 小时前
mysql系列5—Innodb的缓存
数据库·mysql·缓存
元争栈道7 小时前
webview和H5来实现的android短视频(短剧)音视频播放依赖控件
android·音视频
阿甘知识库8 小时前
宝塔面板跨服务器数据同步教程:双机备份零停机
android·运维·服务器·备份·同步·宝塔面板·建站