1.分别把memcached和libevent下载下来
2.先安装libevent:
# tar zxvf libevent-1.2.tar.gz
# cd libevent-1.2
# ./configure
# make
# make install
默认安装在/usr/local/lib/libevent
3.把新共享库目录加入到共享库配置文件
# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf
# echo "/usr/local/lib" >> /etc/ld.so.conf
# ldconfig
3.安装memcached,同时需要安装中指定libevent的安装位置:
# tar zxvf memcached-1.2.0.tar.gz
# cd memcached-1.2.0
#./configure --with-libevent=/usr/local/lib/libevent
# make
# make install
装完成后会把memcached放到 /usr/local/bin/memcached
4.测试是否成功安装memcached:
# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 137986 11?? 12 17:39 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 140179 11?? 12 17:39 /usr/local/bin/memcached-debug
5.启动
/usr/local/bin/memcached -d -m 200 -u root -l 10.60.1.26 -p 11211 -c 1000 -P /tmp/memcached.pid
也可以启动多个守护进程,但是端口不能重复
解释如下:
-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,这里是200MB
-u是运行Memcache的用户,如果当前为 root 的话,需要使用此参数指定用户。
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.1.91
-p是设置Memcache监听的端口,我这里设置了12301,最好是1024以上的端口
-c选项是最大运行的并发连接数,默认是1024,这里设置了256
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid
6.停止
查看memcache的进程号:ps -ef | grep memcached
停止Memcache进程 kill -9 进程号
查看启动的memcache服务: netstat -lp | grep memcached
测试程序:
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("10.60.1.26:11211"));
MemcachedClient memcachedClient=builder.build();
memcachedClient.set( "hello", 0, "Hello,xmemcached1");
String value = memcachedClient.get("hello");
System. out.println( "hello=" + value);
memcachedClient.delete("hello");
value = memcachedClient.get("hello");
System. out.println( "hello=" + value);
// close memcached client
memcachedClient.shutdown();