標籤:定義 目的 tor ror mon ble 變更 connect show
DCL(Data Control Language)語句:資料控制語句,用於控制不同資料區段直接的許可和存取層級的語句。這些語句定義了資料庫、表、欄位、使用者的存取權限和安全層級。主要的語句關鍵字包括 grant、revoke 等。
DCL 語句主要是 DBA 用來管理系統中的對象許可權時所使用,一般的開發人員很少使用。下面通過一個例子來簡單說明一下。
建立一個資料庫使用者 z1,具有對 sakila 資料庫中所有表的 SELECT/INSERT 許可權:
| 123456789101112 |
mysql> grant select,insert on sakila.* to ‘z1‘@‘localhost‘ identified by ‘123‘;Query OK, 0 rows affected (0.00 sec)mysql> exitBye[[email protected] ~]$ mysql -uz1 -p123Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 21671 to server version: 5.1.9-beta-logType ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the buffer.mysql> use sakilaDatabase changedmysql> insert into emp values(‘bzshen‘,‘2005-04-01‘,3000,‘3‘);Query OK, 1 row affected (0.04 sec) |
由於許可權變更,需要將 z1 的許可權變更,收回 INSERT,只能對資料進行 SELECT 操作:
| 12345678 |
[[email protected] ~]$ mysql -urootWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 21757 to server version: 5.1.9-beta-logType ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the buffer.mysql> revoke insert on sakila.* from ‘z1‘@‘localhost‘;Query OK, 0 rows affected (0.00 sec)mysql> exitBye |
使用者 z1 重新登入後執行前面語句:
| 12345678910 |
[mysql@db3 ~]$ mysql -uz1 -p123Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 21763 to server version: 5.1.9-beta-logType ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the buffer.mysql> insert into emp values(‘bzshen‘,‘2005-04-01‘,3000,‘3‘);ERROR 1046 (3D000): No database selectedmysql> use sakilaDatabase changedmysql> insert into emp values(‘bzshen‘,‘2005-04-01‘,3000,‘3‘);ERROR 1142 (42000): INSERT command denied to user ‘z1‘@‘localhost‘ for table ‘emp‘ |
以上例子中的 grant 和 revoke 分別授出和收回了使用者 z1 的部分許可權,達到了我們的目的。
詳解MySQL第三篇—DCL語句