Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute shell commands from MySQL stored procedure

Tags:

mysql

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?

like image 991
Syed Md. Kamruzzaman Avatar asked Jan 31 '17 07:01

Syed Md. Kamruzzaman


1 Answers

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

like image 185
Kevin Jimenez Avatar answered Oct 01 '22 10:10

Kevin Jimenez