memcached 설치 방법

2011. 6. 3. 13:19 from Cloud/etc

MEMCACHED INSTALL


wget http://memcached.googlecode.com/files/memcached-1.4.8.tar.gz 
tar zxvf memcached-1.4.7.tar.gz
cd memcached-1.4.7
./configure

다음의 에러가 발생하고 중단되면,

checking for libevent directory... configure: error: libevent is required.  You can get it from http://www.monkey.org/~provos/libevent/ 

다음을 실행한다.

yum install libevent*

다시 실행

./configure
make
make install

아래와 같이 2개의 파일이 설치된다.

/usr/local/bin/memcached
/usr/local/include/memcached

다음과 같이 해도 된다고는 하나, php모듈만 제외하고 설치가 이루어지지 않는다.

yum install memcached php-pecl-memcache memcached-selinux

모듈 제대로 설치 확인

[root@localhost memcached-1.4.7]# ldd /usr/local/bin/memcached
 libevent-1.4.so.2 => /usr/lib64/libevent-1.4.so.2 (0x00002ad04fe7a000)
 librt.so.1 => /lib64/librt.so.1 (0x0000003332a00000)
 libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003331600000)
 libc.so.6 => /lib64/libc.so.6 (0x0000003330e00000)
 libnsl.so.1 => /lib64/libnsl.so.1 (0x0000003332e00000)
 libresolv.so.2 => /lib64/libresolv.so.2 (0x0000003334e00000)
 /lib64/ld-linux-x86-64.so.2 (0x0000003330a00000)

위에서 lib들이 /usr/lib, /usr/lib64, /lib, /lib64가 아닐 경우에는

more /etc/ld.so.conf
include ld.so.conf.d/*.conf
/usr/src/hoard-38/src
/usr/local/lib

echo "/usr/local/lib64" >> /etc/ld.so.conf

ldconfig /etc/ld.so.conf

실행전 메모리 확인

vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0 156932  74276   2132  27056    3   50   304   116 1030  119  2  1 88  8  0

실행, 실행확인하기

[root@localhost memcached-1.4.7]# memcached -d -m 50 -p 11211 -u nobody
[root@localhost memcached-1.4.7]# ps ax | grep memcached
 6279 ?        Ssl    0:00 memcached -d -m 50 -p 11211 -u nobody
 6286 pts/0    S+     0:00 grep memcached

실행상태 확인

telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
stats  <- 명령어
STAT pid 6279
STAT uptime 45
STAT time 1316274124
STAT version 1.4.7
STAT libevent 1.4.13-stable
STAT pointer_size 64
STAT rusage_user 0.000999
STAT rusage_system 0.000999
STAT curr_connections 10
STAT total_connections 11
STAT connection_structures 11
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 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
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 7
STAT bytes_written 0
STAT limit_maxbytes 52428800
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
STAT reclaimed 0
END
quit <- 명령어
Connection closed by foreign host.

실행후의 메모리 확인, 변화가 없다.

vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0 156932  71480   2300  28428    3   48   293   111 1029  118  2  1 89  8  0 
wget http://launchpad.net/libmemcached/1.0/0.52/+download/libmemcached-0.52.tar.gz
tar xzvf libmemcached-0.52.tar.gz
cd libmemcached-0.52
./configure
make
make install

vi memc_basic.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libmemcached/memcached.h>

int main(int argc, char *argv[])
{
  memcached_server_st *servers = NULL;
  memcached_st *memc;
  memcached_return rc;
  char *key= "keystring";
  char *value= "keyvalue";

  memc= memcached_create(NULL);

  servers= memcached_server_list_append(servers, "localhost", 11211, &rc);
  rc= memcached_server_push(memc, servers);

  if (rc == MEMCACHED_SUCCESS)
    fprintf(stderr,"Added server successfully\n");
  else
    fprintf(stderr,"Couldn't add server: %s\n",memcached_strerror(memc, rc));

  rc= memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint32_t)0);

  if (rc == MEMCACHED_SUCCESS)
    fprintf(stderr,"Key stored successfully\n");
  else
    fprintf(stderr,"Couldn't store key: %s\n",memcached_strerror(memc, rc));

  return 0;
}

<끝>

gcc -o memc_basic memc_basic.c -lmemcached
./memc_basic 

아래처럼 에러가 발생하면

[root@localhost libmemcached-0.52]# ./memc_basic
./memc_basic: error while loading shared libraries: libmemcached.so.8: cannot open shared object file: No such file or directory

ldconfig /etc/ld.so.conf 실행후에,

[root@localhost libmemcached-0.52]# ./memc_basic Added server successfully Key stored successfully

vi memc_multi_fetch.c

#include <stdio.h>
#include <sstring.h>
#include <unistd.h>
#include <libmemcached/memcached.h>

int main(int argc, char *argv[])
{
  memcached_server_st *servers = NULL;
  memcached_st *memc;
  memcached_return rc;
  const char *keys[]= {"huey", "dewey", "louie"};
  size_t key_length[3];
  char *values[]= {"red", "blue", "green"};
  size_t value_length[3];
  unsigned int x;
  uint32_t flags;

  char return_key[MEMCACHED_MAX_KEY];
  size_t return_key_length;
  char *return_value;
  size_t return_value_length;

  memc= memcached_create(NULL);

  servers= memcached_server_list_append(servers, "localhost", 11211, &rc);
  rc= memcached_server_push(memc, servers);

  if (rc == MEMCACHED_SUCCESS)
    fprintf(stderr,"Added server successfully\n");
  else
    fprintf(stderr,"Couldn't add server: %s\n",memcached_strerror(memc, rc));

  for(x= 0; x < 3; x++)
    {
      key_length[x] = strlen(keys[x]);
      value_length[x] = strlen(values[x]);

      rc= memcached_set(memc, keys[x], key_length[x], values[x],
                        value_length[x], (time_t)0, (uint32_t)0);
      if (rc == MEMCACHED_SUCCESS)
        fprintf(stderr,"Key %s stored successfully\n",keys[x]);
      else
        fprintf(stderr,"Couldn't store key: %s\n",memcached_strerror(memc, rc));
    }

  rc= memcached_mget(memc, keys, key_length, 3);

  if (rc == MEMCACHED_SUCCESS)
    {
      while ((return_value= memcached_fetch(memc, return_key, &return_key_length,
                                            &return_value_length, &flags, &rc)) != NULL)
        {
          if (rc == MEMCACHED_SUCCESS)
            {
              fprintf(stderr,"Key %s returned %s\n",return_key, return_value);
            }
        }
    }

  return 0;
}

<끝>

gcc -o memc_multi_fetch memc_multi_fetch.c -lmemcached

[root@localhost libmemcached-0.52]# ./memc_multi_fetch
Added server successfully
Key huey stored successfully
Key dewey stored successfully
Key louie stored successfully
Key huey + returned red
Key dewey+ returned blue
Key louie+ returned green

mysql과의 연동 시험

wget http://launchpad.net/memcached-udfs/trunk/1.1/+download/memcached_functions_mysql-1.1.tar.gz
tar xzvf memcached_functions_mysql-1.1.tar.gz
cd memcached_functions_mysql-1.1
./configure
make
make install

include에 있는 파일하고, 이 버전하고 type이 맞지 않아 에러를 계속 발생시키고 있다. mysql-1.1버전은 2009년 11월 버전이고, memcached-1.47버전은 2011년이어서 맞지 않는거 같다. 0.35버전이 2009년 11월버전이다.

출처 - http://calmroad.blog.me/100138253397

Posted by 신공표 트랙백 1 : 댓글 0

댓글을 달아 주세요