mysql 非definer用户如何查看存储过程定义

当我们创建存储过程时,如果没有显示指定definer,则会默认当前用户为该sp的definer,如果没有相关授权,则其他用户是看不了这个sp的。

比如用户zhenxi1拥有如下权限:

它拥有对dev_nacos库的查询权限,这个时候我想查看该库下面的存储过程(这些存储过程的definer非zhenxi1),我使用如下语句查看表结构的定义

SHOW CREATE PROCEDURE x

你会发现Create Procedure字段的值是NULl。

那我们有什么办法可以让这个只读用户(zhenxi1)看到这个存储过程的定义吗?

在8.0版本之前,我们可以通过授予该用户对mysql.proc的select权限来达成目的。

grant select on mysql.proc to zhenxi1@'%';

To use either statement, you must be the user named in the routine DEFINER clause or have SELECT access to the mysql.proc table. If you do not have privileges for the routine itself, the value displayed for the Create Procedure or Create Function column is NULL.

引用:https://dev.mysql.com/doc/refman/5.7/en/show-create-procedure.html

8.0版本之后,去掉了mysql.proc,所以这种方法,不再有效,一种可替代的方案时,授予该账号对所有库的select权限。

grant select on *.* to zhenxi1@'%'

但是授予用户对所有库的select权限,范围太广了,所以mysql从8.0.20增加了show_routine权限,解决这个问题:

GRANT show_routine on *.* TO 'zhenxi1'@'%'

Prior to MySQL 8.0.20, for a user to access definitions of routines the user did not define, the user must have the global SELECT privilege, which is very broad. As of 8.0.20, SHOW_ROUTINE may be granted instead as a privilege with a more restricted scope that permits access to routine definitions. (That is, an administrator can rescind global SELECT from users that do not otherwise require it and grant SHOW_ROUTINE instead.) This enables an account to back up stored routines without requiring a broad privilege. 引用:https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html#priv_show-routine

需要注意的是show_routine是一个global privilege,需要在全局授予,也即*.*,不能在库级别授予,否则,将会报如下错误:

ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES

在 MySQL 中,有些权限是可以在全局范围内授予的,而有些权限只能在数据库或表级别上授予。

总结

8.0版本之前,非definer用户查看存储过程定义,我们可以通过授予该用户对mysql.proc的select权限来达成目的。
此外,8.0.20之前,通过授予用户对所有库的select权限也可以实现。不够这个权限太大了。8.0.20之后,可以通过授予用户的show_routine权限来解决问题。

参考: https://dba.stackexchange.com/questions/184724/permissions-for-mysql-show-create-procedure
https://dev.mysql.com/doc/refman/5.7/en/show-create-procedure.html
https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html

图片

点个“赞 or 在看” 你最好看!

喜欢,就关注我吧!

图片

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值