[文章作者:张宴 本文版本:v1.0 最后修改:2010.09.08 转载请注明原文链接:http://blog.zyan.cc/mysql-udf-http/]

  Mysql-udf-http 是一款简单的MySQL用户自定义函数(UDF, User-Defined Functions),具有http_get()、http_post()、http_put()、http_delete()四个函数,可以在MySQL数据库中利用HTTP协议进行REST相关操作。

  项目网址http://code.google.com/p/mysql-udf-http/
  中文说明http://blog.zyan.cc/mysql-udf-http/
  使用环境:Linux操作系统,支持的MySQL版本:5.1.x 和 5.5.x。5.0.x未经测试。
  软件作者:张宴



  一、REST架构风格:

  REST(Representational State Transfer)是一种轻量级的Web Service架构风格,其实现和操作明显比SOAP和XML-RPC更为简洁,可以完全通过HTTP协议实现,还可以利用缓存Cache来提高响应速度,性能、效率和易用性上都优于SOAP协议。REST最早是由 Roy Thomas Fielding 博士2000年在论文《Architectural Styles and the Design of Network-based Software Architectures》中提出的,中文译文全文PDF点此下载。另外,有篇译文对REST做了一个简化说明。

  目前,REST架构风格的常见实现是基于HTTP协议及其四种基本方法(如POST、GET、PUT和DELETE)的。有人将HTTP协议的四种方法与CRUD原则相对应,CRUD原则对于资源只需要四种行为:Create(创建)、Read(读取)、Update(更新)和Delete(删除)就可以完成对其操作和处理。

  点击在新窗口中浏览此图片

  点击在新窗口中浏览此图片

  在Mysql-udf-http中,四个函数http_post()、http_get()、http_put()、http_delete()分别对应HTTP协议的POST、GET、PUT、DELETE四种方法。

  REST是一种架构风格,而不是协议或标准。HTTP协议“POST、GET、PUT、DELET”四种方法与CRUD原则“Create、Read、Update、Delete”四种行为的一一对应关系只是一种架构设计习惯,而不是规范。因此,POST方法也可以用来更新资源,PUT方法也可以用来创建资源,这就要看具体应用程序作者的定义了。例如Tokyo Tyrant除了支持Memcached协议外,还支持REST方式存取,PUT代表创建和更新,GET代表读取,DELETE代表删除(关于Tokyo Tyrant的安装使用请点击这儿)。

  目前国内外流行的Web 2.0应用API接口中,很多都支持REST架构风格。例如:新浪微博开放平台人人网API、Google OpenID、Flickr、Twitter、eBay、Facebook、Last.fm、del.icio.us、Yahoo Search、Amazon S3、Amazon EC2、Digg、Microsoft Bing、FriendFeed、PayPal、Foursquare,更多...

  当记录数成百上千万条时,通常采用 MySQL 分表减低数据库压力。但是,全部数据按点击数、精华、积分排序显示等功能,在MySQL 分表中则无法实现。编写 Mysql-udf-http 的最初目的,是为了在项目开发中,将 MySQL 各分表的数据自动同步到我们的 TCSQL 高速列表数据库,用来做列表查询、显示,内容页则根据ID直接查询各 MySQL 分表的内容。由于HTTP协议的通用性,通过 Mysql-udf-http 可以做更多的事情。

  通过Mysql-udf-http,你可以在MySQL中利用触发器,将MySQL的数据同步到支持REST的应用上。例如你有一个独立博客,你可以在文章表创建MySQL触发器,这样,在发表文章时,就可以将文章标题、URL自动同步到新浪微博、Twitter。你想用 Tokyo Tyrant 做缓存,也可以利用MySQL触发器在发生增、删、改时,将数据自动同步到 Tokyo Tyrant。详细配置方法本文第4节中会有介绍。



  二、Mysql-udf-http的安装与使用:

  1. 在Linux系统上安装Mysql-udf-http

  注意:“/usr/local/webserver/mysql/”是你的MySQL安装路径,如果你的MySQL安装路径不同,请自行修改。

ulimit -SHn 65535
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 ../

echo "/usr/local/webserver/mysql/lib/mysql/" > /etc/ld.so.conf.d/mysql.conf
/sbin/ldconfig
wget http://mysql-udf-http.googlecode.com/files/mysql-udf-http-1.0.tar.gz
tar zxvf mysql-udf-http-1.0.tar.gz
cd mysql-udf-http-1.0/
./configure --prefix=/usr/local/webserver/mysql --with-mysql=/usr/local/webserver/mysql/bin/mysql_config
make && make install
cd ../




  2. 通过命令行登陆进入MySQL

/usr/local/webserver/mysql/bin/mysql -S /tmp/mysql.sock




  3. 创建MySQL自定义函数

  mysql>


  4. 使用方法

  I. 函数描述:

  mysql>
  II. 示例 A:

  mysql>
  III. 示例

  通过MySQL触发器,利用mysql-udf-http和第三方UDF函数lib_mysqludf_json,自动同步数据到 Tokyo Tyrant。

  (1). 下载安装 lib_mysqludf_json 修改版:

  以下安装包适合32位Linux操作系统:
wget http://mysql-udf-http.googlecode.com/files/lib_mysqludf_json-i386.tar.gz
tar zxvf lib_mysqludf_json-i386.tar.gz
cd lib_mysqludf_json-i386/
# 如果你的MySQL安装路径不是/usr/local/webserver/mysql/,请修改以下路径。
cp -f lib_mysqludf_json.so /usr/local/webserver/mysql/lib/mysql/plugin/lib_mysqludf_json.so
cd ../


  以下安装包适合64位Linux操作系统:
wget http://mysql-udf-http.googlecode.com/files/lib_mysqludf_json-x86_64.tar.gz
tar zxvf lib_mysqludf_json-x86_64.tar.gz
cd lib_mysqludf_json-x86_64/
# 如果你的MySQL安装路径不是/usr/local/webserver/mysql/,请修改以下路径。
cp -f lib_mysqludf_json.so /usr/local/webserver/mysql/lib/mysql/plugin/lib_mysqludf_json.so
cd ../


  通过命令行登陆进入MySQL:
/usr/local/webserver/mysql/bin/mysql -S /tmp/mysql.sock


  mysql>  lib_mysqludf_json的详细用法请访问:http://www.mysqludf.org/lib_mysqludf_json/

  (2). 创建测试表

  mysql>
  (3). 为测试表创建触发器:

  mysql>
  (4). 将 MySQL 表和 Tokyo Tyrant 关联进行查询:

  mysql>


  5. 如何删除mysql-udf-http UDF函数:

  mysql>





技术大类 » 数据库技术 | 评论(371) | 引用(0) | 阅读(187401)
脱发怎么办 Homepage
2010-9-22 17:23
宴哥,从你这里学到不少东西,谢谢分享!
NFL Jerseys Email Homepage
2010-9-25 11:00
呵呵,路过,来学习下!谢谢博主!
dunk sb Email Homepage
2010-9-25 15:44
sleepy 很晕,没看懂!还是支持下!
wangjw302 Homepage
2010-9-26 13:38
太强大了    学习
nfl jerseys Email Homepage
2010-9-27 16:01
呵呵,过来看看!因为博客名字,一般人都是回忆过去,博主怎么回忆未来呢?
夏天
2010-10-1 23:05
频繁的使用触发器影响MySQL性能吗? 尤其是还要进行http_put()操作
xibeiwolf Email Homepage
2010-10-5 16:31
不知道这个有什么用处?
天哥
2010-10-12 12:02
select http_get('http://www.baidu.com/');

查询耗时1.37秒,是不是慢了点啊?  这个不会只适合内网吧
12
2010-10-17 16:08
mysql> SELECT json_object(id,addtime,title) FROM mytable WHERE id = 1 LIMIT 1;
ERROR 1123 (HY000): Can\'t initialize function \'json_object\'; Invalid json member name - name cannot be empty

mysql> select version();
+------------------+
| version()        |
+------------------+
| 5.1.50-community |
+------------------+
1 row in set (0.00 sec)
mysql> SELECT json_array(id,addtime,title) FROM mytable WHERE id = 1 LIMIT 1;
+------------------------------+
| json_array(id,addtime,title) |
+------------------------------+
| [1,18,"title"]               |
+------------------------------+
1 row in set (0.00 sec)
博客之家 Homepage
2010-10-18 13:22
好久没写文章了
多动症 Email Homepage
2010-10-21 21:33
你好!我第一来贵站,特来学习!我会持续关注贵站,定期回访!希望交个朋友,一起交流!
asderaa
2010-10-24 17:59
http的put delete真的好用吗?question
fashion_jewelry Email Homepage
2010-11-9 09:54
这里越来越热闹啦,拜读了~
lili
2010-11-16 19:38
我的mysql 安装版本是 5.0.45  , 在线业务, 安装的是 rpm包
我当前os 是 centos5.0.3
mysql 是 5.0.45
安装Mysql-udf-http的文档提示 安装Mysql-udf-http包的时候要执行下面这个
./configure --prefix=/usr/local/webserver/mysql --with-mysql=/usr/local/webserver/mysql/bin/mysql_config
结果发现: (我的mysql5.0.45是rpm安装的 (在线业务))
当前主目录是 /var/lib/mysql 不是上面的 /usr/local/webserver/mysql/b
所以我安装的时候死活找不到 -with-mysql=/usr/local/webserver/mysql/bin/mysql_config

于是我 find / -name "mysql_config"
/usr/bin/mysql_config------------------os 能找到

可惜还是发现错误
./configure --prefix=/var/lib/mysql --with-mysql=/usr/bin/mysql_config
checking for a BSD-compatible install... /usr/bin/install -c
----------------------中间过程是ok
最后有错误
ree... yes
checking for pkg-config... no
checking for DEPS... configure: error: The pkg-config script could not be found or is too old. Make sure it
is in your PATH or set the PKG_CONFIG environment variable to the full
path to pkg-config.

Alternatively, you may set the environment variables DEPS_CFLAGS
and DEPS_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

To get pkg-config, see <http://pkg-config.freedesktop.org/>.
See `config.log' for more details.

当前版本是 5.0.45 几乎我强制装上了 rpm -iUvh MySQL-server-community-5.0.45-0.rhel5.i386.rpm
  rpm -iUvh MySQL-client-community-5.0.45-0.rhel5.i386.rpm
  rpm -iUvh MySQL-devel-community-5.0.45-0.rhel5.i386.rpm
  rpm -iUvh MySQL-shared-community-5.0.45-0.rhel5.i386.rpm
  rpm -iUvh MySQL-shared-compat-5.0.45-0.rhel5.i386.rpm
还是失败啦 。

为什么会失败??-------------------------------希望能解释下
lili
2010-11-17 13:56
请问张宴   , 你的mysql 是二进制版本还是 源码编译的版本
我当初在线安装是 rpm包安装的 。 导致无法找到 /usr/local/webserver/mysql/bin/mysql_config。

这个情况有没有方法来安装Mysql-udf-http。

(问题1:可以把该版本的5.0.45的二进制版本的 bin/mysql_config 复制到 当初的rpm 的目录 /var/lib/mysql下
再执行提示
./configure --prefix=/usr/local/webserver/mysql --with-mysql=/usr/local/webserver/mysql/bin/mysql_config  ?
问题2: 如果 还是出现

checking for pkg-config... no
checking for DEPS... configure: error: The pkg-config script could not be found or is too old. Make sure it
is in your PATH or set the PKG_CONFIG environment variable to the full
path to pkg-config.

Alternatively, you may set the environment variables DEPS_CFLAGS
and DEPS_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

To get pkg-config, see <http://pkg-config.freedesktop.org/>.
See `config.log' for more details.

什么原因如何解决
lj
2010-11-22 06:40
建议使用phprpc,那个东东速度比这个快多了!!!
Email
2010-11-26 17:47
mysql> create function http_get returns string soname 'mysql-udf-http.so';
ERROR 1126 (HY000): Can't open shared library 'mysql-udf-http.so' (errno: 22 mysql-udf-http.so: cannot open shared object file: No such file or directory)
我也是这个错误 :
/mysql/lib/mysql/plugin/ 下面已经有 mysql-udf-http.so' 这个文件了 。但还是报错 、不知道怎么解决、、请教各位 !
广州太和镇 Email Homepage
2010-12-25 12:55
学习了。要时间来消化。
alpha
2010-12-30 14:05
Hi 安装mysqludf 的时候出现如下错误 是在64bit操作系统下 :

[root@swrac1 mysql]# ./bin/mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.45-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create function lib_mysqludf_json_info returns string soname 'lib_mysqludf_json.so';  
ERROR 2013 (HY000): Lost connection to MySQL server during query
mysql> 101230 13:39:28 mysqld_safe Number of processes running now: 0
101230 13:39:28 mysqld_safe mysqld restarted

mysql>
alpha
2010-12-30 16:33
已经解决
分页: 2/19 第一页 上页 1 2 3 4 5 6 7 8 9 10 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]