数据库用户管理

数据库用户管理

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'
相关推荐
DevOpenClub3 分钟前
全国三甲医院主体信息 API 接口
java·大数据·数据库
一勺菠萝丶12 分钟前
管理后台使用手册在线预览与首次登录引导弹窗实现
java·前端·数据库
无忧智库16 分钟前
某大型银行“十五五”金融大模型风控与智能投顾平台建设方案深度解读(WORD)
数据库·金融
爱码小白17 分钟前
数据库多表命名的通用规范
数据库·python·mysql
TE-茶叶蛋25 分钟前
结合登录页-PHP基础知识点解析
android·开发语言·php
alexhilton30 分钟前
Jetpack Compose元球边缘效果
android·kotlin·android jetpack
huohuopro32 分钟前
Hbase伪分布式远程访问配置
数据库·分布式·hbase
XDHCOM1 小时前
ORA-12169: TNS连接标识符过长,Oracle报错故障修复与远程处理
数据库·oracle
爬山算法1 小时前
MongoDB(86)如何使用MongoDB存储大文件?
数据库·mongodb
xcLeigh1 小时前
KES数据库表空间目录自动创建特性详解与存储运维最佳实践
大数据·运维·服务器·数据库·表空间·存储