MySQL ã®ããªã¬ããã·ã§ã«ã¹ã¯ãªãããå®è¡ãã
ä¹
ã
ã®æ´æ°ã
MySQL ã®ç¹å®ã®ãã¼ãã«ã®ãã¼ã¿å¤æ´ãæ¤ç¥ãã¦å®è¡ãããå¦çããã£ãã®ã§ãããªã¬ã使ã£ã¦ãªãã¨ãåºæ¥ãªãã調ã¹ã¦ã¿ã¾ããã
system é¢æ°ã使ã
æå system é¢æ°(\!) ã使ã£ã¦ã§ããããªä»¥ä¸ã®è¨äºãè¦ã¤ãã¾ããã
MySQL :: Re: Can triggers call SYSTEM?
http://forums.mysql.com/read.php?99,170973,236208#msg-236208
ããmysql client ä¸ããã¯ä¸æãåããã®ã®ãã¢ããªããã® DB å¤æ´ã«åå¿ãã¦ãããã»ã»ã»ã
ã¡ãã£ã¨èª¿ã¹ã¦ã¿ãã¨ä»¥ä¸ã®ãããªè¨äºãããã¾ããã
Using the following DOES NOT work!
\! /bin/ls >> /log/yourlog.txt
The !\ (bang or exclamation point + backslash) is a mysql console feature for running commands in the console.
http://patternbuffer.wordpress.com/2012/09/14/triggering-shell-script-from-mysql/
These are ignored on the actual server. Do not be fooled.
Since you are testing in the console, it will appear to work once, but the trigger will not cause your code to fire.
ã©ããããªã¬å
ã® system é¢æ°ã¯ç«¯æ«ä¸ããããåããªãããã§ãã
ã©ããããã®ãã»ã»ã»ã
lib_mysqludf_sys
æ´ã«èª¿ã¹ã¦ããã¨ã以ä¸ã®è¨äºã§ UDF ãä½ãã°åºæ¥ãã¨ããæ å ±ãçºè¦ã
MySQL 㧠UDF ãå®ç¾©ããã - ã«ããã«ããããã°
http://aoking.hatenablog.jp/entry/20120824/1345778096
ãã ãUDF ãèªåã§ä½ãã®ã¯ã¡ãã£ã¨ããã©ãã»ã»ã»ã
ã¨æã£ã¦èª°ãä½ã£ã¦ãã人ãããªããæ¢ãã¦ã¿ãããã¯ãããã¾ããã
mysqludf/lib_mysqludf_sys
https://github.com/mysqludf/lib_mysqludf_sys
ã¨ããããã§æ©éå°å
¥ãã¦ã¿ã¾ãã
試ããç°å¢ã¯ä»¥ä¸ã§ãã
ã¡ãªã¿ã«æ¬æ¥ sudo ããä½æ³ã§ããã /usr/local/src/ ã«ã½ã¼ã¹ç½®ãå ´åã«é¢åãªã®ã§ä»¥ä¸ root ã§ä½æ¥ãã¦ãã¾ãã
(ããããå ´åæ¬å½ã¯ã©ãããã®ããããã§ããããï¼)
ã¾ã㯠GitHub ãã cloneã
# cd /usr/local/src/ # git clone https://github.com/mysqludf/lib_mysqludf_sys.git # cd lib_mysqludf_sys/
GitHub ã® issue ã«æãããã¦ãã¾ããã 64 bit ç°å¢ã®å ´åããã®ã¾ã¾ã§ã¯ã³ã³ãã¤ã«ã§ããªãã®ã§ Makefile ãç·¨éãã¾ãã
# vim Makefile
gcc ã®ã³ãã³ãã©ã¤ã³ã«ã¤ãã¦ã¯ä»¥ä¸ã® issue ã®ã³ã¡ã³ãã§ç´¹ä»ããã¦ãããã®ã«å¤æ´ãã¾ãã
https://github.com/mysqludf/lib_mysqludf_sys/issues/4#issuecomment-48470107
ã¾ããæå ã® MySQL ã§ã¯ plugin ãã£ã¬ã¯ããªãã/usr/lib/mysql/pluginãã ã£ãã®ã§ LIBDIR ã®å¤ãä¿®æ£ãã¦ãã¾ãã
LIBDIR=/usr/lib/mysql/plugin install: gcc -DMYSQL_DYNAMIC_PLUGIN -fPIC -Wall -I/usr/include/mysql -I. -shared lib_mysqludf_sys.c -o $(LIBDIR)/lib_mysqludf_sys.so
ä¿®æ£ãããã¤ã³ã¹ãã¼ã«ã
# ./install.sh
å®è¡ãã㨠mysql ã® root ã¦ã¼ã¶ã®ãã¹ã¯ã¼ããèãããã®ã§å
¥åããã¨ãã©ã°ã¤ã³ããã¼ããã㦠UDF ãç»é²ããã¾ãã
ç»é²ãããé¢æ°ã確èªãã¦ã¿ã¾ãã
# mysql -uroot -p -e "select * from mysql.func;" Enter password: +-----------------------+-----+---------------------+----------+ | name | ret | dl | type | +-----------------------+-----+---------------------+----------+ | lib_mysqludf_sys_info | 0 | lib_mysqludf_sys.so | function | | sys_get | 0 | lib_mysqludf_sys.so | function | | sys_set | 2 | lib_mysqludf_sys.so | function | | sys_exec | 2 | lib_mysqludf_sys.so | function | | sys_eval | 0 | lib_mysqludf_sys.so | function | +-----------------------+-----+---------------------+----------+
ç¡äºè¿½å ããã¾ããã
ãã ãåãã issue ã³ã¡ã³ãã§ææããã¦ãã¾ãããããã®ã¾ã¾ã§ã¯ apparmor ãéªéããã¦ä¸æãåä½ãã¾ããã
ãã¼ã«ã«ç°å¢ã¨ããäºãããããµã¯ãã¨ç¡å¹ã«ãã¦ãã¾ãã¾ãã
# ln -s /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/disable/ # apparmor_parser -R /etc/apparmor.d/usr.sbin.mysqld
ããã§ã¤ã³ã¹ãã¼ã«ã¯å®äºããã¯ããªã®ã§ã試ãã«ä½¿ã£ã¦ã¿ã¾ãã
# mysql -uroot -p -e "select sys_eval('id');" Enter password: +--------------------------------------------------+ | sys_eval('id') | +--------------------------------------------------+ | uid=117(mysql) gid=127(mysql) groups=127(mysql) | +--------------------------------------------------+
ã©ãããã¡ããã¨åãã¦ããããã§ãã
ããªã¬ãã使ã£ã¦ã¿ã
ããã§ãã£ã¨æ¬æ¥ããããã£ãããªã¬ããã®ã¹ã¯ãªããèµ·åã試ãã¦ã¿ã¾ãã
å®éã«ã¹ã¯ãªãããåããå ´åãç°å¢å¤æ°ãªã©ãé©åã«è¨å®ããå¿ è¦ãããå ´åãå¤ãã®ã§ãããã§ã¯ Ruby ã¹ã¯ãªãããå®è¡ããã·ã§ã«ã¹ã¯ãªãããä½æãããã®ã·ã§ã«ã¹ã¯ãªãããå®è¡ããããªã¬ãä½æãã¦ã¿ã¾ããã
Ruby ã¹ã¯ãªããã¨ã·ã§ã«ã¹ã¯ãªããã¯ä»¥ä¸ã®ãããªåç´ãªãã®ã§ãã
- example.sh
#!/bin/sh ruby /home/akishin/src/ruby/example.rb $@
- example.rb
# -*- coding: utf-8 -*- File.open('/tmp/ruby.log', 'a') { |f| f.puts ARGV.join(',') }
MySQL ä¸ã« example.sh ãå¼ã³åºãããªã¬ãä½æãã¾ãã
CONCAT ã§å®è¡ããã³ãã³ãæååãä½æããlib_mysqludf_sys ã§è¿½å ããã sys_eval é¢æ°ã§å®è¡ãã¦ãã¾ãã
DELIMITER // DROP TRIGGER IF EXISTS logging_notes; CREATE TRIGGER logging_notes AFTER INSERT ON notes FOR EACH ROW BEGIN DECLARE cmd CHAR(255); DECLARE result CHAR(255); SET cmd = CONCAT('/home/akishin/src/sh/example.sh ', NEW.id, ' ', NEW.title); SET result = sys_eval(cmd); END // DELIMITER ;
ããã§ç»é¢ããé©å½ã«ç»é²ãè¡ã㨠/tmp/ruby.log å ã« id ã«ã©ã ã®å¤ã¨ title ã«ã©ã ã®å¤ãè¨é²ãããããã«ãªãã¾ããï¼
å®éä»åèªåã®è¦ä»¶ã§ã¯ã«ã©ã ã®å¤ã¾ã§ã¯å¿ è¦ãªãã£ããã§ãããsys_eval ã§ããã°ã«ã©ã å¤ãã¹ã¯ãªããã«æ¸¡ããã®ã§æ´»ç¨ç¯å²ã¯åºããããã§ãã
åè
sql - Invoking a PHP script from a mysql trigger - Stack Overflow
http://stackoverflow.com/questions/1467369/invoking-a-php-script-from-a-mysql-trigger