当前位置:Gxlcms > 数据库问题 > mysql stored routine (存储例程) 中 definer 的作用 和实例

mysql stored routine (存储例程) 中 definer 的作用 和实例

时间:2021-07-01 10:21:17 帮助过:7人阅读

definer 的作用是进行一个权限的控制 只有super 权限或者 指定的 procedure 创建者 才能执行这个procedure
只有super 用户才能使用definer 语法

创建一个简单的实例 我是在root@localhost 下面创建的

  1. <code class="hljs">mysql> delimiter #
  2. mysql> CREATE DEFINER=<span class="hljs-code">`hee`@<span class="hljs-code">`localhost` PROCEDURE <span class="hljs-code">`simpleproc`(OUT param1 INT)
  3. <span class="hljs-code"> begin select count(*) INTO param1 from `categories`;
  4. end #
  5. mysql> delimiter ;
  6. #<span class="zh-hans">调用的时候直接
  7. mysql> call simpleproc(@a);
  8. <span class="hljs-section">mysql> select @a;
  9. +------+
  10. <span class="hljs-section">| @a |
  11. +------+
  12. <span class="hljs-section">| 6 |
  13. +------+
  14. 1 row in set (0.00 sec)
  15. </span></span></span></span></span></span></span></span></code>

现在我切换到 hee@localhost

本应该 我执行 simpleproc 就可以的 因为当前用户就是hee@localhost 但是仍然失败 代码如下

  1. <code class="hljs">#<span class="zh-hans">我先创建了 hee@localhost <span class="zh-hans">用户 <span class="zh-hans">【在root@localhost <span class="zh-hans">下面 <span class="zh-hans">创建】
  2. mysql> create user hee@localhost identified by "abc";
  3. #<span class="zh-hans">在给了一部分权限给hee@localhost
  4. grant insert,update,select on <span class="hljs-code">`api_db`.<span class="hljs-code">`categories` to hee@localhost;
  5. # <span class="zh-hans">为什么我没有直接给ALL PRIVILEGES <span class="zh-hans">给 hee@localhost <span class="zh-hans">是因为 <span class="zh-hans">不是所有的情况都可以给all privileges <span class="zh-hans">的 <span class="zh-hans">我旨在说明 <span class="zh-hans">执行 procedure <span class="zh-hans">的权限
  6. # <span class="zh-hans">切换到 hee@localhost
  7. mysql> call simpleproc(@a);
  8. ERROR 1370 (42000): execute command denied to user <span class="hljs-emphasis">‘hee‘@<span class="hljs-emphasis">‘localhost‘ for routine <span class="hljs-emphasis">‘api_db.simpleproc‘
  9. <span class="hljs-section">mysql> select CURRENT_USER;
  10. +---------------+
  11. <span class="hljs-section">| CURRENT_USER |
  12. +---------------+
  13. <span class="hljs-section">| hee@localhost |
  14. +---------------+
  15. 1 row in set (0.00 sec)
  16. </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>

为啥不能执行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 上面的 所以 要授权使用

  1. <code class="hljs">mysql> grant EXECUTE <span class="hljs-keyword">on <span class="hljs-string">`api_db`.* <span class="hljs-keyword">to hee@localhost;
  2. Query OK, <span class="hljs-number">0 <span class="hljs-keyword">rows affected (<span class="hljs-number">0.00 sec)
  3. mysql> flush privileges;
  4. Query OK, <span class="hljs-number">0 <span class="hljs-keyword">rows affected (<span class="hljs-number">0.00 sec)
  5. </span></span></span></span></span></span></span></span></span></code>

但是 还是在另外一个终端 (hee@localhost 登陆的终端) 还是执行 call simpleproc(@a) 失败 只要重新登录mysql一下就可以了

  1. <code class="hljs">mysql> call simpleproc1(@a) ;
  2. Query OK, 1 row affected (0.00 sec)
  3. mysql> select @a
  4. <span class="hljs-section"> -> ;
  5. +------+
  6. <span class="hljs-section">| @a |
  7. +------+
  8. <span class="hljs-section">| 1 |
  9. +------+
  10. 1 row in set (0.00 sec)</span></span></span></code>

mysql stored routine (存储例程) 中 definer 的作用 和实例

标签:def   tps   grant   htm   ring   space   mysql   cte   res   

人气教程排行