Mysql – What permissions do I need if database a contains a stored procedure that inserts into a table in database b (same machine)

MySQLpermissionsstored-procedures

I have a stored procedure in database a that contains dynamic sql to perform an insert into a table in database b. What permissions does the executing user need on database b?

Best Answer

It all depends on how the Stored Procedure was created

In the table mysql.proc (physical home of Stored Procedures), there is am ENUM column called security_type defined as enum('INVOKER','DEFINER').

If a Stored Procedure was defined as DEFINER and the calling user has the EXECUTE privilege, then all grants are proxied and the calling user can run everything in the Stored Procedure. Nevertheless, you will be able to run it.

If a Stored Procedure was defined as INVOKER, then the calling user needs to have all necessary grants before the calling user can run everything in the Stored Procedure.

If you are in database A running CALL B.MyProc() as myuser@'%', then myuser@'%' needs to have the EXECUTE privilege to even call Stored Procedures.

In your case, if you are in database A running CALL MyProc() as myuser@'%', then myuser@'%' needs the database-level grants to database B defined. SUGGESTION: Move the Stored Procedure to database B, give the user the EXECUTE privilege and run running CALL B.MyProc().