mysql stored routine (儲存常式) 中 definer 的作用 和執行個體

來源:互聯網
上載者:User

標籤:def   tps   grant   htm   ring   space   mysql   cte   res   

建立 常式文法參見https://dev.mysql.com/doc/refman/5.7/en/create-procedure.html

建立procedure 的文法如下

CREATE    [DEFINER = { user | CURRENT_USER }]    PROCEDURE sp_name ([proc_parameter[,...]])    [characteristic ...] routine_body
definer 的作用是進行一個許可權的控制 只有super 許可權或者 指定的 procedure 建立者 才能執行這個procedure只有super 使用者才能使用definer 文法

建立一個簡單的執行個體 我是在[email protected] 下面建立的

mysql> delimiter #mysql> CREATE DEFINER=`hee`@`localhost` PROCEDURE `simpleproc`(OUT param1 INT)    begin select count(*) INTO param1 from `categories`;  end #mysql> delimiter ;#調用的時候直接mysql> call simpleproc(@a);mysql> select @a;+------+| @a   |+------+|    6 |+------+1 row in set (0.00 sec)

現在我切換到 [email protected]

本應該 我執行 simpleproc 就可以的 因為目前使用者就是[email protected] 但是仍然失敗 代碼如下

#我先建立了 [email protected] 使用者 【在[email protected] 下面 建立】mysql> create user [email protected]ost identified by "abc";#在給了一部分許可權給[email protected]grant insert,update,select on `api_db`.`categories` to [email protected];# 為什麼我沒有直接給ALL PRIVILEGES 給 [email protected] 是因為 不是所有的情況都可以給all privileges 的 我旨在說明 執行 procedure 的許可權# 切換到 [email protected]mysql> call simpleproc(@a);ERROR 1370 (42000): execute command denied to user ‘hee‘@‘localhost‘ for routine ‘api_db.simpleproc‘mysql> select CURRENT_USER;+---------------+| CURRENT_USER  |+---------------+| [email protected] |+---------------+1 row in set (0.00 sec)
為啥不能執行simpleproc ?

因為在還需要另外的許可權

參考 grant 許可權列表 https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html#priv_execute

摘抄

The EXECUTE privilege is required to execute stored routines (procedures and functions). 要執行 procedure 必須擁有execute 許可權 這個可以再 mysql.user 表格中查看

EXECUTE 是載入在一個database 上面的 所以 要授權使用

mysql> grant EXECUTE on `api_db`.* to [email protected];Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)

但是 還是在另外一個終端 ([email protected] 登陸的終端) 還是執行 call simpleproc(@a) 失敗 只要重新登入mysql一下就可以了

mysql> call simpleproc1(@a) ;Query OK, 1 row affected (0.00 sec)mysql> select @a    -> ;+------+| @a   |+------+|    1 |+------+1 row in set (0.00 sec)

mysql stored routine (儲存常式) 中 definer 的作用 和執行個體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.