权限管理
在mysql中将权限管理分为三类:
1、数据权限:增删改查(select\update\delete\insert)
2、结构权限:结构操作(create\drop)
3、管理权限:权限管理(create user\grant\revoke),通常只给管理员如此权限
授予权限:grant
将权限分配给指定的用户
基本语法:grant 权限列表 on 数据库.表名 to 用户
备注:权限列表:使用逗号分隔,但是可以使用all privileges代表全部权限
数据库.表名:可以是单表(数据库名字.表名),可以是具体某个数据库(数据库.*),也可以是整库(*.*),*代表所有。
通配符%和*号适用情况:在字符串或者引号内必须用%,没引号的地方就用*
-- 超级用户下的数据库如下:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| example |
| job |
| mydatabase |
| mydatabase1 |
| mydatabase2 |
| mydatabases |
| mysql |
| performance_schema |
| test |
+--------------------+
10 rows in set (0.15 sec)
-- 超级用户root权限下创建用户
mysql> create user 'tom'@'%' identified by 'password';
Query OK, 0 rows affected (0.00 sec)
mysql> grant select on job.* to tom;
Query OK, 0 rows affected (0.00 sec)
-- 重新用账号tom密码password重新登入数据库
C:\Users\47876>mysql -u tom -p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.53 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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.
-- 账户名tom登入的数据库,可以看到用户tom数据库显示只有两项
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0.00 sec)
-- 然后在超级用户下授予用户tom的查看数据库job所有表的权限
mysql> grant select on job.* to tom;
Query OK, 0 rows affected (0.00 sec)
-- 账户名tom登入的数据库,现在可以看到用户tom数据库显示有三项
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| job |
| test |
+--------------------+
3 rows in set (0.00 sec)
-- 可以查看表
mysql> show tables;
+---------------+
| Tables_in_job |
+---------------+
| address |
| class |
| class1 |
| employee |
| information |
| mygood |
| mytable |
| mytable_view |
| score |
| student |
| teacher |
| teacher66 |
| text |
| text1 |
| user |
| work |
| work_info |
| work_view |
+---------------+
18 rows in set (0.00 sec)
-- 没有其他权限,只有查看权限。
mysql> create table class2;
ERROR 1142 (42000): CREATE command denied to user 'tom'@'localhost' for table 'class2'
取消权限:revoke
权限回收:将权限从用户手中收回
基本语法:revoke 权限列表/all privileges on 数据库.表名 from 用户; //数据库.表名的格式同上一条备注说明
-- 超级用户登入数据库管理下授予用户tom查看和取消用户查看job表权限
mysql> grant select on job.* to tom;
Query OK, 0 rows affected (0.00 sec)
mysql> revoke all privileges on job.* from 'tom'@'%';
Query OK, 0 rows affected (0.00 sec)
-- 用户tom登入数据库管理下查看到数据库的显示情况
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| job |
| test |
+--------------------+
3 rows in set (0.00 sec)
-- 取消权限后job表消失了
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| test |
+--------------------+
2 rows in set (0.00 sec)
权限回收,同样不需要刷新,用户马上就会感受到
刷新权限:flush
flush:刷新,将当前对用户的权限操作,进行一个刷新,将操作的具体内容同步到对应的表中。
基本语法:flush privileges;
mysql> flush privileges;
Query OK, 0 rows affected (0.12 sec)