Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Shell script/command from MySQL Trigger/Stored Procedure

Tags:

shell

mysql

I'm currently writing my Main Assignment on my last semester at my study (IT-Engineering with Networking) and currently working with MySQL.

My question is: Is it possible to execute a Shell script/Command from within a MySQL Trigger/Procedure? Or can it be done from a CASE statement? I've been searching around the internet and read that it's inadvisable to do it. But I need a script to check a table in a database for alerts and then warn people if there is any. If there is anyway else this could be done, then I'm open for ideas. Any input will be appreciated :)

like image 439
capacop Avatar asked Apr 23 '15 10:04

capacop


People also ask

Can MySQL trigger call stored procedure?

MySQL allows you to call a stored procedure from a trigger by using the CALL statement. By doing this, you can reuse the same stored procedure in several triggers. However, the trigger cannot call a stored procedure that has OUT or INOUT parameters or a stored procedure that uses dynamic SQL.

How do you call a shell script in SQL?

sql file from PUTTY, we first connect to DB using syntax <sqlplus> and then DB credentials. Then, we will give the file execution command as <@file_name. sql> and once the file is executed, we will give <exit> to come out of db and then remaining process.


1 Answers

You can read this blog for triggering a shell script from MySQL: https://patternbuffer.wordpress.com/2012/09/14/triggering-shell-script-from-mysql/. To summarize, two options are presented:

  1. Polling. To improve performance, a trigger could record the change in another table which you poll instead.
  2. MySQL UDF. Write your own plugin, and beware of security implications!

I think for your requirement just write a python/php/perl script which will connect your MySQL DB and query the alert table for any alert and accordingly show warning message on the screen or send email/sms warning.

like image 56
Pranab Sharma Avatar answered Sep 23 '22 12:09

Pranab Sharma