MySQL implementation calls external programs and system commands
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: Compile 64-bit 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 would generate and copy lib_mysqludf_sys.so into $ (libdir)
**********************************************************************************************
or omit the above steps to download directly:
Ftp://pub:[email protected]/tarball/lib_mysqludf_sys.tar.gz
It includes the compiled 64-bit lib_mysqludf_sys.so, just copy it to the Plugin_dir folder.
**********************************************************************************************
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) test
Mysql> Select Sys_eval (' id ');
+----------------+
| Sys_eval (' id ') |
+----------------+
| |
+----------------+
1 row in Set (0.00 sec)
Discovery does not work. This is due to apparmor control. AppArmor is a similar thing to SELinux. The basic function is to set access control permissions for a running program. Ability to restrict program read/write to a folder/file. Open/Read/write network port and so on. (References: Http://www.oschina.net/p/apparmor)
Run the following command:
$ sudo ln-s/etc/apparmor.d/usr.sbin.mysqld/etc/apparmor.d/disable/$ sudo apparmor_parser-r/etc/apparmor.d/ Usr.sbin.mysqld
Test again:
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)
Success!
8) Practical Application
To create a folder:
$ sudo mkdir/usr/local/logserver/mysqludf$ sudo vi/usr/local/logserver/mysqludf/test.sh
Test.sh content such as the following:
#!/bin/sh## mysqludf-test.sh### 2017-02-11###################################################################### # #_file =$ (readlink-f $) _cdir=$ (dirname $_file) _name=$ (basename $_file) echo "Create date File:" Date > ${_cdir}/ Test.logecho "${_cdir}/test.log" Exit 21
Set permissions:
$ sudo chown mysql:mysql-r/usr/local/logserver/mysqludf$ sudo chmod a+x/usr/local/logserver/mysqludf/test.sh
Make sure MYSQLUDF and test.sh are all mysql:mysql.
9) Test 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)
The sys_eval shows the full echo output.
10) Test 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)
The return value after Sys_exec runs test.sh is 5376 (=256*exit 21). Because the last sentence of test.sh: Exit 21.
So, try to use sys_exec in the MySQL process to get the return value of the script (test.sh) run.
By changing test.sh into your script, you'll be able to use MYSQLUDF's powerful features.
MySQL implementation calls external programs and system commands