服务器端主要是安装memcache服务器端
Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent。
下载地址:~provos/libevent
memcache下载地址:
详细的方法如下:
1,先安装libevent:
# tar zxvf libevent-1.4.6-stable.tar.gz
# ./configure --prefix=/usr
# make
# make install
2,测试libevent是否安装成功:
# ls -al /usr/lib | grep libevent
[root@localhost soft]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 21 Dec 30 11:00 libevent-1.4.so.2 -> libevent-1.4.so.2.1.1
-rwxr-xr-x 1 root root 280179 Dec 30 11:00 libevent-1.4.so.2.1.1
-rw-r--r-- 1 root root 366946 Dec 30 11:00 libevent.a
lrwxrwxrwx 1 root root 26 Dec 30 11:00 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.1
-rwxr-xr-x 1 root root 107523 Dec 30 11:00 libevent_core-1.4.so.2.1.1
-rw-r--r-- 1 root root 146944 Dec 30 11:00 libevent_core.a
-rwxr-xr-x 1 root root 860 Dec 30 11:00 libevent_core.la
lrwxrwxrwx 1 root root 26 Dec 30 11:00 libevent_core.so -> libevent_core-1.4.so.2.1.1
lrwxrwxrwx 1 root root 27 Dec 30 11:00 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.1.1
-rwxr-xr-x 1 root root 219483 Dec 30 11:00 libevent_extra-1.4.so.2.1.1
-rw-r--r-- 1 root root 281326 Dec 30 11:00 libevent_extra.a
-rwxr-xr-x 1 root root 867 Dec 30 11:00 libevent_extra.la
lrwxrwxrwx 1 root root 27 Dec 30 11:00 libevent_extra.so -> libevent_extra-1.4.so.2.1.1
-rwxr-xr-x 1 root root 825 Dec 30 11:00 libevent.la
lrwxrwxrwx 1 root root 21 Dec 30 11:00 libevent.so -> libevent-1.4.so.2.1.1
3,安装memcached,同时需要安装中指定libevent的安装位置:
# tar zxvf memcached-1.2.6.tar.gz
# ./configure --with-libevent=/usr
# make
# make install
安装完成后会把memcached放到 /usr/local/bin/memcached
4,测试是否成功安装memcached:
# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 121261 Dec 30 11:02 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 130191 Dec 30 11:02 /usr/local/bin/memcached-debug
5,安装Memcache的PHP扩展
在 选择相应想要下载的memcache版本。
# tar vxzf memcache-2.2.4
# cd memcache-2.2.4
# /usr/local/php/bin/phpize
# ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
# make
# make install
6,上述安装完后会有类似这样的提示:
Installing shared extensions: "/usr/local/php/lib/php/extensions/no-debug-non-zts-2006xxxx/"
7,把php.ini中的extension_dir = “./”修改为
extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/”
8,添加一行来载入memcache扩展:
extension=memcache.so
9,启动和停止Memcache的服务器端:
# /usr/local/bin/memcached -d -m 200 -u root -l 192.168.1.91 -p 12000 -c 1000 -P /tmp/memcached.pid
相关解释如下:
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,这里是200MB
-u是运行Memcache的用户,我这里是root
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.1.91
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid
停止Memcache进程:
# kill `cat /tmp/memcached.pid`
也可以启动多个守护进程,但是端口不能重复
10,重启apache
service httpd restart
11,Memcache环境测试:
运行下面的php文件,如果有输出This is a test!,就表示环境搭建成功。
< ?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;
?>
php的测试代码:
ini_set("memcache.hash_function","crc32");
$memcache = new Memcache;
$memcache->addServer('192.168.1.91', 12000);
$memcache->flush();
for($i=0;$i<120000;$i++){
$memcache->set($i,
"hi memcache, i want to use memcache, you good", 0, 1000);
}
?>
perl的测试代码:
#!/usr/bin/perl
use Cache::Memcached::Fast;
my $memd = new Cache::Memcached::Fast({
servers => [ { address => '192.168.1.91:12000', weight => 2.5 } ],
});
$memd->flush_all;
for($i=0;$i<120000;$i++){
$memd->set($i, "hi memcache, i want to use memcache, you good");
}
$memd = undef;
这里的perl采用了Cache::Memcached::Fast模块,没有采用Cache::Memcached,因为Cache::Memcached::Fast模块是用C写的,所以很多人测试的perl模块没有php效率高,原因就在这
Linux下Memcache服务器的安装及测试
最新推荐文章于 2024-07-02 00:15:00 发布