一.软件下载
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
root@localhost ~]# cd /usr/tools/magent/
[root@localhost magent]# ls
ketama.c ketama.h magent-0.5.tar.gz magent.c Makefile
[root@localhost magent]# /sbin/ldconfig
[root@localhost magent]# sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
[root@localhost magent]# 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
报错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/
/usr/local/bin/memcached -d -m 4024 -u root -p 11212 -c 4024 -P /tmp/memcached2.pid
/usr/local/bin/memcached -d -m 4024 -u root -p 11213 -c 4024 -P /tmp/memcached3.pid
/usr/local/bin/memcached -d -m 4024 -u root -p 11214 -c 4024 -P /tmp/memcached4.pid
magent -u root -n 51200 -l 127.0.0.1 -p 11211 -s 127.0.0.1:11212 -s 127.0.0.1:11213 -b 127.0.0.1:11214
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是1024MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid
如果要结束Memcache进程,执行:
# kill `cat /tmp/memcached.pid`
测试Memcached:
[root@localhost /]# telnet 192.168.141.64 12000
Trying 192.168.141.64...
Connected to 192.168.141.64 (192.168.141.64).
Escape character is '^]'.
set key1 0 60 4
zhou
STORED
get key1
VALUE key1 0 4
zhou
END