免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 985 | 回复: 0
打印 上一主题 下一主题

memcache的安装 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-10-23 12:18 |只看该作者 |倒序浏览

Memcached是什么?
Memcached是由Danga Interactive开发的,高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。
Memcached能缓存什么?
通过在内存里维护一个统一的巨大的hash表,Memcached能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。
Memcached快么?
非常快。Memcached使用了libevent(如果可以的话,在linux下使用epoll)来均衡任何数量的打开链接,使用非阻塞的网络I/O,对内部对象实现引用计数(因此,针对多样的客户端,对象可以处在多样的状态), 使用自己的页块分配器和哈希表, 因此虚拟内存不会产生碎片并且虚拟内存分配的时间复杂度可以保证为O。
memcache安装:
由于memcached需要libevent,所以先安装libevent.
下载:
libevent
最新版本
libevent-1.3d.tar.gz
  
memcached
最新版本:
memcached-1.2.2.tar.gz
下载目录: /www/src
view plain
copy to clipboard
print
?
cd /www/src/   
tar zxvf libevent-1.3d.tar.gz   
cd libevent-1.3d   
./configure --prefix=/usr   //指定目录   
make   
make install   
tar zxvf memcached-1.2.2.tar.gz   
cd memcached-1.2.2   
./configure --with-libevent=/usr //必须指定libevent目录   
make   
make install    cd /www/src/ tar zxvf libevent-1.3d.tar.gz cd libevent-1.3d ./configure --prefix=/usr   //指定目录 make make install tar zxvf memcached-1.2.2.tar.gz cd memcached-1.2.2 ./configure --with-libevent=/usr //必须指定libevent目录 make make install
安装完成之后,memcached 应该在 /usr/bin/memcached
运行memcached守护进程:
/usr/bin/memcached -d -m 128 -l localhost -p 11211 -u root    /usr/bin/memcached -d -m 128 -l localhost -p 11211 -u root
-d 以守护程序(daemon)方式运行 memcached;
-m 设置 memcached 可以使用的内存大小,单位为 M;
-l 设置监听的 IP 地址,如果是本机的话,通常可以不设置此参数;
-p 设置监听的端口,默认为 11211,所以也可以不设置此参数;
-u 指定用户;

