开发者

Install Stored Procedure on Multiple Databases

开发者 https://www.devze.com 2023-02-06 02:12 出处:网络
Is there any way to easily create a stored procedure on multiple MySQL databases at 开发者_如何学Conce? All the databases are on the same MySQL install.Installing in all schemas

Is there any way to easily create a stored procedure on multiple MySQL databases at 开发者_如何学Conce? All the databases are on the same MySQL install.


Installing in all schemas

To get a list of the schemas, use show databases;. Combine this with -- use:

use schemaA;
-- use schemaB;
-- use schemaC;

create procedure ...

Manually iterate through the schemas, removing and uncommenting use clauses as you move on, checking that everything works out. In MySQL Workbench, Ctrl+Shift+Enter is your friend.

Installing routines in a subset of schemas

Normally you don't want to install the stored routine in all schemas on a server, but only in a subset --- often defined by the set of schemas which already have some specific stored routine installed. Then, as discussed on SO, you can use a query like this to get the names of the relevant schemas:

SELECT ROUTINE_SCHEMA FROM `information_schema`.`ROUTINES` where specific_name = 'MyRoutine'; 

Verification

After deploying routines, to verify the existence of them, you can use a query like this:

SELECT distinct
    r1.ROUTINE_SCHEMA, 
    case when r2.specific_name is not null then '' else '####' end as RoutineName1,
    case when r3.specific_name is not null then '' else '####' end as RoutineName2,
    case when r4.specific_name is not null then '' else '####' end as RoutineName3
FROM 
    `information_schema`.`ROUTINES` as r1 
LEFT JOIN (select * from `information_schema`.`ROUTINES` where specific_name = 'RoutineName1') as r2 on r1.routine_schema = r2.routine_schema
LEFT JOIN (select * from `information_schema`.`ROUTINES` where specific_name = 'RoutineName2') as r3 on r1.routine_schema = r3.routine_schema
LEFT JOIN (select * from `information_schema`.`ROUTINES` where specific_name = 'RoutineName3') as r4 on r1.routine_schema = r4.routine_schema
where 
    r1.specific_name = 'FilteringRoutineName'; 

This query will check whether RoutineName1, RoutineName2 and RoutineName3 exist in the database schemas on your server which have the routine FilteringRoutineName. If a routine is missing, it will be marked with ####.

Of course, this only checks for routine existence. To verify their implementation, you may need a database diff tool (such as MySQL Compare or similar).


Assuming you are using Linux, a simple BASH loop with an array of schema names will let you do this.

Save your procedure definition to a file (e.g. myproc.sql), then use the file as input to mysql in the loop. If you put your sign-in details in ~/.my.cnf you can also avoid having to put usernames and passwords on the cmdline.

for i in dbname1 dbname2 dbname3; do mysql ${i} < myproc.sql; done;


I would recommend doing a copy-paste and create the stored procedure in each database schema if they need to be available to that schema only. Otherwise I would follow the recommendation from 'Kelly Vista' and just refer to the stored procedure located in one of the schema's.

0

精彩评论

暂无评论...
验证码 换一张
取 消