MySQL 实现调用外部程序和系统命令

MySQL 实现调用外部程序和系统命令


Refer:

http://www.cnblogs.com/yunsicai/p/4080864.html


1) Download lib_mysqludf_sys

    $ git clone https://github.com/mysqludf/lib_mysqludf_sys.git

2) 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-dev

5) Compile on the mysql linux server:

    $ sudo make install

    That will generate and copy lib_mysqludf_sys.so into $(LIBDIR)


**********************************************************************************************

或者省去上面的步骤,直接下载:

ftp://pub:[email protected]/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 xxx

    mysql> source ./lib_mysqludf_sys.sql

7) 测试

mysql> select sys_eval('id');
+----------------+
| sys_eval('id') |
+----------------+
|                       |
+----------------+
1 row in set (0.00 sec)
发现不起作用。这是由于apparmor控制引起的。AppArmor 是一个类似于selinux  的东西,主要的作用是设置某个可执行程序的访问控制权限,可以限制程序 读/写某个目录/文件,打开/读/写网络端口等。(参考: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强大的功能了!


你可能感兴趣的:(mysql)