[文章作者:张宴 本文版本: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>





技术大类 » 数据库技术 | 评论(375) | 引用(0) | 阅读(187584)
lirourou77 Email Homepage
2012-9-5 13:21
in a new mechanism of system to replace the old in medicine have medical mechanism, which is the cheap designer handbags      
cattle nets", and click "online voting" found that, the original vote is paid, 1 yuan 1 ticket.

"If you have to vote online, by pay treasure, net silver or cell phone pays fee." HeXianSheng cheap designer clothes        
said, let people will choose the doctors like this is a good thing, but if will pay it is stale, "so, mean who's money, who can election?"

【 operation 】 can buy tickets into batch quantity
discount designer handbags        

According to HeXianSheng provide web site, reporter found "purple cattle nets", "popular activities" column in the article 1 is: ningxiang county "open the door the medical" advanced unit and "top ten doctors, top ten nurses, top ten technicians" selection activities to officially wholesale designer handbags
Coach Email Homepage
2012-11-3 14:41
"The Coach Outletengines that will likely power the J-31 we do know a bit mor.Coach Outlet  Those engines were actually revealed at the Zhuhai showCoach Outlet in 2008," Fisher said referring to an annual China air show. He beCoach Outletlieves the new J-31 engine is undergoing preliminary
north face outlet Email Homepage
2012-11-6 09:39
diapers,adult incontinence products,feminine hygiene products,and jeckluckp North Face Outlet personal care markets with growth.According to the The North Face Outlet relevant data,and The North Face Outlet the The North Face Outlet Turkish nonwovens in 2010 the The North Face Outlet total production capacity of 18 North Face Outlet Store million tons.Baby diaper market annually consume about 4 billion
hrjdd Email Homepage
2012-11-12 14:11
provide superior innovative fabric flowers,but the The North Face Outlet market is The North Face Outlet payment jecklucku <a href="http://www.thenorthfaceoutletdenali.com/">The North Face Denali Outlet</a> will not be made for the The North Face Outlet product.Canada load coverage solutions company <a href="http://www.thenorthfaceoutletdenali.com/">The North Face Outlet</a> president Brian said,as long as the The North Face Outlet government agencies continue to <a href="http://www.thenorthfaceoutletdenali.com/">The North Face Outlet Store</a> requirements the The North Face Outlet automobile coverage system suppliers and endproduct
WERW Email Homepage
2012-11-17 13:30
only 26 million bales.At present,the The North Face Outlet domestic market is The North Face Outlet almost no The North Face Outlet Store inventory available should the The North Face Outlet domestic industry,which is The North Face Outlet a special The North Face Outlet case.guarantee The North Face Outlet selling secret lies in the The North Face Outlet social network,this sounds North Face Outlet Store very strange? from Chicago online sales of The North Face Outlet website Threadless versed in jecklucks
fdgfg
2012-11-21 10:19
You could make some sort of crownless model of George Washington's three-cornered cheap hats quickly, making use of construction paper. Utilize it for a college perform or maybe a costume party. It will carry collectively for a minimum of 2-3 makes use of, or perhaps laminate flooring the three pieces that can end up being the factors on the hats factory when you assemble this should you prefer a stronger version. If you want to generate a experienced model, your own document cheap hats online has to be your design template.
磨延城 Email Homepage
2013-10-21 20:34
磨途歌学习了
张三
2013-11-5 10:34
感谢高人开发的这个模块, 这样模块相当有用,解决了我的很多问题,非常佩服!
www.gegejiejie.com Homepage
2014-5-26 22:58
www.gegejiejie.com
ctbsea Email
2014-6-16 17:51
view plainprint?/* HTTP GET、POST方式提交关键词“xoyo”到百度移动搜索 */  SELECT http_get('http://m.baidu.com/s?word=xoyo&pn=0');  SELECT http_post('http://m.baidu.com/s','word=xoyo&pn=0');  上面那种请求方式 可以获得结果 下面那种请求 获取不了 返回结果为空 请问下这是怎么回事
winie
2014-8-18 10:30
有没基于Binlog的解决方案呢?
Steven Email Homepage
2015-3-19 02:29
Thank you for this post Weight Loss Tea
sdfsdsdf Email Homepage
2015-3-20 05:37
zmonebird
2015-10-29 20:49
最近一直在配这个环境,现在遇到两个个问题:在slave数据库上添加触发器,当master上有更新操作时,触发器触发了两次,按道理master上是没有配触发器的。另一个问题是:如果触发器执行http请求,而这个请求是不存在的路径,那么数据库就会宕机。第一次接触项目,太技术的问题没有经验
iis
2017-1-5 10:17
stupid居然是同一届的校友,好像我们见过。你已经在创业的路上了,赞。我在创业半路上:)有空聊聊创业心得,我微信zhu8066
july
2017-9-20 11:30
echo "/usr/local/webserver/mysql/lib/mysql/" > /etc/ld.so.conf.d/mysql.conf如果你的*.so文件在/usr/local/webserver/mysql/lib/目录下,就写成echo "/usr/local/webserver/mysql/lib/mysql/" > /etc/ld.so.conf.d/mysql.conf
pandaman
2017-12-22 16:32
zan
priscamelda Email Homepage
2019-12-4 17:50
Acquiring HealthCare Papers Writing Services doesn’t have to drain your finances since HealthCare Paper Assignment Services charges fairly and ensure clients receive Best Custom HealthCare Research Papers written from scratch. https://researchpapers247.com/health-care-paper/
TopEssayWriting Homepage
2020-1-28 13:29
In an era where legitimate essay writing service are thriving, it has become increasingly difficult for students to identify with a reliable company that offers top essay writing and essay writing service accepting paypal.
分页: 5/19 第一页 上页 1 2 3 4 5 6 7 8 9 10 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]