Centos下安装Memcache服务器端

分类:CentOS教程 阅读:78328 次

Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。简单的说就是将数据调用到内存中,然后从内存中读取,从而大大提高读取速度。

服务器端主要是安装memcache服务器端,目前的最新版本是memcached-1.3.0 。
下载:http://www.danga.com/memcached/dist/memcached-1.2.5.tar.gz
另外,Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent,libevent的最新版本是libevent-1.3。

用wget指令直接下载这两个东西.下载回源文件后。
1.先安装libevent。这个东西在配置时需要指定一个安装路径,即./configure –prefix=/usr;然后make;然后make install;
2.再安装memcached,只是需要在配置时需要指定libevent的安装路径即./configure –with-libevent=/usr;然后make;然后make install;
这样就完成了Linux下Memcache服务器端的安装。详细的方法如下:


1.分别把memcached和libevent下载回来,放到 /tmp 目录下:
# cd /tmp
# wgethttp://www.monkey.org/~provos/libevent-1.3.tar.gz

# wgethttp://www.danga.com/memcached/dist/memcached-1.2.5.tar.gz

2.先安装libevent:
# tar zxvf libevent-1.3.tar.gz
# cd libevent-1.3
# ./configure -prefix=/usr
# make
# make install

3.测试libevent是否安装成功:
# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 22 Jan 24 01:38 libevent-1.1a.so.1 ->libevent-1.1a.so.1.0.2

-rwxr-xr-x 1 rootroot 31596 Jan 7 2007libevent-1.1a.so.1.0.2

lrwxrwxrwx 1 rootroot 21 Jan 24 2013 libevent-1.3.so.1 ->libevent-1.3.so.1.0.3

-rwxr-xr-x 1 rootroot 260076 Jan 24 2013 libevent-1.3.so.1.0.3

-rw-r--r-- 1 rootroot 331144 Jan 24 2013 libevent.a

-rwxr-xr-x 1 rootroot 805 Jan 24 2013 libevent.la

lrwxrwxrwx 1 rootroot 21 Jan 24 2013 libevent.so -> libevent-1.3.so.1.0.3


还不错,都安装上了。

4.安装memcached,同时需要安装中指定libevent的安装位置:
# cd /tmp
# tar zxvf memcached-1.2.5.tar.gz
# cd memcached-1.2.5
# ./configure -with-libevent=/usr
# make
# make install
如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。
安装完成后会把memcached放到/usr/local/bin/memcached ,

5.测试是否成功安装memcached:
# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 124281 Jan 242013 /usr/local/bin/memcached

-rwxr-xr-x 1 root root 132762 Jan 24 2013 /usr/local/bin/memcached-debug

安装Memcache的PHP扩展
1.在http://pecl.php.net/package/memcache选择相应想要下载的memcache版本。
2.安装PHP的memcache扩展

tar vxzf memcache-2.2.1.tgz
cd memcache-2.2.1
/usr/bin/phpize
./configure -enable-memcache -with-php-config=/usr/bin/php-config-with-zlib-dir

make
make install

3.上述安装完后会有类似这样的提示:

Installing shared extensions: /usr/lib/php/modules/

4.把php.ini中的extension_dir = “./”修改为

extension_dir = "/usr/lib/php/modules"

5.添加一行来载入memcache扩展:extension=memcache.so

memcached的基本设置:

1.启动Memcache的服务器端:
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 -p 11211 -c 256 -P/tmp/memcached.pid

-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,

2.如果要结束Memcache进程,执行:

# kill `cat /tmp/memcached.pid`

也可以启动多个守护进程,不过端口不能重复。

3.重启apache,servicehttpd restart

Memcache环境测试:
运行下面的php文件,如果有输出This is a test!,就表示环境搭建成功。开始领略Memcache的魅力把!
< ?php
$mem = new Memcache;
$mem->connect(”127.0.0.1″, 11211);
$mem->set(’key’, ‘This is a test!’, 0, 60);
$val = $mem->get(’key’);
echo $val;
?>

-第二种安装方法----------------------------

用yum来装

在CentOS5下为PHP安装memcache支持

--------------------------------------------------------------------------------
装了CentOS5之后,由于有了yum这个非常有用的包管理器,我们基本不用亲自手动一步步去configure,make,make install了。
只要简单的运行yum命令就可以搞定,如果你还是需要自己手动编译,请参阅文末所附参考文档。

1、检查安装第三方软件仓库
标准的CentOS5软件仓库里面是没有memcache相应的包的,不信你自己运行
yum search memcache

看看结果,是不是提示如下?

Loading “installonlyn” plugin

Setting up Install Process

Setting up repositories

Reading repository metadata in from local files

Parsing package install arguments

Nothing to do

所以,我们的第一步就是导入第三方软件仓库,这里推荐的是 Dag Wieers 库(现在叫 RPMForge 了),安装方法如下:


