Syed Md. Kamruzzaman
Syed Md. Kamruzzaman

Reputation: 1089

Execute shell commands from MySQL stored procedure

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?

Upvotes: 7

Views: 10544

Answers (1)

Kevin Jimenez
Kevin Jimenez

Reputation: 436

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.)

Upvotes: 7

Related Questions