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?
Asked
Active
Viewed 9,668 times
7
-
2This is a security flaw by design. Please don't do this. – Bill Karwin Feb 02 '17 at 06:09
1 Answers
7
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.)

Mark Amery
- 143,130
- 81
- 406
- 459

Kevin Jimenez
- 436
- 2
- 10