Memcached虽然称为“分布式“缓存服务器,但服务器端并没有“分布式”的功能。Memcached的分布式完全是有客户端实现的。即通过环形hash
http://www.danga.com/memcached/dist/memcached-1.2.2.tar.gz 下载 memcached
http://www.monkey.org/~provos/libevent-1.3.tar.gz 下载 libevent
先安装libevent
解压进入目录 ./configure --prefix=/usr
执行make
如果报错如下
make all-recursive
make[1]: Entering directory `/usr/local/libevent-1.3'Making all in .
make[2]: Entering directory `/usr/local/libevent-1.3'
/bin/sh ./libtool --tag=CC --mode=compile gcc -DHAVE_CONFIG_H -I. -I. -I. -Icompat -g -O2 -Wall -c -o event.lo event.c
mkdir .libs
gcc -DHAVE_CONFIG_H -I. -I. -I. -Icompat -g -O2 -Wall -c event.c -fPIC -DPIC -o .libs/event.o
event.c: In function 'gettime':
event.c:156:2: warning: implicit declaration of function 'clock_gettime' [-Wimplicit-function-declaration]
if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
^
event.c:156:20: error: 'CLOCK_REALTIME' undeclared (first use in this function)
if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
^
event.c:156:20: note: each undeclared identifier is reported only once for each function it appears in
make[2]: *** [event.lo] Error 1
make[2]: Leaving directory `/usr/local/libevent-1.3'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/usr/local/libevent-1.3'
make: *** [all] Error 2
vi event.c 加入 #include <time.h>
再次 make make install
ls -al /usr/lib | grep libevent 验证libevent是否成功安装
安装memcached
解压进入目录 ./configure --with-libevent=/usr
然后 make make install
ls -al /usr/local/bin/mem* 验证memcached是否成功安装
执行 /usr/local/bin/memcached -d -m 10 -u root -l 192.168.141.64 -p 12000 -c 256 -P /tmp/memcached.pid
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,
报错:
/usr/local/bin/memcached: error while loading shared libraries: libevent-1.3.so.1: cannot open shared object file: No such file or directory
解决:
为了让动态链接库为系统所共享,还需运行动态链接库的管理命令 ldconfig
or
LD_DEBUG=libs memcached -v
ln -s /usr/lib/libevent-1.3.so.1 /usr/lib64/libevent-1.3.so.1
再次执行 /usr/local/bin/memcached -d -m 10 -u root -l 192.168.141.64 -p 12000 -c 256 -P /tmp/memcached.pid
报错:
bind(): Cannot assign requested address
failed to listen
解决:
ifconfig
inet 172.17.0.1 netmask 255.255.240.0 broadcast 172.17.15.255
IP 子网 广播
再次执行 /usr/local/bin/memcached -d -m 10 -u root -l 172.17.0.1 -p 12000 -c 256 -P /tmp/memcached.pid
ps -aux查看服务
kill `cat /tmp/memcached.pid` 关闭服务
telnet 172.17.0.1 11000 测试连接