数据库用户管理

数据库用户管理

1.创建用户

MySQL在安装是,会默认创建一个名位root的用户,该用户拥有超级权限,可以控制整个MySQL服务器。

在对MySQL的日常管理和操作中,通常创建一些具有适当权限的用户,尽可能的不用或少用root登录系统,以此来确保数据的安全访问。

可以使用create use语句创建用户,并设置相应密码:

sql 复制代码
create user 用户 [indentified by [password] 'password']

参数说明:

  1. 用户

指定创建用户账号,格式为 user_name'@'host_name。这里的user_name是用户名,host_name为主机名,即用户连接 MySQL 时所用主机的名字。如果在创建的过程中,只给出了用户名,而没

指定主机名,那么主机名默认为"%",表示一组主机,即对所有主机开放权限。

  1. IDENTIFIED BY子句

用于指定用户密码。新用户可以没有初始密码,若该用户不设密码,可省略此子句。

  1. PASSWORD 'password'

PASSWORD 表示使用哈希值设置密码,该参数可选。如果密码是一个普通的字符串,则不需要使用 PASSWORD 关键字。'password' 表示用户登录时使用的密码,需要用单引号括起来。

⚠️使用 CREATE USER 语句时应注意以下几点:

  • CREATE USER 语句可以不指定初始密码。但是从安全的角度来说,不推荐这种做法。

  • 使用 CREATE USER 语句必须拥有 mysql 数据库的 INSERT 权限或全局 CREATE USER 权限。

  • 使用 CREATE USER 语句创建一个用户后,MySQL 会在 mysql 数据库的 user 表中添加一条新记录。

  • CREATE USER 语句可以同时创建多个用户,多个用户用逗号隔开。

新创建的用户拥有的权限很少,它们只能执行不需要权限的操作。如登录 MySQL、使用 SHOW 语句查询所有存储引擎和字符集的列表等。如果两个用户的用户名相同,但主机名不同,MySQL 会将它们视为两个用户,并允许为这两个用户分配不同的权限集合。

案例1:

创建一个用户,用户名是test1,密码是test1,主机名是localhost。

sql 复制代码
mysql> create user 'test1'@'locahost' identified by 'test1';
Query OK, 0 rows affected (0.01 sec)

mysql> select user ,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| test1             | locahost  |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

C:\Users\k>mysql -utest1 -p
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.4.3 MySQL Community Server - GPL
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input
statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| performance_schema |
+--------------------+
2 rows in set (0.01 sec)

2.用户修改

可以使用rename user语句修改一个或多个已经存在的用户账号。

sql 复制代码
rename user 旧用户 to 新用户

<旧用户>:系统中已经存在的 MySQL 用户账号。

<新用户>:新的 MySQL 用户账号。

使用 RENAME USER 语句时应注意以下几点:

  • RENAME USER 语句用于对原有的 MySQL 用户进行重命名。

  • 若系统中旧账户不存在或者新账户已存在,该语句执行时会出现错误。

  • 使用 RENAME USER 语句,必须拥有 mysql 数据库的 UPDATE 权限或全局 CREATE USER 权限。

案例:

将用户test1修改为testuser1,主机是locahost

sql 复制代码
mysql> RENAME USER 'test1'@'localhost' TO 'testuser1'@'localhost';
Query OK, 0 rows affected (0.01 sec)

mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
| testuser1        | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

C:\Users\kittod>mysql -utestUser1 -p
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 8.4.3 MySQL Community Server - GPL
Copyright (c) 2000, 2024, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input
statement

3.用户授权

可以通过mysql.user表中的数据记录来查看相应的用户权限,也可以使用show grants语句来查询用户的权限。

mysql数据库下的user表中存储这用户的基本权限,也可以使用select语句来查看。

sql 复制代码
select * from mysql.user;

要执行该语句,就必须拥有对user表的查询权限。

⚠️新创建的用户只有登录mysql服务器的权限,没有任何其他权限,不能查询user表。

除了使用select语句之外,还可以使用show grants for语句查看权限:

