memcached安装

今天 ,心血来潮 想安装下memcached 先给自己测试一下。

首先 从memcached 官网中下载 memcached-1.4.15

首先memcached 安装需要libevent

从官网中下载:

libevent-2.0.21-stable.tar.gz

解压 安装:

1
# tar zxvf <span>libevent-2.0.21-stable.tar.gz</span><span> </span> # cd libevent-1.2
2
# ./configure --prefix=/usr
3
# make
4
# make install
判断memcached是否安装成功

1
# ls -al /usr/lib | grep libevent
结果如下:
memcached安装_第1张图片

.安装memcached,同时需要安装中指定libevent的安装位置

1
# tar zxvf memcached-1.4.15.tar.gz
2
# cd memcached-1.4.15
3
# ./configure --with-libevent=/usr
4
# make
5
# make install
测试是否成功安装memcached:
1
# ls -al /usr/local/bin/mem*
2
-rwxr-xr-x 1 root root 137986 11?? 12 17:39 /usr/local/bin/memcached
3
-rwxr-xr-x 1 root root 140179 11?? 12 17:39 /usr/local/bin/memcached-debug
这个时候 我们需要来启动memcached了。

1
usr/local/bin/memcached -d -m 100 -u root -p 12000 -c 1000 -P /tmp/memcached.pid
我们来分析下参数代表的含义:

1
-d选项是启动一个守护进程,
2
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
3
-u是运行Memcache的用户,我这里是root,
4
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.0.200,
5
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
6
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
7
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,
当我启动进程 调用



memcached 已经启动了 。

好 现在我想用telnet命令来测试一下

可是 当我用telnet时 发现竟然显示

1
-bash: telent: command not found
马上百度之
很多人说是因为没有装 telnet服务 。

所以调用yum命令来安装 telnet

1
um -y install telnet
另外需要配置:
01
[root@localhost memcached-1.4.15]# vi /etc/xinetd.d/telnet
02

03
# default: on
04
# description: The telnet server serves telnet sessions; it uses \
05
#       unencrypted username/password pairs for authentication.
06
service telnet
07
{
08
        flags           = REUSE
09
        socket_type     = stream
10
        wait            = no
11
        user            = root
12
        server          = /usr/sbin/in.telnetd
13
        log_on_failure  += USERID
14
        disable         = no
15
}
这里需要将disable 从 yes 修改成 no
需要启动服务

1
cd /etc/rc.d/init.d/
2
service xinetd restart
现在 我用 xmemcached来测试一下
测试代码如下:

01
package nio;
02

03

04
import net.rubyeye.xmemcached.MemcachedClient;
05
import net.rubyeye.xmemcached.MemcachedClientBuilder;
06
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
07
import net.rubyeye.xmemcached.exception.MemcachedException;
08
import net.rubyeye.xmemcached.utils.AddrUtil;
09

10
import java.io.IOException;
11
import java.util.HashMap;
12
import java.util.concurrent.TimeoutException;
13

14
/**
15
* 测试 cas
16
* User: zhangyong
17
* Date: 12-12-2
18
* Time: 上午10:54
19
* To change this template use File | Settings | File Templates.
20
*/
21
public class SimpleTest {
22

23
    public static void main(String[] args) throws IOException {
24
        MemcachedClientBuilder builder = new XMemcachedClientBuilder(
25
                AddrUtil.getAddresses("192.168.56.1:12001"));
26
        MemcachedClient memcachedClient = builder.build();
27
        try {
28
            memcachedClient.set("hello", 0, "Hello,xmemcached");
29
            String value = memcachedClient.get("hello");
30
            System.out.println("hello=" + value);
31

32
            //尝试添加 看看返回什么
33
            boolean  flag = memcachedClient.add("hello", 0, "Hello,xmemcached");
34
            System.out.println("flag==" + flag);
35

36
            memcachedClient.delete("hello");
37
            value = memcachedClient.get("121709_300");
38
            System.out.println("value=" + value);
39

40
            HashMap map = new HashMap();
41
            map.put("key" ,"123");
42
            memcachedClient.set("map",0,map) ;
43

44
            System.out.println(memcachedClient.get("map").getClass());
45

46
        } catch (MemcachedException e) {
47
            System.err.println("MemcachedClient operation fail");
48
            e.printStackTrace();
49
        } catch (TimeoutException e) {
50
            System.err.println("MemcachedClient operation timeout");
51
            e.printStackTrace();
52
        } catch (InterruptedException e) {
53
            e.printStackTrace();
54
        }
55
//        try {
56
//            //close memcached client
57
//            memcachedClient.shutdown();
58
//        } catch (IOException e) {
59
//            System.err.println("Shutdown MemcachedClient fail");
60
//            e.printStackTrace();
61
//        }
62
    }
63

64
}
结果如下:
[WARN ]2013-03-09 20:14:19416 net.rubyeye.xmemcached.XMemcachedClient XMemcachedClient use Text protocol
[INFO ]2013-03-09 20:14:19465 com.google.code.yanf4j.nio.impl.SelectorManager Creating 8 reactors...
[WARN ]2013-03-09 20:14:19507 com.google.code.yanf4j.core.impl.AbstractController The Controller started at localhost/127.0.0.1:0 ...
[WARN ]2013-03-09 20:14:19521 com.google.code.yanf4j.core.impl.AbstractController Add a session: 192.168.56.1:12001
hello=Hello,xmemcached
flag==false
value=null
class java.util.HashMap
ok 测试工作准备就绪 可以开始 写个类似memcached-client的小东西了 练练手 下两周的计划。


memcached安装_第2张图片

你可能感兴趣的:(jvm,工作,memcached)