分类: LINUX
2010-09-01 10:17:09
Linux下缓存服务器-memcached
环境:
libevent库
php支持
我此次安装的memcache是在lamp环境下:RedHat 5.4-bit64 ;Apache、MySQL、PHP的安装路径都在/usr/local下;源代码包都放在/usr/local/src下
相关软件:
memcached:或 .
memcache:或
libevent:
安装libevent:
cd /usr/local/src/
tar zxvf libevent-1.3d.tar.gz
cd libevent-1.3d
./configure
make && make install
ln -s /usr/local/lib/libevent-1.3d.so.1 /usr/lib64/libevent-1.3d.so.1
如果是32位系统的话则改为:
ln -s /usr/local/lib/libevent-1.3d.so.1 /usr/lib/libevent-1.3d.so.1
安装Memcached
cd /usr/local/src
tar vzxf memcached-
cd memcached-1.4.5
./configure --prefix=/usr/local/memcached
make && make install
启动服务
cd /usr/local/memcached/bin/
./memcached -d -m 1024 -p 11211 -u root
参数说明 -m 指定缓存空间大小单位兆 ;-p指定监听端口 一般为11211 ; -u指定用哪个用户来运行 一般是root
安装memcache PHP模块
cd /usr/local/src/
tar zxvf memcache-
cd memcache-2.2.5
/usr/local/php/bin/phpize
(这步可能会提示错误
[root@localhost memcache-
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF
environment variable is set correctly and then rerun this script.
如果出现这种情况yum install autoconf就可以了
这样会装上两个包:imake autoconf)
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
make && make install
安装完成后会有类似的提示:
extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-XXXXXXXX(X代表数字)
然后修改/usr/local/php/lib/下的php.ini
找到extension_dir 这一行
如果是extension_dir = "./"
则修改为
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-XXXXXXXX/"
如果是其他路径则添加一行
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-XXXXXXXX/"
或者把"/usr/local/php/lib/php/extensions/no-debug-non-zts-XXXXXXXX/"下的文件拷到原来制定的目录下也可以
因为我原来添加过Apache的扩展模块,我的路径是extension_dir = "/usr/local/apache/modules/"
所以我又添加了一行新的。
最后一步在/usr/local/php/lib/(这个路径要根据实际情况,这个文件是在编译PHP时自己设定的)下的php.ini添加一行extension=memcache.so
(本人试验了好几次,有时候extension_dir = "XXXXXXXXXXXXXX"和extension=memcache.so会自动添加,有时候需要手动大家改之前最好先看下是否已被自动添加)
测试脚本
安装完成了写个PHP脚本测试一下吧:
$memcache = new Memcache; //创建一个memcache对象
$memcache->connect('localhost', 11211) or die ("Could not connect"); //连接Memcached服务器;localhost可以换成ip ,11211是启动时指定的端口
$memcache->set('key', 'This is a test!'); //设置一个变量到内存中,名称是key 值是This is a test!
$get_value = $memcache->get('key'); //从内存中取出key的值
echo $get_value;
?>
如果输出This is a test!表示环境搭建成功。
启动参数详解
[root@localhost ~]#/usr/local/memcached/bin/memcached -h
memcached
-p
-U
-s
-a
-l
-d run as a daemon #以后台守护进程方式运行
-r maximize core file limit
-u
-m
-M return error on memory exhausted (rather than removing items)
-c
-k lock down all paged memory. Note that there is a
limit on how much memory you may lock. Trying to
allocate more than that would fail, so be sure you
set the limit correctly for the user you started
the daemon with (not for -u
under sh this is done with 'ulimit -S -l NUM_KB').
-v verbose (print errors/warnings while in event loop)#显示详细的运行状态,在前台启动
-vv very verbose (also print client commands/reponses) #显示更详细的运行状态。
-vvv extremely verbose (also print internal state transitions)
-h print this help and exit #打印此帮助信息
-i print memcached and libevent license
-P
-f
-n
-L Try to use large memory pages (if available). Increasing
the memory page size could reduce the number of TLB misses
and improve the performance. In order to get large pages
from the OS, memcached will allocate the total item-cache
in one large chunk.
-D
This is used for per-prefix stats reporting. The default is
":" (colon). If this option is specified, stats collection
is turned on automatically; if not, then it may be turned on
by sending the "stats detail on" command to the server.
-t
-R Maximum number of requests per event, limits the number of
requests process for a given connection to prevent
starvation (default: 20)
-C Disable use of CAS
-b Set the backlog queue limit (default: 1024)
-B Binding protocol - one of ascii, binary, or auto (default)
-I Override the size of each slab page. Adjusts max item size
(default: 1mb, min: 1k, max: