mysql-udf-http 是一款简单的MySQL用户自定义函数,具有http_get()、http_post()、http_put()、http_delete()四个函数,可以在MySQL数据库中利用HTTP协议进行REST相关操作,它的安装方式如下:
-
下载并安转curl:
wget http://curl.haxx.se/download/curl-7.21.1.tar.gz tar zxvf curl-7.21.1.tar.gz cd curl-7.21.1/ ./configure --prefix=/usr make && make install cd ../
-
下载mysql-udf-http-1.0.tar.gz:(百度网盘:http://pan.baidu.com/s/1nuYZqR3)
tar zxvf mysql-udf-http-1.0.tar.gz cd mysql-udf-http-1.0/ ./configure --prefix=/usr/local/mysql --with-mysql=/usr/bin/mysql_config make && make install
注意:prefix记得改成你自己的mysql安装目录
-
正常的情况mysql-udf-http.so等文件将安装至/usr/local/mysql/lib/plugin下,
如果不是,那么先找到这个文件的位置:find / -name mysql-udf-http.so
加一个软连接:
ln -s /usr/lib64/mysql/lib/mysql/plugin/mysql-udf-http.so /usr/lib64/mysql/plugin/mysql-udf-http.so
-
安装成功后,进到mysql控制台,注册相关函数:
create function http_get returns string soname 'mysql-udf-http.so'; create function http_post returns string soname 'mysql-udf-http.so'; create function http_put returns string soname 'mysql-udf-http.so'; create function http_delete returns string soname 'mysql-udf-http.so';
-
在业务表中加入更新操作的触发器:
DELIMITER $$ DROP TRIGGER IF EXISTS test_update$$ CREATE TRIGGER test_update AFTER UPDATE ON sp_bal FOR EACH ROW BEGIN SET @tt_re = (SELECT http_get(CONCAT('http://192.168.99.200:8005/test?id=', OLD.id))); END; $$
常见错误解决:
1、如果mysql_config文件不存在(执行 find / -name mysql_config
,提示没有找到):yum install mysql-devel;
2、如果执行./configure --prefix=/usr/local/mysql --with-mysql=/usr/local/mysql/bin/mysql_config
时候报错:checking for DEPS… configure: error: Package requirements (libcurl >= 7.15) were not met:No package ‘libcurl’ found
如果是第一步安装cur成功,那么执行下面的命令:
[root@localhost mysql-udf-http-1.0]# which curl
/usr/bin/curl
[root@localhost mysql-udf-http-1.0]# find / -name libcurl.pc
/home/mysql/curl-7.21.1/libcurl.pc
/usr/lib/pkgconfig/libcurl.pc
[root@localhost mysql-udf-http-1.0]# cp /usr/lib/pkgconfig/libcurl.pc /usr/lib64/pkgconfig/
如果提示缺少libcurl,就安装curl:yum install curl*