Home > Database > Mysql Tutorial > body text

MysSQLMariaDB用trigger调用shell

WBOY
Release: 2016-06-07 16:45:42
Original
968 people have browsed it

MysSQLMariaDB用trigger调用shell

Windows环境下:

DROP FUNCTION IF EXISTS lib_mysqludf_sys_info;
DROP FUNCTION IF EXISTS sys_get;
DROP FUNCTION IF EXISTS sys_set;
DROP FUNCTION IF EXISTS sys_exec;
DROP FUNCTION IF EXISTS sys_eval;

CREATE FUNCTION lib_mysqludf_sys_info RETURNS string SONAME 'lib_mysqludf_sys.dll';
CREATE FUNCTION sys_get RETURNS string SONAME 'lib_mysqludf_sys.dll';
CREATE FUNCTION sys_set RETURNS int SONAME 'lib_mysqludf_sys.dll';
CREATE FUNCTION sys_exec RETURNS int SONAME 'lib_mysqludf_sys.dll';
CREATE FUNCTION sys_eval RETURNS string SONAME 'lib_mysqludf_sys.dll';

select sys_eval('ipconfig');

Linux环境下:

在mysql的触发器中执行一个外部程序。

步骤如下:

一、解压附件的压缩包之后

如果不想自己编译的话,,把lib_mysqludf_sys.so文件放到 mysql的lib/mysql/plugin/

目录下。

二、执行chcon -t texrel_shlib_t mysql/lib/mysql/plugin/lib_mysqludf_sys.so

三、创建函数

DROP FUNCTION IF EXISTS lib_mysqludf_sys_info;
DROP FUNCTION IF EXISTS sys_get;
DROP FUNCTION IF EXISTS sys_set;
DROP FUNCTION IF EXISTS sys_exec;
DROP FUNCTION IF EXISTS sys_eval;

CREATE FUNCTION lib_mysqludf_sys_info RETURNS string SONAME 'lib_mysqludf_sys.so';
CREATE FUNCTION sys_get RETURNS string SONAME 'lib_mysqludf_sys.so';
CREATE FUNCTION sys_set RETURNS int SONAME 'lib_mysqludf_sys.so';
CREATE FUNCTION sys_exec RETURNS int SONAME 'lib_mysqludf_sys.so';
CREATE FUNCTION sys_eval RETURNS string SONAME 'lib_mysqludf_sys.so';
四、测试

1、准备sh文件
在linux系统中执行下面的命令
su mysql
mkdir /mysqlUDFtest

cd mysqlUDFtest
vi test.sh

#/bin/sh
date > testlog.txt

chmod +x ./test.sh
2、准备数据库表和触发器
选择一个数据库执行如下命令:

CREATE TABLE test1(a1 INT) engine=InnoDB;
CREATE TABLE test2(a2 INT) engine=InnoDB;

DELIMITER |

DROP TRIGGER /*!50032 IF EXISTS */ `test`.`testref`|

create trigger `test`.`testref` BEFORE INSERT on `test`.`test1`
for each row BEGIN
DECLARE done INT DEFAULT 999;
set done = sys_exec("/mysqlUDFtest/test.sh");
INSERT INTO test2(a2)values(2);
END;
|

DELIMITER ;

3、向test1表中插入一条数据后,请在Linux命令行:用root用户,执行以下命令:find / -name testlog.txt

查找testlog.txt文件。

在 CentOS/RHEL/Scientific Linux 6 下安装 LAMP (Apache with MariaDB and PHP)

MariaDB Proxy读写分离的实现

Linux下编译安装配置MariaDB数据库的方法

CentOS系统使用yum安装MariaDB数据库

安装MariaDB与MySQL并存

MariaDB 的详细介绍:请点这里
MariaDB 的下载地址:请点这里

本文永久更新链接地址:

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template