MySQL使用入门--DCL语句

DCL 语句主要是DBA用来管理系统中的对象权限时所使用,一般的开发人员很少使用。

创建一个数据库用户z1,具有对sakila数据库中所有表的SELECT/INSERT权限:

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> GRANT SELECT,INSERT ON test.* TO 'z1'@'localhost' IDENTIFIED BY '#System20';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[root@localhost ~]# mysql -uz1 -p#System20
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.17 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.

mysql> USE test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> INSERT INTO emp VALUES('liming','2005-06-09',4000,3);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM emp;
+--------+------------+---------+--------+
| ename  | hiredate   | sal     | deptno |
+--------+------------+---------+--------+
| lisa   | 2003-02-01 | 8000.00 |      1 |
| dony   | 2005-02-05 | 5000.00 |      2 |
| bzshen | 2005-09-01 | 4000.00 |      1 |
| zzx    | 2000-01-01 | 3000.00 |      3 |
| bjguan | 2004-04-02 | 3000.00 |      1 |
| atai   | 1999-01-01 | 8000.00 |      4 |
| liming | 2005-06-09 | 4000.00 |      3 |
+--------+------------+---------+--------+
7 rows in set (0.00 sec)

由于权限变更,需要将z1的权限变更,收回INSERT,只对数据进行SELECT 操作:

[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.17 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.

mysql> REVOKE INSERT ON test.* FROM 'z1'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

用户z1 重新登录后执行前面语句:

[root@localhost ~]# mysql -uz1 -p#System20
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.17 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.

mysql> USE test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> INSERT INTO emp VALUES('liming','2005-06-09',4000,3);
ERROR 1142 (42000): INSERT command denied to user 'z1'@'localhost' for table 'emp'

 

未完,待续。。。

 

posted @ 2017-03-20 02:56  追阳  阅读(627)  评论(0)    收藏  举报