Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent.
1.先安装libevent。这个东西在配置时需要指定一个安装路径,即./configure –prefix=/usr/local;然后make;然后make install;
2.再安装memcached,只是需要在配置时需要指定libevent的安装路径即./configure –with-libevent=/usr/local;然后make;然后make install;
这样就完成了Linux下Memcache服务器端的安装。详细的方法如下:
# cd /usr/local/src/tarbag
# wget http://www.danga.com/memcached/dist/memcached-1.4.0.tar.gz
# wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz
# tar zxvf libevent-1.4.13-stable.tar.gz -C ../software
# cd ../software/libevent-1.4.13-stable
# ./configure –prefix=/usr/local
# make
# make install
3.测试libevent是否安装成功:
# ls -al /usr/local/lib | grep libevent
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent-1.2.so.1 -> libevent-1.2.so.1.0.3
-rwxr-xr-x 1 root root 263546 11?? 12 17:38 libevent-1.2.so.1.0.3
-rw-r–r– 1 root root 454156 11?? 12 17:38 libevent.a
-rwxr-xr-x 1 root root 811 11?? 12 17:38 libevent.la
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent.so -> libevent-1.2.so.1.0.3
还行,都安装上了。
# cd /usr/local/src/tarbag
# tar zxvf memcached-1.4.0.tar.gz -C ../software
# cd memcached-1.4.0
# ./configure –with-libevent=/usr/local
# make
# make install
如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。
安装完成后会把memcached放到 /usr/local/bin/memcached ,
# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 137986 11?? 12 17:39 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 140179 11?? 12 17:39 /usr/local/bin/memcached-debug
1.在http://pecl.php.net/package/memcache 选择相应想要下载的memcache版本。
2.安装PHP的memcache扩展
cd ../software/memcache-2.2.5
/usr/local/php/bin/phpize
./configure -enable-memcache -with-php-config=/usr/local/php/bin/php-config -with-zlib-dir
make
make install
1.启动Memcache的服务器端:
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.50.240 -p 12000 -c 256 -P /tmp/memcached.pid
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.50.240
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,
运行下面的php文件,如果有输出This is a test!,就表示环境搭建成功。开始领略Memcache的魅力把!
<?php
启动master
#memcached -v -l 192.168.50.240 -p 11211 -uroot
replication: listen (master监听)
#memcached -v -l 192.168.50.241 -p 11213 -uroot -x 127.0.0.1 -X 11212
replication: connect (peer=192.168.50.240:11212)
replication: marugoto copying
replication: start
操作master
#telnet 192.168.50.240 11211
#set key1 0 0 3
111
#telnet 192.168.50.241 11213
#get key1
可以实现cache冗余