詳解MySQL第三篇—DCL語句

來源:互聯網
上載者:User

標籤:定義   目的   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語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.