问题
I'm struggling a little with MySQL stored procedures and getting a bit frustrated. I have a set of SPs created by Bob. As he is the DEFINER, only he can see the CREATE statement for them, amend them etc.
Mary can see Bob's stored procedures in the schema in MySQL Workbench, but can't see what they do - when she clicks on the SP and selects "Send to SQL Editor -> CREATE statement" nothing happens, because she's not the definer.
Coming from a MS SQL background this is a little bizarre to me. Is there any way I can set up a group (e.g. "DB_DEVS") and make this group the definer of the Stored Procs so Bob and Mary can see each other's code?
回答1:
DEFINER always refers to the user who created the stored procedure. This can only be 1 user.
If Mary wants to see Bobs procedure, she could call:
SHOW CREATE PROCEDURE proc_name
To see the code of the procedure. She could also call the following to see the code:
SELECT ROUTINE_DEFINITION FROM information_schema.ROUTINES WHERE SPECIFIC_NAME='proc_name'
Heres how to enable Mary to access the view of the procedure via MySQL-Workbench:
By default, Mary is not able to send the create statement to the SQL-Editor. But this is just a privilege thing. Mary just needs a basic SELECT
privilege in the mysql.proc
table. To do this, run the following SQL-Statement (via Command line or directly in the Workbench):
GRANT SELECT ON mysql.proc TO 'mary'@'%'
This command enables Mary to access the Create-Statement from all hosts. If you want to limit it to a specific host you would do something like:
GRANT SELECT ON mysql.proc TO 'mary'@'192.168.2.1'
If Mary has the SELECT privilege she should be able to see the procedure after doing Send to SQL Editor -> CREATE statement
NOTE: In order to run the GRANT
-Command you need to be logged in as user who has the privilege to grant privileges (e.g. root-user)
+++++++++++++++++++++++++EDIT+++++++++++++++++++++++++
There is a "quick and dirty" way to achieve this for a large number of users without writing for each user a new command to grant the privilege: (make sure to do this with a user who has the privilege to insert rows via the Workbench)
- Open a new SQL-Tab in your Workbench
- Type
SELECT * FROM mysql.tables_priv;
and run it - Above the result-grid there should be the a small button which allows you to import data from a csv-File.
Create a CSV-File which looks like this:
%,mysql,jane,proc,root@localhost,"2016-02-19 22:51:47",Select, %,mysql,max,proc,root@localhost,"2016-02-19 22:51:47",Select, %,mysql,steve,proc,root@localhost,"2016-02-19 22:51:47",Select, %,mysql,greg,proc,root@localhost,"2016-02-19 22:51:47",Select, %,mysql,jamie,proc,root@localhost,"2016-02-19 22:51:47",Select,
...further users
jane, max, steve,... would be your users. Leave the other columns the way they are.
- Import your csv-File
- Run
FLUSH PRIVILEGES
in an SQL-Window (reloades privileges from priv-tables) - Finished! All your users can now access Stored Procedures
来源:https://stackoverflow.com/questions/35511862/possible-to-have-multiple-users-as-definer-for-mysql-stored-procedure