[文章作者:张宴 本文版本:v1.4 最后修改:2010.06.11 转载请注明原文链接:http://blog.zyan.cc/read.php/362/]

  Tokyo Cabinet 是日本人 平林幹雄 开发的一款 DBM 数据库,该数据库读写非常快,哈希模式写入100万条数据只需0.643秒,读取100万条数据只需0.773秒,是 Berkeley DB 等 DBM 的几倍。

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



  Tokyo Tyrant 是由同一作者开发的 Tokyo Cabinet 数据库网络接口。它拥有Memcached兼容协议,也可以通过HTTP协议进行数据交换。

  Tokyo Tyrant 加上 Tokyo Cabinet,构成了一款支持高并发的分布式持久存储系统,对任何原有Memcached客户端来讲,可以将Tokyo Tyrant看成是一个Memcached,但是,它的数据是可以持久存储的。这一点,跟新浪的Memcachedb性质一样。

  相比Memcachedb而言,Tokyo Tyrant具有以下优势:

  1、故障转移:Tokyo Tyrant支持双机互为主辅模式,主辅库均可读写,而Memcachedb目前支持类似MySQL主辅库同步的方式实现读写分离,支持“主服务器可读写、辅助服务器只读”模式。

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

  这里使用 $memcache->addServer 而不是 $memcache->connect 去连接 Tokyo Tyrant 服务器,是因为当 Memcache 客户端使用 addServer 服务器池时,是根据“crc32(key) % current_server_num”哈希算法将 key 哈希到不同的服务器的,PHP、C 和 python 的客户端都是如此的算法。Memcache 客户端的 addserver 具有故障转移机制,当 addserver 了2台 Memcached 服务器,而其中1台宕机了,那么 current_server_num 会由原先的2变成1。

  引用 memcached 官方网站和 PHP 手册中的两段话:
引用
http://www.danga.com/memcached/
If a host goes down, the API re-maps that dead host's requests onto the servers that are available.

http://cn.php.net/manual/zh/function.Memcache-addServer.php
Failover may occur at any stage in any of the methods, as long as other servers are available the request the user won't notice. Any kind of socket or Memcached server level errors (except out-of-memory) may trigger the failover. Normal client errors such as adding an existing key will not trigger a failover.




  2、日志文件体积小:Tokyo Tyrant用于主辅同步的日志文件比较小,大约是数据库文件的1.3倍,而Memcachedb的同步日志文件非常大,如果不定期清理,很容易将磁盘写满。



  3、超大数据量下表现出色:

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

  但是,Tokyo Tyrant 也有缺点:在32位操作系统下,作为 Tokyo Tyrant 后端存储的 Tokyo Cabinet 数据库单个文件不能超过2G,而64位操作系统则不受这一限制。所以,如果使用 Tokyo Tyrant,推荐在64位CPU、操作系统上安装运行。



  一、安装
  1、首先编译安装tokyocabinet数据库
wget http://www.1978th.net/tokyocabinet/tokyocabinet-1.4.45.tar.gz
tar zxvf tokyocabinet-1.4.45.tar.gz
cd tokyocabinet-1.4.45/
./configure
#注:在32位Linux操作系统上编译Tokyo cabinet,请使用./configure --enable-off64代替./configure,可以使数据库文件突破2GB的限制。
#./configure --enable-off64
make
make install
cd ../


  2、然后编译安装tokyotyrant
wget http://www.1978th.net/tokyotyrant/tokyotyrant-1.1.40.tar.gz
tar zxvf tokyotyrant-1.1.40.tar.gz
cd tokyotyrant-1.1.40/
./configure
make
make install
cd ../




  二、配置
  1、创建tokyotyrant数据文件存放目录
mkdir -p /ttserver/


  2、启动tokyotyrant的主进程(ttserver)
  (1)、单机模式
ulimit -SHn 51200
ttserver -host 127.0.0.1 -port 11211 -thnum 8 -dmn -pid /ttserver/ttserver.pid -log /ttserver/ttserver.log -le -ulog /ttserver/ -ulim 128m -sid 1 -rts /ttserver/ttserver.rts /ttserver/database.tcb#lmemb=1024#nmemb=2048#bnum=10000000


  (2)、双机互为主辅模式
  服务器192.168.1.91:
ulimit -SHn 51200
ttserver -host 192.168.1.91 -port 11211 -thnum 8 -dmn -pid /ttserver/ttserver.pid -log /ttserver/ttserver.log -le -ulog /ttserver/ -ulim 128m -sid 91 -mhost 192.168.1.92 -mport 11211 -rts /ttserver/ttserver.rts /ttserver/database.tcb#lmemb=1024#nmemb=2048#bnum=10000000


  服务器192.168.1.92:
ulimit -SHn 51200
ttserver -host 192.168.1.92 -port 11211 -thnum 8 -dmn -pid /ttserver/ttserver.pid -log /ttserver/ttserver.log -le -ulog /ttserver/ -ulim 128m -sid 92 -mhost 192.168.1.91 -mport 11211 -rts /ttserver/ttserver.rts /ttserver/database.tcb#lmemb=1024#nmemb=2048#bnum=10000000


  (3)、参数说明
  ttserver [-host name] [-port num] [-thnum num] [-tout num] [-dmn] [-pid path] [-log path] [-ld|-le] [-ulog path] [-ulim num] [-uas] [-sid num] [-mhost name] [-mport num] [-rts path] [dbname]

  -host name : 指定需要绑定的服务器域名或IP地址。默认绑定这台服务器上的所有IP地址。
  -port num : 指定需要绑定的端口号。默认端口号为1978
  -thnum num : 指定线程数。默认为8个线程。
  -tout num : 指定每个会话的超时时间(单位为秒)。默认永不超时。
  -dmn : 以守护进程方式运行。
  -pid path : 输出进程ID到指定文件(这里指定文件名)。
  -log path : 输出日志信息到指定文件(这里指定文件名)。
  -ld : 在日志文件中还记录DEBUG调试信息。
  -le : 在日志文件中仅记录错误信息。
  -ulog path : 指定同步日志文件存放路径(这里指定目录名)。
  -ulim num : 指定每个同步日志文件的大小(例如128m)。
  -uas : 使用异步IO记录更新日志(使用此项会减少磁盘IO消耗,但是数据会先放在内存中,不会立即写入磁盘,如果重启服务器或ttserver进程被kill掉,将导致部分数据丢失。一般情况下不建议使用)。
  -sid num : 指定服务器ID号(当使用主辅模式时,每台ttserver需要不同的ID号)
  -mhost name : 指定主辅同步模式下,主服务器的域名或IP地址。
  -mport num : 指定主辅同步模式下,主服务器的端口号。
  -rts path : 指定用来存放同步时间戳的文件名。

  如果使用的是哈希数据库,可以指定参数“#bnum=xxx”来提高性能。它可以指定bucket存储桶的数量。例如指定“#bnum=1000000”,就可以将最新最热的100万条记录缓存在内存中:
ttserver -host 127.0.0.1 -port 11211 -thnum 8 -dmn -pid /ttserver/ttserver.pid -log /ttserver/ttserver.log -le -ulog /ttserver/ -ulim 128m -sid 1 -rts /ttserver/ttserver.rts /ttserver/database.tch#bnum=1000000


  如果大量的客户端访问ttserver,请确保文件描述符够用。许多服务器的默认文件描述符为1024,可以在启动ttserver前使用ulimit命令提高这项值。例如:
ulimit -SHn 51200


  3、停止tokyotyrant(ttserver)
ps -ef | grep ttserver

  找到ttserver的进程号并kill,例如:
kill -TERM 2159




  三、调用
  1、任何Memcached客户端均可直接调用tokyotyrant。

  2、还可以通过HTTP方式调用,下面以Linux的curl命令为例,介绍如何操作tokyotyrant:
  (1)、写数据,将数据“value”写入到“key”中:
curl -X PUT http://127.0.0.1:11211/key -d "value"


  (2)、读数据,读取“key”中数据:


  (3)、删数据,删除“key”:




  附:文章修改历史

  ● [2008年08月07日] [Version 1.0] 撰写本文

  ● [2008年10月16日] [Version 1.1] Tokyo Cabinet 版本升级到 1.3.12;Tokyo Tyrant 版本升级到 1.1.5

  ● [2008年12月04日] [Version 1.2] Tokyo Cabinet 版本升级到 1.3.22;Tokyo Tyrant 版本升级到 1.1.8

  ● [2008年07月06日] [Version 1.3] Tokyo Cabinet 版本升级到 1.4.28;Tokyo Tyrant 版本升级到 1.1.29

  ● [2010年06月11日] [Version 1.4] Tokyo Cabinet 版本升级到 1.4.45;Tokyo Tyrant 版本升级到 1.1.40;默认示例改为B+Tree数据库;增加32位Linux操作系统上编译Tokyo cabinet的configue配置。





技术大类 » Cache与存储 | 评论(124) | 引用(7) | 阅读(173256)
小小
2010-9-13 18:13
QQ群12304685 交流key-value存储引擎tokyo cabinet  
平林干雄的日语BLOG翻译计划:很多TT/TC的细节在日语BLOG中很详细。请懂日语的朋友予支持
小小
2010-9-13 18:14
平林干雄的日语BLOG翻译计划:很多TT/TC的细节在日语BLOG中很详细。请懂日语的朋友予支持

交流key-value存储引擎tokyo cabinet  QQ群交流 12304685
小小 Email Homepage
2010-9-13 18:16
平林干雄的日语BLOG翻译计划:很多TT/TC的细节在日语BLOG中很详细。请懂日语的朋友予支持

交流key-value存储引擎tokyo cabinet  QQ群交流 12304685
小小 Email Homepage
2010-9-13 18:16
平林干雄的日语BLOG翻译计划:很多TT/TC的细节在日语BLOG中很详细。请懂日语的朋友予支持

交流key-value存储引擎tokyo cabinet  QQ群交流 12304685
小小 Email Homepage
2010-9-13 18:16
平林干雄的日语BLOG翻译计划:很多TT/TC的细节在日语BLOG中很详细。请懂日语的朋友予支持

交流key-value存储引擎tokyo cabinet  QQ群交流 12304685
lizheng473
2010-9-25 17:57
仁兄可不可以不这么帅 狂顶
Deeka Email Homepage
2010-10-28 18:29
晏哥,有个问题想请教一下。我在使用MemcacheClient访问Tyrant的时间,往里set 800万数据(分段,每次set 10w),最后统计出来,每次都会有1-10w数据是set失败的。针对这个问题有什么解决办法吗?
deeka Email Homepage
2010-10-29 22:17
tt写入数据失败的问题找到了原因,问题在不应该用mc客户端来连接tt,解决的办法和一些其他的tt使用心得,我记录在http://www.hdj.me/post/tokyo-tyrant-faq.php,有兴趣的过去看看:)
jason Email
2010-10-31 22:48
在使用TT的时候, 主要是不知道 怎么选择指定的 tchdb,tcbdb,tcfdb,tctdb.h,tcadb? 用的是 java客户端api
为什么
2010-11-26 16:23
用tt 启动后,若是关闭,再启动,数据就不能插入,若是把原来的数据文件删除掉就又可以了,是什么原因呢?
liulin_good Email
2011-4-15 13:28
楼上的那个网络拓扑图是 用什么工具画的呢?
phpor Email
2011-4-15 14:51
如果使用的是哈希数据库,可以指定参数“#bnum=xxx”来提高性能。它可以指定bucket存储桶的数量。例如指定“#bnum=1000000”,就可以将最新最热的100万条记录缓存在内存中

这个解释有误吧? bnum 不是说“将最新最热的100万条记录缓存在内存中"吧
louis vuitton uk Email Homepage
2011-11-23 09:04
This louis vuitton uk for sale belongs to the sounding just what are termed as Louis Vuitton vintage best sellers, many other products and services for the reason that range appearing companies.You will easily notice the unfold zippers of this coach outlet store online. That is the decoration. There are some inside pockets for you as well. They are easy to match your clothes and to carry.Let us inspire your inner beauty with fine christian louboutin sale. Purse the elegance in bridal wedding. Enjoy the fashion.
rusherding
2012-1-5 10:47
crc32(key) % current_server_num
这个不太对,会有多种算法,这个只是其中一种,不过好像不是默认的……
coach factory outlet Email Homepage
2012-5-17 11:42
Today, following half a century, mentor leather-bases coach factory outlet continues to be the delicate craft of leather-based master is accountable for,Would you like to meet more friends, or go with the times? If yes, coach factory online is opening welcome doors to you.in the market you definitely can find various colorways that are designed in as well as the high quality that applied in. For most of you would like to come. So just come to our coach factory outlet online store to choose one.
louis vuitton sale Email Homepage
2012-5-17 11:42
Louis Vuitton belt at louis vuitton sale is one kind of fashion accessory with high cost performance among the Louis Vuitton accessories.Offering quality LV products with favorable prices, louis vuitton outlet store is at your service. Hurry up, or you can not seize the chance.in fact, louis vuitton is one of the most famous fashion design master.he opened the fist suitcase shop called after his name.
coach outlet online Email Homepage
2012-5-17 11:42
I heard of coach outlet online through the advertisement when I was shopping. And now I often brow the webpage and buy Coach bags online.It is a symbol regarding position not to mention nature.Here I would like to launch a excellent bags pertaining to business men.Which may be coach outlet store.coach outlet has always been simple,durable style features to win consumers.The products are more flexible,with easy bleaching,wear characteristics,and simply use a damp cloth.
分页: 4/7 第一页 上页 1 2 3 4 5 6 7 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]