数据库系统默认一个超级管理员root用户,root用户拥有操作数据库系统的高级权限,但是,在实际的应用当中,并不是所有的用户都需要这些权限,为了数据和数据库系统的安全性,针对不同的用户,为其分配不同的操作权限是很好的选择。
资料显示,创建一个用户的时候,可以为其指定权限范围,如果没有新的授权操作,新建用户只能在其规定的权限范围之内进行操作。操作权限范围有三个大类,分别是DBA,RESOURCE和CONNECT。
即便是指定了权限范围,拥有更高授权权限的用户也可以为其他用户授予新的权限或者回收已有权限。
但是笔者实验的时候并没有成功指定这三种权限范围,正在寻找新的途径,本篇博客以MySQL8.0.12数据库为例,仅演示默认拥有的CONNECT权限范围的用户授权和回收权限的操作。DBA和RESOURCE权限用户待学习掌握之后进行补充。
create user 'username'@'host' identified [with mysql_native_password] by 'pass';
如果新建用户的时候没有指定新用户的权限,默认该用户拥有CONNECT权限。
笔者本机配置了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)
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"
mysql> grant select on table test.course to 'student'@'localhost';
Query OK, 0 rows affected (0.11 sec)
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'
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)
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)
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 关键字,那么,系统不仅会回收该用户的这个权限,所有由该用户授予给其他用户的这个权限都将会被级联收回。