数据库用户管理

数据库用户管理

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'
相关推荐
llxxyy卢8 小时前
SQL注入之堆叠及waf绕过注入(安全狗)
数据库·sql·安全
参宿四南河三10 小时前
Android Compose SideEffect(副作用)实例加倍详解
android·app
dblens 数据库管理和开发工具10 小时前
PostgreSQL模式:数据库中的命名空间艺术
数据库·postgresql·oracle
Java追光着10 小时前
ADB 无线调试 APP 完全攻略(2025 最新版)—— 从连接到查看日志,一文搞定!
adb
火柴就是我11 小时前
mmkv的 mmap 的理解
android
没有了遇见11 小时前
Android之直播宽高比和相机宽高比不支持后动态获取所支持的宽高比
android
数据最前线11 小时前
数据管理技术发展的3个阶段
数据库·考研·数据库系统概论
SelectDB11 小时前
冷查第一,再登榜首!Apache Doris 3.1 全面刷新 JSONBench 性能纪录
数据库·apache
wei_shuo11 小时前
智能运维×低资源占用:金仓数据库助力能源企业降本增效与国产化替换实践
运维·数据库·king base
shenshizhong11 小时前
揭开 kotlin 中协程的神秘面纱
android·kotlin