search
HomeDatabaseMysql Tutorial[MySQL] 存储过程、函数、触发器和视图的权限检查_MySQL

bitsCN.com

[MySQL] 存储过程、函数、触发器和视图的权限检查

 

当存储过程、函数、触发器和视图创建后,不单单创建者要执行,其它用户也可能需要执行,换句话说,执行者有可能不是创建者本身,那么在执行存储过程时,MySQL是如何做权限检查的?

在默认情况下,MySQL将检查创建者的权限。假设用户A创建了存储过程p()访问表T,并把execute的权限赋给了B,即使用户B没有访问表T的权限,也能够通过执行存储过程p()访问表T。

下面看一个例子:

首先,我们创建一个表test.t和两个用户a,b,并把权限赋予用户a

[sql] root@(none) 05:39:45>create table portal.t as select * from mysql.user;  Query OK, 25 rows affected (0.16 sec)  Records: 25  Duplicates: 0  Warnings: 0    root@(none) 05:39:55>create user a identified by 'a';  Query OK, 0 rows affected (0.02 sec)    root@(none) 05:40:51>create user b identified by 'b';  Query OK, 0 rows affected (0.00 sec)    root@(none) 05:40:59>grant all privileges on portal.* to a;  Query OK, 0 rows affected (0.01 sec)  

 

接着,以用户a创建存储过程p():

[sql] DELIMITER $$  USE portal$$  CREATE PROCEDURE `p`()  BEGIN    SELECT COUNT(*) FROM portal.t;  END$$  DELIMITER ;  

 

并把执行该存储过程的权限赋给用户b:

[sql] root@(none) 05:54:28>grant execute on procedure portal.p to b;  Query OK, 0 rows affected (0.00 sec)  

 

这时候,已用户b连接后通过执行存储过程可以获得t表的访问权限:

[sql] b@(none) 05:58:20>call portal.p();  +----------+  | COUNT(*) |  +----------+  |       25 |  +----------+  1 row in set (0.00 sec)    Query OK, 0 rows affected (0.00 sec)  

 

但如果直接访问将出现权限错误:

[sql] b@(none) 05:58:40>select count(*) from portal.t;  ERROR 1142 (42000): SELECT command denied to user 'b'@'192.168.1.15' for table 't'  

 

MySQL这样的设置有一定的道理,但同时也带来了安全隐患:比如如果一个用户通过创建一个存储过程来访问敏感数据,则可以调用该存储过程的所有用户都能访问敏感数据。

如果你不想使用MySQL的默认设置,可以在定义存储程序和视图时在create语句里使用definer = account字句指定定义者,这样在执行存储程序和视图时,将检查definer的权限,而不是创建者的权限。

举个例子,当你用root 创建一个存储过程时,在默认情况下,在执行该存储过程时,执行者将获得root的权限,但当你加上definer = A后,执行者只能获得A的权限。

但是definer还是没能完全解决上面提到的安全隐患,别急,MySQL还提供了SQL SECURITY选项来控制权限,它有两个取值:

1)DEFINER:以定义者的权限执行(默认)

2)INVOKER:以调用者的权限执行

如果你不想在存储程序或试图在执行时的权限多于调用者,就设置SQL SECURITY INVOKER即可。

例如,下面的试图将访问mysql.user,并设置了SQL SECURITY INVOKER选项,这样如果调用者没有访问mysql.user的权限,则无法通过权限检查。

[sql] create sql security invoker view v    as select * from mysql.user;  

 

注意:因为触发器和事件是由系统调用的,没有调用者的概念,所以它们没有SQL SECURITY选项。

bitsCN.com
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment