Execute shell commands from MySQL stored procedure

后端 未结 1 1709
Happy的楠姐
Happy的楠姐 2021-02-10 07:02

Is it possible to run arbitrary shell commands - for instance, to move a file from one folder to another - using a MySQL stored procedure? If so, how?

相关标签:
1条回答
  • 2021-02-10 07:51

    MySQL doesn't provide this functionality out of the box, but it is provided by the lib_mysqludf_sys library. If you install that, you will be able to call its sys_exec function to execute commands:

    DELIMITER @@
    
    CREATE TRIGGER Test_Trigger 
    AFTER INSERT ON MyTable 
    FOR EACH ROW 
    BEGIN
     DECLARE cmd CHAR(255);
     DECLARE result int(10);
     SET cmd=('mv path/to/file new/path/file');
     SET result = sys_exec(cmd);
    END;
    @@
    DELIMITER ;
    

    (I found this approach at http://crazytechthoughts.blogspot.com/2011/12/call-external-program-from-mysql.html.)

    0 讨论(0)
提交回复
热议问题