如果遇到运行memcached时遇到问题,错误提示如下:
view plain
copy to clipboard
print
?
/usr/bin/memcached/bin/memcached: error while loading shared libraries: libevent-1.3d.so.1: cannot open shared object file: No such file or directory    /usr/bin/memcached/bin/memcached: error while loading shared libraries: libevent-1.3d.so.1: cannot open shared object file: No such file or directory
则运行 LD_DUBUG=libs 就可以知道memcached启动时库的加载路径。具体如下:
LD_DEBUG=libs /usr/bin/memcached/bin/memcached -v   
21404: find library=libpthread.so.0 [0]; searching   
21404: search cache=/etc/ld.so.cache   
21404: trying file=/lib/tls/libpthread.so.0   
21404:   
21404: find library=libevent-1.3d.so.1 [0]; searching   
21404: search cache=/etc/ld.so.cache   
21404: search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib (system search path)   
21404: trying file=/lib/tls/i686/sse2/libevent-1.3d.so.1   
21404: trying file=/lib/tls/i686/libevent-1.3d.so.1   
21404: trying file=/lib/tls/sse2/libevent-1.3d.so.1   
21404: trying file=/lib/tls/libevent-1.3d.so.1   
21404: trying file=/lib/i686/sse2/libevent-1.3d.so.1   
21404: trying file=/lib/i686/libevent-1.3d.so.1   
21404: trying file=/lib/sse2/libevent-1.3d.so.1   
21404: trying file=/lib/libevent-1.3d.so.1   
21404: trying file=/usr/lib/tls/i686/sse2/libevent-1.3d.so.1   
21404: trying file=/usr/lib/tls/i686/libevent-1.3d.so.1   
21404: trying file=/usr/lib/tls/sse2/libevent-1.3d.so.1   
21404: trying file=/usr/lib/tls/libevent-1.3d.so.1   
21404: trying file=/usr/lib/i686/sse2/libevent-1.3d.so.1   
21404: trying file=/usr/lib/i686/libevent-1.3d.so.1   
21404: trying file=/usr/lib/sse2/libevent-1.3d.so.1   
21404: trying file=/usr/lib/libevent-1.3d.so.1   
21404:    LD_DEBUG=libs /usr/bin/memcached/bin/memcached -v 21404: find library=libpthread.so.0 [0]; searching 21404: search cache=/etc/ld.so.cache 21404: trying file=/lib/tls/libpthread.so.0 21404: 21404: find library=libevent-1.3d.so.1 [0]; searching 21404: search cache=/etc/ld.so.cache 21404: search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib (system search path) 21404: trying file=/lib/tls/i686/sse2/libevent-1.3d.so.1 21404: trying file=/lib/tls/i686/libevent-1.3d.so.1 21404: trying file=/lib/tls/sse2/libevent-1.3d.so.1 21404: trying file=/lib/tls/libevent-1.3d.so.1 21404: trying file=/lib/i686/sse2/libevent-1.3d.so.1 21404: trying file=/lib/i686/libevent-1.3d.so.1 21404: trying file=/lib/sse2/libevent-1.3d.so.1 21404: trying file=/lib/libevent-1.3d.so.1 21404: trying file=/usr/lib/tls/i686/sse2/libevent-1.3d.so.1 21404: trying file=/usr/lib/tls/i686/libevent-1.3d.so.1 21404: trying file=/usr/lib/tls/sse2/libevent-1.3d.so.1 21404: trying file=/usr/lib/tls/libevent-1.3d.so.1 21404: trying file=/usr/lib/i686/sse2/libevent-1.3d.so.1 21404: trying file=/usr/lib/i686/libevent-1.3d.so.1 21404: trying file=/usr/lib/sse2/libevent-1.3d.so.1 21404: trying file=/usr/lib/libevent-1.3d.so.1 21404:
然后给libevent-1.3d.so.1建立一个链接:
ln -s /usr/lib/libevent-1.3d.so.1 /lib/libevent-1.3d.so.1    ln -s /usr/lib/libevent-1.3d.so.1 /lib/libevent-1.3d.so.1
再启动,OK。
然后安装php的pecl库的memcache
下载 memcache-2.1.2.tgz   
http://pecl.php.net/get/memcache-2.1.2.tgz
或者用php自带的pecl安装程序,假设php安装目录: /usr/local/php
cd /usr/local/php/bin   
./pecl install memcache    cd /usr/local/php/bin ./pecl install memcache
我安装的时候报错,好像是autoconf没有安装,我是centos系统,直接:
yum install autoconf    yum install autoconf
安装完以后在php.ini里添加:
view plain
copy to clipboard
print
?
extension="memcache.so"   extension="memcache.so"
运行
/usr/local/php/bin/php -l     /usr/local/php/bin/php -l
检查模块是否加载,如果提示找不到模块,修改php.ini的 extension_dir = memcache.so所在目录.
如果以上都没问题,重启apache,编写phpinfo.php文件,内容:

phpinfo();   
?>     
如果出现memcache就表示memcache模块安装成功.
小例子:
view plain
copy to clipboard
print
?

$mc = memcache_connect('localhost', 11211);   
$key = 'mykey';   
$val = $mc->get($key);   
if($val) {   
echo $val;   
}   
$mc->add($key, '你想干嘛');   
$mc->set($key, $val.'你想干嘛 ');   
?>    get($key); if($val) {  echo $val; } $mc->add($key, '你想干嘛'); $mc->set($key, $val.'你想干嘛 '); ?>
一直刷新这个页面看看什么效果.


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/50764/showart_405800.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP