如何在 Linux 下安装 Memcached

源码包准备:

1,http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz

这是最新版的 memcached ,直接下载即可;

2,libevent-2.0.4-alpha.tar.gz

Memcached 用到了 libevent 这个库用于 Socket 的处理,所以还需要安装 libevent,libevent的最新版本是 libevent-2.0.4;

其实还有一个需要安装的是 PHP 的 PECL 扩展(php_memcache.so),在下面会看到我们使用 apt-get 来得到。

-->先来安装libevent:

# tar zxvf libevent-1.2.tar.gz
# cd libevent-1.2
# ./configure –prefix=/usr
# make
# make install

-->使用下列命令来测试 libevent 是否安装成功: 

# cd /usr/lib/
# ls -al libevent*

看看有没有显示:

lrwxrwxrwx 1 root root 21 2010-01-13 08:15 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3
-rw-r--r-- 1 root root 99940 2009-05-18 17:30 libevent-1.4.so.2.1.3
-rw-r--r-- 1 root root 915142 2010-04-25 14:29 libevent.a
-rw-r--r-- 1 root root 574012 2010-04-25 14:29 libevent_core.a
-rwxr-xr-x 1 root root 961 2010-04-25 14:29 libevent_core.la
lrwxrwxrwx
1 root root 22 2010-04-25 14:29 libevent_core.so -> libevent_core.so.3.0.0
lrwxrwxrwx
1 root root 22 2010-04-25 14:29 libevent_core.so.3 -> libevent_core.so.3.0.0
-rwxr-xr-x 1 root root 426876 2010-04-25 14:29 libevent_core.so.3.0.0
-rw-r--r-- 1 root root 341202 2010-04-25 14:29 libevent_extra.a
-rwxr-xr-x 1 root root 968 2010-04-25 14:29 libevent_extra.la
lrwxrwxrwx
1 root root 23 2010-04-25 14:29 libevent_extra.so -> libevent_extra.so.3.0.0
lrwxrwxrwx
1 root root 23 2010-04-25 14:29 libevent_extra.so.3 -> libevent_extra.so.3.0.0
-rwxr-xr-x 1 root root 289438 2010-04-25 14:29 libevent_extra.so.3.0.0
-rwxr-xr-x 1 root root 926 2010-04-25 14:29 libevent.la
-rw-r--r-- 1 root root 7030 2010-04-25 14:29 libevent_pthreads.a
-rwxr-xr-x 1 root root 989 2010-04-25 14:29 libevent_pthreads.la
lrwxrwxrwx
1 root root 26 2010-04-25 14:29 libevent_pthreads.so -> libevent_pthreads.so.0.0.0
lrwxrwxrwx
1 root root 26 2010-04-25 14:29 libevent_pthreads.so.0 -> libevent_pthreads.so.0.0.0
-rwxr-xr-x 1 root root 11186 2010-04-25 14:29 libevent_pthreads.so.0.0.0
lrwxrwxrwx
1 root root 17 2010-04-25 14:29 libevent.so -> libevent.so.3.0.0
lrwxrwxrwx
1 root root 17 2010-04-25 14:29 libevent.so.3 -> libevent.so.3.0.0
-rwxr-xr-x 1 root root 698792 2010-04-25 14:29 libevent.so.3.0.0

-->安装 memcached,同时需要安装中指定 libevent 的安装位置:

# cd /tmp
# tar zxvf memcached
-1.2.0.tar.gz
# cd memcached
-1.2.0
# .
/configure –with-libevent=/usr
# make
# make install

安装完成后会把 memcached 放到 /usr/local/bin/memcached

-->测试是否成功安装 memcached:

# ls -al /usr/local/bin/mem*

看看有没有显示:

-rwxr-xr-x 1 root root 222103 2010-04-25 14:32 /usr/local/bin/memcached

如果一切顺利的话,我们继续往下走:

-->安装 PHP 的 PECL 扩展(php_memcache.so)一句命令:

 

apt-get install libcache-memcached-perl

 

跟着命令走吧~~呵呵~

一切就绪以后重启阿帕奇(Apache)或者(Nginx)服务器

使用 phpinfo(); 命令查看 php 配置信息

会有一项:

         memcache
memcache support              enabled
Version                      
3.0.1
Revision                     $Revision:
1.83.2.24 $

Directive               
      Local Value                Master Value
memcache.allow_failover          
1                           1
memcache.chunk_size             
32768                       32768
memcache.default_port          
11211                       11211
memcache.hash_function           crc32                       crc32
memcache.hash_strategy           consistent                  consistent
memcache.max_failover_attempts  
20                          20
memcache.protocol               ascii                       ascii
memcache.redundancy            
1                           1
memcache.session_redundancy    
2                           2
说明我们的扩展以成功安装并运行,下面开始测试吧!

新建 example.php 文件:

1
2 <?php
3
4 $memcache = new Memcache;
5 $memcache->connect('localhost', 11211) or die ("Could not connect");
6
7 $version = $memcache->getVersion();
8 echo "Server's version: ".$version."<br/>\n";
9
10 $tmp_object = new stdClass;
11 $tmp_object->str_attr = 'test';
12 $tmp_object->int_attr = 123;
13
14 $memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
15 echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
16
17 $get_result = $memcache->get('key');
18 echo "Data from the cache:<br/>\n";
19
20 var_dump($get_result);
21
22 ?>
看看输出结果是不是:

Server's version: 1.4.5
Store data in the cache (data will expire in 10 seconds)
Data from the cache:
object(stdClass)#3 (2) { ["str_attr"]=> string(4) "test" ["int_attr"]=> int(123) }
如果是的话,那就欢呼吧~~~成功啦!

关于 Memcache 的配置及其使用说明,请参考 PHP 手册的 Memcache Functions 这一章,介绍的很详细!

posted @ 2010-04-25 17:42  无墨来点睛  Views(669)  Comments(0Edit  收藏  举报