数据库中创建用户并管理用户权限的相关内容与命令

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38234015/article/details/89215230

概述

数据库系统默认一个超级管理员root用户,root用户拥有操作数据库系统的高级权限,但是,在实际的应用当中,并不是所有的用户都需要这些权限,为了数据和数据库系统的安全性,针对不同的用户,为其分配不同的操作权限是很好的选择。

资料显示,创建一个用户的时候,可以为其指定权限范围,如果没有新的授权操作,新建用户只能在其规定的权限范围之内进行操作。操作权限范围有三个大类,分别是DBA,RESOURCE和CONNECT。

即便是指定了权限范围,拥有更高授权权限的用户也可以为其他用户授予新的权限或者回收已有权限。

但是笔者实验的时候并没有成功指定这三种权限范围,正在寻找新的途径,本篇博客以MySQL8.0.12数据库为例,仅演示默认拥有的CONNECT权限范围的用户授权和回收权限的操作。DBA和RESOURCE权限用户待学习掌握之后进行补充。


 创建用户操作

create user 'username'@'host' identified [with mysql_native_password] by 'pass';

 如果新建用户的时候没有指定新用户的权限,默认该用户拥有CONNECT权限。


操作演示

用root用户创建一个student用户

笔者本机配置了path环境变量,所以可以不进入MySQL的bin目录直接使用mysql命令。有关登录MySQL数据库的操作可以参看”登录MySQL服务器“

C:\Windows\system32>mysql -hlocalhost -uroot -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.12 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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> # 创建一个用户
mysql> create user 'student'@'localhost' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.04 sec)

mysql> # 查看用户表
mysql> select user,host from user;
ERROR 1046 (3D000): No database selected
mysql> use mysql;
Database changed
mysql> select user,host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
| student          | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| company            |
| information_schema |
| mydatabase         |
| mysql              |
| performance_schema |
| registersystem     |
| sys                |
| test               |
+--------------------+
12 rows in set (0.00 sec)

 现在新开一个dos窗口,登录student用户

Microsoft Windows [版本 10.0.17134.706]
(c) 2018 Microsoft Corporation。保留所有权利。

C:\Windows\system32>mysql -hlocalhost -ustudent -p
Enter password: ******
ERROR 1045 (28000): Access denied for user 'student'@'localhost' (using password: YES)

C:\Windows\system32>mysql -hlocalhost -ustudent -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.12 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

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> # student用户登录成功,现在试图选择一个数据库进行操作
mysql> use test;
ERROR 1044 (42000): Access denied for user 'student'@'localhost' to database 'test'
mysql> # 拒绝用户"student"@"localhost"访问数据库"test"

使用root用户为student用户授予test数据库中查看course表的权限

mysql> grant select on table test.course to 'student'@'localhost';
Query OK, 0 rows affected (0.11 sec)

使用student用户查看course表

mysql> use test;
Database changed
mysql> select * from course;
+------+--------------+------+---------+
| cno  | cname        | cpno | ccredit |
+------+--------------+------+---------+
| 1    | 数据库       | 5    | 4       |
| 2    | 数学         | NULL | 2       |
| 3    | 信息系统     | 1    | 4       |
| 4    | 操作系统     | 6    | 3       |
| 5    | 数据结构     | 7    | 4       |
| 6    | 数据处理     | NULL | 2       |
| 7    | PASCAL       | 6    | 4       |
+------+--------------+------+---------+
7 rows in set (0.00 sec)

mysql> # 查询成功
mysql> # 试图修改数据
mysql> update course set cname = '数据库系统' where cno = '1';
ERROR 1142 (42000): UPDATE command denied to user 'student'@'localhost' for table 'course'

继续给student用户授权

mysql> grant select on table test.course to 'student'@'localhost';
Query OK, 0 rows affected (0.11 sec)

mysql> # 给student用户授予对course表的全部权限
mysql> grant all privileges on table test.course to 'student'@'localhost';
Query OK, 0 rows affected (0.08 sec)

mysql> # 给student用户授予对student表中sdept列的修改权限
mysql> grant update(Sdept) on test.student to 'student'@'localhost';
Query OK, 0 rows affected (0.01 sec)

使用student操作数据库

mysql> # 现在student用户拥有对course表的所有权限
mysql> update course set cname = '数据库系统' where cno = '1';
Query OK, 1 row affected (0.09 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from course;
+------+-----------------+------+---------+
| cno  | cname           | cpno | ccredit |
+------+-----------------+------+---------+
| 1    | 数据库系统      | 5    | 4       |
| 2    | 数学            | NULL | 2       |
| 3    | 信息系统        | 1    | 4       |
| 4    | 操作系统        | 6    | 3       |
| 5    | 数据结构        | 7    | 4       |
| 6    | 数据处理        | NULL | 2       |
| 7    | PASCAL          | 6    | 4       |
+------+-----------------+------+---------+
7 rows in set (0.00 sec)

mysql> # 查询student表
mysql> select * from student;
ERROR 1142 (42000): SELECT command denied to user 'student'@'localhost' for table 'student'
mysql> select Sdept from student;
ERROR 1142 (42000): SELECT command denied to user 'student'@'localhost' for table 'student'
mysql> update student set Sdept = '修改过的专业名' where Sno = '001';
ERROR 1143 (42000): SELECT command denied to user 'student'@'localhost' for column 'Sno' in table 'student'

权限限制就是这么严格,因为只对student.Sdept有修改权,连select Sdept以及通过Sno做条件都是不可以的。

回收用户权限

mysql> # 收回studenth用户对course表的查询权限
mysql> revoke select on table test.course from 'student'@'localhost';
Query OK, 0 rows affected (0.06 sec)

student用户再进行对course表的查询操作将出现权限限制

mysql> select * from test.course;
ERROR 1142 (42000): SELECT command denied to user 'student'@'localhost' for table 'course'

总结

授予用户权限

grant <权限>[,<权限>,···] 
on <对象类型> <对象名> [,<对象类型> <对象名>,···]
to <用户> [,<用户>,···]
[with grant option];

回收用户权限

revoke <权限>[,<权限>,···] 
on <对象类型> <对象名> [,<对象类型> <对象名>,···]
from <用户> [,<用户>,···]
[cascade];

说明

给用户授予所有操作权限使用 all privileges.

如果使用了  [with grant option] 字段修饰,那么该用户还可以将这个权限授予其他用户,如果没有这个字段修饰,用户只能自己使用这个权限而不能授予给其他用户。

回收权限的时候,如果使用了 cascade 关键字,那么,系统不仅会回收该用户的这个权限,所有由该用户授予给其他用户的这个权限都将会被级联收回。

猜你喜欢

转载自blog.csdn.net/qq_38234015/article/details/89215230
今日推荐