MySQL 实现调用外部程序和系统命令
Refer:
http://www.cnblogs.com/yunsicai/p/4080864.html
1) Download lib_mysqludf_sys
$ git clone https://github.com/mysqludf/lib_mysqludf_sys.git2) get mysql plugin dir as LIBDIR:
mysql > show variables like 'plugin_dir';
+--------------+--------------------------+
|variable_name | value |
+--------------+--------------------------+
| plugin_dir | /usr/lib64/mysql/plugin |
+--------------+--------------------------+
3) Modify Makefile as below: 编译64位的so
LIBDIR=/usr/lib64/mysql/plugin
install:
gcc -m64 -fPIC -Wall -I/usr/include/mysql -I. -shared lib_mysqludf_sys.c -o $(LIBDIR)/lib_mysqludf_sys.so
4) If need, install:
$ apt-get install libmysqlclient-dev5) Compile on the mysql linux server:
$ sudo make installThat will generate and copy lib_mysqludf_sys.so into $(LIBDIR)
**********************************************************************************************
或者省去上面的步骤,直接下载:
ftp://pub:pub@pepstack.com/tarball/lib_mysqludf_sys.tar.gz
里面包括了已经编译好的64位 lib_mysqludf_sys.so,仅仅要拷贝到 plugin_dir 文件夹下就可以。
**********************************************************************************************
6) Install the library 'lib_mysqludf_sys.so' from terminal using following command:
$ mysql -u root -p??? -h xxxmysql> source ./lib_mysqludf_sys.sql
7) 測试
mysql> select sys_eval('id');
+----------------+
| sys_eval('id') |
+----------------+
| |
+----------------+
1 row in set (0.00 sec)
发现不起作用。这是因为apparmor控制引起的。AppArmor 是一个相似于selinux 的东西。基本的作用是设置某个可运行程序的訪问控制权限。能够限制程序 读/写某个文件夹/文件。打开/读/写网络port等。(參考:http://www.oschina.net/p/apparmor)
运行以下的命令:
$ sudo ln -s /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/disable/ $ sudo apparmor_parser -R /etc/apparmor.d/usr.sbin.mysqld再次測试:
mysql> mysql> select sys_eval('id');
+--------------------------------------------------+
| sys_eval('id') |
+--------------------------------------------------+
| uid=114(mysql) gid=125(mysql) groups=125(mysql) |
+--------------------------------------------------+
1 row in set (0.01 sec)
成功!
8)实战应用
创建文件夹:
$ sudo mkdir /usr/local/logserver/mysqludf $ sudo vi /usr/local/logserver/mysqludf/test.sh
test.sh内容例如以下:
#!/bin/sh # # mysqludf-test.sh # # # 2017-02-11 ######################################################################## _file=$(readlink -f $0) _cdir=$(dirname $_file) _name=$(basename $_file) echo "create date file:" date > ${_cdir}/test.log echo "${_cdir}/test.log" exit 21
设置权限:
$ sudo chown mysql:mysql -R /usr/local/logserver/mysqludf $ sudo chmod a+x /usr/local/logserver/mysqludf/test.sh
确保mysqludf及test.sh都是mysql:mysql
9)測试sys_eval:
mysql> select sys_eval('/usr/local/logserver/mysqludf/test.sh'); +-----------------------------------------------------------+ | sys_eval('/usr/local/logserver/mysqludf/test.sh') | +-----------------------------------------------------------+ | create date file: /usr/local/logserver/mysqludf/test.log | +-----------------------------------------------------------+ 1 row in set (0.01 sec)可见sys_eval把全部的echo输出显示出来。
10)測试sys_exec:
mysql> select sys_exec('/usr/local/logserver/mysqludf/test.sh'); +---------------------------------------------------+ | sys_exec('/usr/local/logserver/mysqludf/test.sh') | +---------------------------------------------------+ | 5376 | +---------------------------------------------------+ 1 row in set (0.00 sec)
sys_exec运行test.sh之后的返回值是5376 (=256*exit 21)。因为test.sh 最后一句的:exit 21。
所以,在mysql过程里尽量使用sys_exec以获取脚本(test.sh)运行的返回值。
把test.sh改动成你的脚本,就能够使用mysqludf强大的功能了。