How to set Trigger on your MySQL table in CentOS

Hesam Moosapour
1 min readJun 15, 2024

--

Imagine in a scenario we need to set a trigger on a table so that everytime a row gets inserted to our table, it would run a specific file for us:

If we don’t have 64 version library for executing command function , so we need to install it :

sudo yum groupinstall "Development Tools"
sudo yum install mysql-devel
git clone https://github.com/mysqludf/lib_mysqludf_sys.git
cd lib_mysqludf_sys
make clean
CFLAGS="-m64" make
sudo cp lib_mysqludf_sys.so /usr/lib64/mysql/plugin/
file /usr/lib64/mysql/plugin/lib_mysqludf_sys.so

then we can go ahead to mysql database , and in func table , we can add our own function here:

CREATE FUNCTION sys_exec RETURNS INT SONAME 'lib_mysqludf_sys.so';

And finally we set this trigger in our mariadb:

DELIMITER $$

CREATE TRIGGER after_insert_your_table_name
AFTER INSERT ON your_table_name FOR EACH ROW
BEGIN
DECLARE cmd CHAR(255);
DECLARE result INT;

-- Set the command to execute your PHP script and redirect output to a log file
SET cmd = '/usr/bin/php /var/project/my_script.php > /tmp/trigger_log 2>&1';

-- Execute the command using sys_exec (Make sure sys_exec is enabled and available in your MySQL environment)
SET result = sys_exec(cmd);
END$$

DELIMITER ;

Make sure to change or modify the code to suit your own situation.

Hope it was useful.
Good luck

--

--

Hesam Moosapour

Full Stack Web Developer , Interested in evolutionary biological anthropology on genders ; Men are good as are you.