How can I a call stored procedure on a remote MySQL Ubuntu server?

守給你的承諾、 提交于 2019-12-06 11:18:22

AFAIK, you can't call a procedure stored in server A from server B.

What I'd do is:

  1. Modify the procedure so the output is stored in a table.
  2. Use mysqldump to dump the data of this output table and store it in the other server.

Example:

On server A, the procedure can be something like this:

delimiter $$
create procedure my_procedure()
begin
    -- Create a table to store the output:
    drop table if exists temp_result;
    create table temp_result (
        CID int not null primary key,
        name varchar(50)
    );
    -- Populate the table
    insert into temp_result
        select ...
end $$
delimiter ;

On server B, execute the following statement in the shell, not in MySQL CLI:

mysqldump <options_A> db_A temp_result --no-create-db --add-drop-table | mysql <options_B> db_B

where:

  • <options_A> The options needed to connect to server A from server B:
    -h <IP of server A> -u <user> -p<password>.
  • db_A The database in server A where the result is stored
  • <options_B> The options needed to connecto to server B:
    -h localhost -u <user> -p<password>
Ivan Cachicatari

There is a way to send data to a remote table using stored procedures, the trick is use federated tables. I created tables in two servers and stored procedure to reproduce your situation:

  1. SERVER A: create table and insert some data

    create table course_A
    (
      CID int not null primary key,
      name varchar(50),
      year int
    );
    
    insert into course_A values 
    (1,'CS1P',2016),
    (2,'CS1Q',2016),
    (109,'CS1-CT',2016),
    (120,'CS1PX',2016),
    (121,'CS1S',2016);
    
  2. SERVER B: create a table with structure

    create table course_B
    (
      CID int not null primary key,
      name varchar(50)
    );
    
  3. SERVER A: create a remote table connected to table in server B:

    create table course_B_remote
    (
      CID int not null primary key,
      name varchar(50)
    )
    ENGINE=FEDERATED
    CONNECTION='mysql://user:password@SERVER_B_IP:3306/database/course_B';
    

    Now, whatever to do with table_B_remote affects directly to table_B in SERVER B.

  4. SERVER A: Create stored procedure to send the query results from course_A table to course_B_remote:

    delimiter $$
    drop procedure if exists sp_coursesForYear$$
    create procedure sp_coursesForYear(p_year int)
    begin
    
        /*
         Your procedure code...
        */
    
        insert into course_B_remote (CID,name)
        select  CID, name 
        from course_A   
        where year = p_year;
    
    end$$
    delimiter ;
    
  5. Now, test the stored procedure:

    server A prompt> call sp_coursesForYear(2016);
    Query OK, 5 rows affected (0,00 sec)
    
  6. Check if it worked:

    server B prompt> select * from  course_B;
    +-----+--------+
    | CID | name   |
    +-----+--------+
    |   1 | CS1P   |
    |   2 | CS1Q   |
    | 109 | CS1-CT |
    | 120 | CS1PX  |
    | 121 | CS1S   |
    +-----+--------+
    5 rows in set (0.00 sec)
    
  7. Enjoy!

Maybe you will need to alter your procedures to use the FEDERATED tables. No enable FEDERATED tables see this answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!