sql 复制代码
show grants for 'username'@'hostname';

案例1:

sql 复制代码
mysql> show grants for 'testuser1'@'localhost';
+-----------------------------------------------+
| Grants for testuser1@localhost                |
+-----------------------------------------------+
| GRANT USAGE ON *.* TO `testuser1`@`localhost` |
+-----------------------------------------------+
1 row in set (0.00 sec)

其中,USAGE ON *.*表示该用户对任何数据库和任何表都没有权限。

授权就是为某个用户赋予某些权限。例如,可以为新建的用户赋予查询所有数据库和表的权限。MySQL提供了 GRANT 语句来为用户设置权限。

案例2:

sql 复制代码
mysql> grant all on test.* to 'testuser1'@'localhost';
Query OK, 0 rows affected (0.01 sec)

mysql> show grants for 'testuser1'@'localhost';
+-------------------------------------------------------------+
| Grants for testuser1@localhost                              |
+-------------------------------------------------------------+
| GRANT USAGE ON *.* TO `testuser1`@`localhost`               |
| GRANT ALL PRIVILEGES ON `test`.* TO `testuser1`@`localhost` |
+-------------------------------------------------------------+
2 rows in set (0.00 sec)

# 使用用户 testUser1
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| performance_schema |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| dept |
| emp |
| like_tb_emp1 |
| myview |
| salgrade |
| tb_dept1 |
| tb_dept2 |
| tb_dept3 |
| tb_dept4 |
| tb_emp1 |
| tb_emp4 |
| tb_emp5 |
| tb_emp6 |
+----------------+
13 rows in set (0.00 sec)

案例3:

sql 复制代码
mysql> revoke all on test.* from 'testuser1'@'localhost';
Query OK, 0 rows affected (0.01 sec)

mysql> show grants for 'testuser1'@'localhost';
+-----------------------------------------------+
| Grants for testuser1@localhost                |
+-----------------------------------------------+
| GRANT USAGE ON *.* TO `testuser1`@`localhost` |
+-----------------------------------------------+
1 row in set (0.00 sec)

### 使用用户 testUser1
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| performance_schema |
+--------------------+
2 rows in set (0.00 sec)

4.删除用户

使用drop use语句来删除用户,也可以直接在mysql.user表中删除用户以及相关权限。

sql 复制代码
drop user 用户

其中,用户用来指定需要删除的用户账号。

案例:

sql 复制代码
mysql> drop user 'testuser1'@'localhost';
Query OK, 0 rows affected (0.01 sec)

mysql> show grants for 'testuser1'@'localhost';
ERROR 1141 (42000): There is no such grant defined for user 'testuser1' on host 'localhost'
相关推荐
凌虚7 小时前
面向 MySQL 用户的 PostgreSQL 快速上手指南
数据库·后端·架构
五阿哥永琪7 小时前
MySQL中操作json的函数!
数据库·mysql·json
来者皆善7 小时前
了解Mysql优化吗?如何优化索引?
数据库·mysql
赤壁小虾10 小时前
【渲染流水线】[逐片元阶段]-[透明度测试]以UnityURP为例
java·前端·数据库
MC皮蛋侠客10 小时前
SQLAlchemy 系列(七):高级建模与高效写入——批量 DML、方言与扩展
数据库·python
奈斯先生Vector11 小时前
把 Midjourney 二次编辑做成生产系统:customId 能力令牌、Action Graph 与 WebUI 精修工作台
数据库·人工智能·架构·aigc·音视频·midjourney
fengci.11 小时前
Microweber CMS 未授权路径穿越漏洞(CVE-2026-65694)
android·开发语言·前端·学习·php
Lethehong11 小时前
双擎并驱·全链路并行:KFS让TB级异构增量同步秒级到达
数据库
MC皮蛋侠客11 小时前
SQLAlchemy 系列(八):AsyncIO、并发与 Web 生命周期——让每个并发任务持有自己的 Session
数据库·python
一水12 小时前
Redis实战:一个AI工作流系统里的五个应用场景,从传参到限流的完整链路
数据库·redis·wpf