magent+memcached

本文转自:http://hi.baidu.com/ghhryr/item/6bc810a0a02c65a228ce9d4c

作者:867358162

本人在本着共享的原则在文中略作修改.

一.软件下载

wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz


wget http://memagent.googlecode.com/files/magent-0.6.tar.gz


wget http://monkey.org/~provos/libevent-1.4.9-stable.tar.gz




二。安装libevent-1.4.9-stable.tar.gz

tar zxvf libevent-2.0.13-stable.tar.gz

cd libevent-2.0.13-stable

./configure --with-libevent=/usr

make

make install







三。安装memcacd

tar zxvf memcached-1.4.13.tar.gz

cd memcached-1.4.13

./configure --with-libevent=/usr

make

make install



四。安装magent

tar zxvf magent-0.6.tar.gz

make




报错1:

gcc -Wall -g -O2 -I/usr/local/include -m64 -c -o magent.o magent.c

magent.c: In function 'writev_list':

magent.c:729: error: 'SSIZE_MAX' undeclared (first use in this function)

magent.c:729: error: (Each undeclared identifier is reported only once

magent.c:729: error: for each function it appears in.)

make: *** [magent.o] Error 1


解决办法:

[root@centos6 memcached]# vi ketama.h

#在开头加入

#ifndef SSIZE_MAX

#define SSIZE_MAX      32767

#endif




继续make


报错2:

gcc -Wall -g -O2 -I/usr/local/include -m64 -c -o magent.o magent.c

gcc -Wall -g -O2 -I/usr/local/include -m64 -c -o ketama.o ketama.c

gcc -Wall -g -O2 -I/usr/local/include -m64 -o magent magent.o ketama.o /usr/lib64/libevent.a /usr/lib64/libm.a

gcc: /usr/lib64/libevent.a: No such file or directory

gcc: /usr/lib64/libm.a: No such file or directory



解决办法:

[root@centos6 memcached]# ln -s /usr/lib/libevent*  /usr/lib64/

[root@centos6 memcached]# make


报错3:

gcc -Wall -g -O2 -I/usr/local/include -m64 -o magent magent.o ketama.o /usr/lib64/libevent.a /usr/lib64/libm.a

gcc: /usr/lib64/libm.a: No such file or directory

make: *** [magent] Error 1



解决办法:

yum install glibc glibc-devel

如果是64bit的系统则不会在/usr/lib64/libm.a 生成,如果是32bit即会有。


[root@centos6 memcached]# cp /usr/lib64/libm.so /usr/lib64/libm.a



继续make

如果继续上面报错先查看/usr/lib/libm.so是否存在,如不存在可用/lib64/libm.so.6

文件做软连接为/usr/lib/libm.so重复上面步骤解决此问题。


报错4:

gcc -Wall -g -O2 -I/usr/local/include -m64 -o magent magent.o ketama.o /usr/lib64/libevent.a /usr/lib64/libm.a

/usr/lib64/libevent.a(event.o): In function `detect_monotonic':

event.c:(.text+0xc79): undefined reference to `clock_gettime'

/usr/lib64/libevent.a(event.o): In function `gettime':

event.c:(.text+0xd60): undefined reference to `clock_gettime'

collect2: ld returned 1 exit status

make: *** [magent] Error 1



解决办法:

[root@centos6 memcached]# vi Makefile

CFLAGS = -Wall -g -O2 -I/usr/local/include $(M64)

改为:    

CFLAGS = -lrt -Wall -g -O2 -I/usr/local/include $(M64)




[root@centos6 memcached]# make

gcc -Wall -g -O2 -I/usr/local/include -m64 -o magent magent.o ketama.o -lrt /usr/lib64/libevent.a /usr/lib64/libm.a

[root@centos6 memcached]# ls                                                                                             成功

Makefile  ketama.c  ketama.h  ketama.o  magent  magent-0.6.tar.gz  magent.c  magent.o

[root@centos6 memcached]# cp magent /usr/bin/




五。启动进程

memcached启动参数描述:


-d :启动一个守护进程,


-m:分配给Memcache使用的内存数量,单位是MB,默认是64MB,


-u :运行Memcache的用户


-l  :监听的服务器IP地址


-p :设置Memcache监听的端口,默认是11211    注:-p(p为小写)


-c :设置最大并发连接数,默认是1024


-P :设置保存Memcache的pid文件   注:-P(P为大写)


如果要结束Memcache进程,执行:kill cat pid文件路径




[root@centos6 memcached]#  memcached -m 10 -u root -d -l 0.0.0.0 -p 11211

[root@centos6 memcached]#  memcached -m 10 -u root -d -l 0.0.0.0 -p 11212

[root@centos6 memcached]#  memcached -m 10 -u root -d -l 0.0.0.0 -p 11213

[root@centos6 memcached]# netstat -antp

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  

tcp        0      0 127.0.0.1:11211             0.0.0.0:*                   LISTEN      12296/memcached    

tcp        0      0 0.0.0.0:11212               0.0.0.0:*                   LISTEN      12412/memcached    

tcp        0      0 0.0.0.0:11213               0.0.0.0:*                   LISTEN      12419/memcachcd



magent启动


[root@centos6 memcached]# magent -u root -n 51200 -l 192.168.20.122 -p 12000 -s 127.0.0.1:11211 -s 127.0.0.1:11212 -b 127.0.0.1:11213


分别在127.0.0.1机器的11211,11212,11213端口启动3个Memcached进程,在12000端口开启magent代理程序;(以此类推可在不同机器上启动memcached)


11211和11212为主Memcached,11213为备份Memcached;




[root@centos6 memcached]# telnet 192.168.20.159 12000

Trying 192.168.20.159...

Connected to 192.168.20.159.

Escape character is '^]'.

stats

memcached agent v0.6

matrix 1 -> 127.0.0.1:11211, pool size 0

matrix 2 -> 127.0.0.1:11212, pool size 0

matrix 3 -> 127.0.0.1:11213, pool size 0

END





六。安装php的memcache客户端

下载


wget http://pecl.php.net/get/memcache-2.2.6.tgz




安装


tar zxvf memcache-2.2.6.tgz -C /usr/src/

cd /usr/src/memcache-2.2.6/

ls

/usr/local/php/bin/phpize

./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config

make && make install


Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/   出现该行安装成功



修改php.ini


[root@LNMP4 memcache-2.2.6]# vi /usr/local/php/etc/php.ini


extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/"                  该行修改

extension = memcache.so                                                                          该行添