到安装库的网页http://dag.wieers.com/rpm/packages/rpmforge-release/,找到合适的软件包,事实上很简单,我们是centos5.0,由于是和红帽子企业版5.0同样的源编译出来的,那么要的就是rhel5.0的就可以,备选的就只有以下两个
rpmforge-release-0.3.6-1.el5.rf.i386.rpm Fri 09 Mar 2007 16 kB Red Hat EL 5 -i386

rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm Fri 09 Mar 200716 kB Red Hat EL 5 - x86_64

我的是32位的系统,我就选择上面那个地址下载,如果是64位的,选下面那个,呵呵。
[root@localhost src]# wgethttp://dag.wieers.com/rpm/packages/r...l5.rf.i386.rpm

[root@localhost src]# rpm -ivhrpmforge-release-0.3.6-1.el5.rf.i386.rpm

后面没有其他提示,那差不多好了。我们就可以安装我们的memcache相关的模块了。

2、查找相关软件包

在查找的时候会自动更新我们的版本库索引的,这点从下面可以看出来。我们可以发现有两个包要装,一个是 memcache ,一个是php-pecl-memecache。还有个新发现,python 版本的 memcache 客户端也有了,爽啊。
[root@localhost src]# yum search memcache

Loading “installonlyn” plugin

Setting up repositories

update 100% |=========================| 951 B 00:00

rpmforge 100% |=========================| 1.1 kB 00:00

base 100% |=========================| 1.1 kB 00:00

addons 100% |=========================| 951 B 00:00

extras 100% |=========================| 1.1 kB 00:00

Reading repository metadata in from local files

primary.xml.gz 100% |=========================| 1.1 MB02:07

rpmforge :################################################## 3907/3907

Added 3907 new packages, deleted 0 old in 13.20 seconds

primary.xml.gz 100% |=========================| 37 kB 00:00

extras : ##################################################145/145

Added 17 new packages, deleted 0 old in 0.16 seconds
php-pecl-memcache.x86_64 2.0.4-1.el5.rf rpmforge

Matched from:

php-pecl-memcache

PECL package to use the memcached distributed cachingsystem

Memcached is a caching daemon designed especially fordynamic web applications

to decrease database load by storing objects in memory.This extension allows

you to work with memcached through handy OO and proceduralinterfaces.

http://pecl.php.net/package/memcache

php-pecl-memcache.x86_64 2.1.2-1.el5.rf rpmforge

Matched from:

php-pecl-memcache

PECL package to use the memcached distributed cachingsystem

Memcached is a caching daemon designed especially fordynamic web applications

to decrease database load by storing objects in memory.This extension allows

you to work with memcached through handy OO and proceduralinterfaces.

http://pecl.php.net/package/memcache

python-memcached.noarch 1.31-1.el5.rf rpmforge

Matched from:

python-memcached

Python interface to the memcached memory cache daemon

Python interface to the memcached memory cache daemon.

ftp://ftp.tummy.com/pub/python-memcached/

memcached.x86_64 1.1.13-4.el5.rf rpmforge

Matched from:

memcached

memcached is a high-performance, distributed memory objectcaching system,

generic in nature, but intended for use in speeding updynamic web

applications by alleviating database load.

http://www.danga.com/memcached/

memcached.x86_64 1.2.1-3.el5.rf rpmforge

Matched from:

memcached

memcached is a high-performance, distributed memory objectcaching system,

generic in nature, but intended for use in speeding updynamic web

applications by alleviating database load.

http://www.danga.com/memcached/

memcached.x86_64 1.1.13-5.el5.rf rpmforge

Matched from:

memcached

memcached is a high-performance, distributed memory objectcaching system,

generic in nature, but intended for use in speeding updynamic web

applications by alleviating database load.

http://www.danga.com/memcached/

memcached.x86_64 1.2.1-4.el5.rf rpmforge

Matched from:

memcached

memcached is a high-performance, distributed memory objectcaching system,

generic in nature, but intended for use in speeding updynamic web

applications by alleviating database load.

http://www.danga.com/memcached/

memcached.x86_64 1.2.2-1.el5.rf rpmforge

Matched from:

memcached

memcached is a high-performance, distributed memory objectcaching system,

generic in nature, but intended for use in speeding updynamic web

applications by alleviating database load.

http://www.danga.com/memcached/

python-openid.noarch 1.2.0-1.el5.rf rpmforge

Matched from:

The OpenID library with batteries included.
The library features:
refined and easy-to-use API,

extensive documentation,

many storage implemetations including file-based, SQL, andmemcached,

simple examples to help you get started and

licensed under the LGPL.


3、安装并验证
[root@localhost src]# yum install –enablerepo=rpmforge memcachedphp-pecl-memcache

[root@localhost src]# memcached -h

memcached 1.2.2

[root@localhost src]# php -m|grep memcache

memcache

当然你可以将所有的代码写成一个shell脚本,自动化安装,我这里就不写了,熟悉shell的人都会写,哈哈~