noel0217 发表于 2015-11-19 08:40:39

Memcached1.4安装笔记

系统安装需求,CentOS6.5,能连接互联网,下面是安装步骤
1.yum install libevent-devel

2.wget http://memcached.org/latest

3.wget http://www.memcached.org/files/memcached-1.4.20.tar.gz
下载最新的安装文件memcached-1.4.20.tar.gz
4.tar -zxvf memcached-1.4.20.tar.gz
5.cd memcached-1.4.20
6。       ./configure && make && make test && sudo make install

提示报错:
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/opt/memcached-1.4.20':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.

7.yum -y install gcc
系统会下载gcc安装包,自动安装。时间较漫长,大约装30分钟。(只能吐槽国内网速)
8.yum -y install gcc-c++
再下一遍安装包,这次速度快点。

9。再次执行第6步,直到看到成功编译的提示。
......
prove ./t
make: prove: Command not found
make: *** Error 127

10.第9步报错误, 重新执行make install
没有提示错误。


11.启动和停止memcached
# ./memcached -d -m 1024 -u root -l 10.11.1.219 -p 11211 -c 256 -P /tmp/memcached.pid //启动memcached 启动参数说明:

   启动参数说明:
   -d   选项是启动一个守护进程,
   -m是分配给Memcache使用的内存数量,单位是MB,默认64MB

   -Mreturn error on memory exhausted (rather than removing items)
   -u是运行Memcache的用户,如果当前为root 的话,需要使用此参数指定用户。
   -l   是监听的服务器IP地址,默认为所有网卡。
   -p是设置Memcache的TCP监听的端口,最好是1024以上的端口
   -c选项是最大运行的并发连接数,默认是1024
   -P是设置保存Memcache的pid文件

   -f   <factor>   chunk size growth factor (default: 1.25)

   -I   Override the size of each slab page. Adjusts max item size(1.4.2版本新增)

也可以启动多个守护进程,但是端口不能重复


停止Memcache进程:
   kill `cat /tmp/memcached.pid`

12.测试memcached,
使用另外一台机器,telnet 10.11.1.219 11211
如果需要,先关闭linux防火墙。关闭linux防火墙的命令:service iptables stop

连接成功输入命令:
stats

显示正常结果,说明服务成功启动了。
stats
STAT pid 2940
STAT uptime 407
STAT time 1409821965
STAT version 1.4.20
STAT libevent 1.4.13-stable
STAT pointer_size 64
STAT rusage_user 0.000000
STAT rusage_system 0.029995
STAT curr_connections 5
STAT total_connections 6
STAT connection_structures 6
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0

13。java客户端上的开发
java memcached client下载地址https://github.com/gwhalin/Memcached-Java-Client/downloads
将下载的ava_memcached-release_2.6.6.zip解压,jar包全部导入到java项目。

测试程序:
import java.util.ArrayList;

import com.danga.MemCached.*;

public class MyClass {
    // create a static client as most installs only need
    // a single instance
    protected static MemCachedClient mcc = new MemCachedClient();
    // set up connection pool once at class load
    static {
      // server list and weights
//      String[] servers = { &quot;server1.mydomain.com:1624&quot;,
//                &quot;server2.mydomain.com:1624&quot;, &quot;server3.mydomain.com:1624&quot; };
      String[] servers = { &quot;10.11.1.219:11211&quot; };      
//      Integer[] weights = { 3, 3, 2 };
      Integer[] weights = { 3};
      // grab an instance of our connection pool
      SockIOPool pool = SockIOPool.getInstance();
      // set the servers and the weights
      pool.setServers(servers);
      pool.setWeights(weights);
      // set some basic pool settings
      // 5 initial, 5 min, and 250 max conns
      // and set the max idle time for a conn
      // to 6 hours
      pool.setInitConn(5);
      pool.setMinConn(5);
      pool.setMaxConn(250);
      pool.setMaxIdle(1000 * 60 * 60 * 6);
      // set the sleep for the maint thread
      // it will wake up every x seconds and
      // maintain the pool size
      pool.setMaintSleep(30);
      // set some TCP settings
      // disable nagle
      // set the read timeout to 3 secs
      // and don't set a connect timeout
      pool.setNagle(false);
      pool.setSocketTO(3000);
      pool.setSocketConnectTO(0);
      // initialize the connection pool
      pool.initialize();
      // lets set some compression on for the client
      // compress anything larger than 64k
//      mcc.setCompressEnable(true);
//      mcc.setCompressThreshold(64 * 1024);
    }

    // from here on down, you can call any of the client calls
    public static void examples() {
      mcc.set(&quot;foo&quot;, &quot;This is a test String&quot;);
      ArrayList a1=new ArrayList();
      a1.add(0);
      a1.add(388);
      a1.add(989);
      a1.add(&quot;中文内容&quot;);
      
      mcc.set(&quot;foo2&quot;, a1);
      String bar = (String)mcc.get(&quot;foo&quot;);
      ArrayList a2 = (ArrayList)mcc.get(&quot;foo2&quot;);
      System.out.println(bar);
      System.out.println(a2.toString());
      
    }
   
    public static void main(String[] args) {
      // TODO Auto-generated method stub
      MyClass cs1=new MyClass();
      cs1.examples();
    }
   
}

运行结果:
This is a test String

说明:本程序在jdk7下测试通过,由于只使用了一个节点的memcached ,所以去掉了集群配置。         版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: Memcached1.4安装笔记