暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

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

477

当我们创建存储过程时,如果没有显示指定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 在看” 你最好看!

喜欢,就关注我吧!




👇👇👇 谢谢各位老板啦!!!

文章转载自PostgreSQL运维技术,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论