memcahed安装及测试

安装libevent

libevent 最新的稳定版:

wget http://monkey.org/~provos/libevent-1.4.14b-stable.tar.gz 
tar zxvf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable
./configure --prefix=/usr/local/libevent/
make
su root
make install

memcahed安装

memcached 最新的稳定版:

wget  http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
tar zxvf  memcached-1.4.5.tar.gz
cd memcached-1.4.5
./configure --prefix=/home/hadoop/soft/memcached --with-libevent=/usr/local/libevent/
make 
make install
ln -s /usr/local/libevent/lib/libevent-1.4.so.2 /lib/libevent-1.4.so.2

启动 memcached

启动参数说明:
-d 选项是启动一个守护进程,
-m 是分配给Memcache使用的内存数量,单位是MB,默认64MB
-M return error on memory exhausted (rather than removing items)
-u 是运行Memcache的用户,如果当前为root 的话,需要使用此参数指定用户。
-l 是监听的服务器IP地址,默认为所有网卡。
-p 是设置Memcache的TCP监听的端口,最好是1024以上的端口
-c 选项是最大运行的并发连接数,默认是1024
-P 是设置保存Memcache的pid文件
-f chunk size growth factor (default: 1.25)
-I Override the size of each slab page. Adjusts max item size(1.4.2版本新增)

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

bin/memcached -d -m 64 -u hadoop -l 127.0.0.1 -p 11211 -c 128 -P /tmp/memcached.pid

编写memcached服务启停脚本

cd /etc/init.d
vi memcached

脚本内容如下:

#!/bin/sh
#
# Startup script for the server of memcached
#http://blog.rekfan.com/?p=172
# processname: memcached
# pidfile: /usr/local/memcached/memcached.pid
# logfile: /usr/local/memcached/memcached_log.txt
# memcached_home: /etc/memcached
# chkconfig: 35 21 79
# description: Start and stop memcached Service

# Source function library
. /etc/rc.d/init.d/functions

RETVAL=0

prog="memcached"
basedir=/usr/local/memcached
cmd=${basedir}/bin/memcached
pidfile="$basedir/${prog}.pid"
#logfile="$basedir/memcached_log.txt"

# 设置memcached启动参数
ipaddr="192.168.1.101"          # 绑定侦听的IP地址
port="11211"                    # 服务端口
username="root"                 # 运行程序的用户身份
max_memory=64                   # default: 64M | 最大使用内存
max_simul_conn=1024             # default: 1024 | 最大同时连接数
#maxcon=51200
#growth_factor=1.3 # default: 1.25 | 块大小增长因子
#thread_num=6 # default: 4
#verbose="-vv" # 查看详细启动信息
#bind_protocol=binary # ascii, binary, or auto (default)

start() {
echo -n $"Starting service: $prog"
$cmd -d -m $max_memory -u $username -l $ipaddr -p $port -c $max_simul_conn -P $pidfile
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}

stop() {
echo -n $"Stopping service: $prog "
run_user=`whoami`
pidlist=`ps -ef | grep $run_user | grep memcached | grep -v grep | awk '{print($2)}'`
for pid in $pidlist
do
# echo "pid=$pid"
kill -9 $pid
if [ $? -ne 0 ]; then
return 1
fi
done
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
#reload)
# reload
# ;;
restart)
stop
start
;;
#condrestart)
# if [ -f /var/lock/subsys/$prog ]; then
# stop
# start
# fi
# ;;
status)
status memcached
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
esac
exit $RETVAL

设置脚本可被执行:

 chmod +x memcached
 chkconfig --add memcached
 chkconfig --level 35 memcached on

查看memcached是否启动:

 ps -ef | grep memcached

设置防火墙

vi /etc/sysconfig/iptables

将下面这行加入进去(注:将memcached加入到防火墙允许访问规则中)

-A RH-Firewall-l-INPUT -p tcp -m tcp --dport 11211 -j ACCEPT

防火墙重启

service iptables restart 

连接到 memcached

telnet localhost 11211

输入stats,会输出memcache的一些连接信息,包括PID
如果需要退出,则执行quit命令即可
memcached进程ID,很重要,如果我们需要关闭memcached服务时,就可以执行命令,kill -9进程号 即可

基本 memcached 客户机命令
您将使用五种基本 memcached 命令执行最简单的操作。这些命令和操作包括:

  1. set
  2. add
  3. replace
  4. get
  5. delete

=====================================================================

Java测试

下载Java客户端
https://github.com/gwhalin/Memcached-Java-Client

=========================================================
测试 1K
insertData(1,1000*64); success!耗时:7675
STAT 1:chunk_size 80
STAT 1:chunks_per_page 13107
STAT 1:total_pages 5
STAT 1:total_chunks 65535
STAT 1:used_chunks 64000
STAT 1:free_chunks 1535
STAT 1:free_chunks_end 0
STAT 1:mem_requested 4212890
STAT 1:get_hits 0
STAT 1:cmd_set 64000
STAT 1:delete_hits 0
STAT 1:incr_hits 0
STAT 1:decr_hits 0
STAT 1:cas_hits 0
STAT 1:cas_badval 0
STAT 1:touch_hits 0
STAT active_slabs 1
STAT total_malloced 5242800

insertData(10,100*64); success!耗时:1946
STAT 1:chunk_size 80
STAT 1:chunks_per_page 13107
STAT 1:total_pages 5
STAT 1:total_chunks 65535
STAT 1:used_chunks 64000
STAT 1:free_chunks 1535
STAT 1:free_chunks_end 0
STAT 1:mem_requested 4212890
STAT 1:get_hits 0
STAT 1:cmd_set 70400
STAT 1:delete_hits 0
STAT 1:incr_hits 0
STAT 1:decr_hits 0
STAT 1:cas_hits 0
STAT 1:cas_badval 0
STAT 1:touch_hits 0
STAT active_slabs 1
STAT total_malloced 5242800

insertData(100,10*64); success!耗时:406
STAT 1:chunk_size 80
STAT 1:chunks_per_page 13107
STAT 1:total_pages 5
STAT 1:total_chunks 65535
STAT 1:used_chunks 64000
STAT 1:free_chunks 1535
STAT 1:free_chunks_end 0
STAT 1:mem_requested 4212890
STAT 1:get_hits 0
STAT 1:cmd_set 71040
STAT 1:delete_hits 0
STAT 1:incr_hits 0
STAT 1:decr_hits 0
STAT 1:cas_hits 0
STAT 1:cas_badval 0
STAT 1:touch_hits 0
STAT active_slabs 1
STAT total_malloced 5242800

insertData(1000,1*64); success!耗时:56
STAT 1:chunk_size 80
STAT 1:chunks_per_page 13107
STAT 1:total_pages 5
STAT 1:total_chunks 65535
STAT 1:used_chunks 64000
STAT 1:free_chunks 1535
STAT 1:free_chunks_end 0
STAT 1:mem_requested 4212890
STAT 1:get_hits 0
STAT 1:cmd_set 71104
STAT 1:delete_hits 0
STAT 1:incr_hits 0
STAT 1:decr_hits 0
STAT 1:cas_hits 0
STAT 1:cas_badval 0
STAT 1:touch_hits 0
STAT active_slabs 1
STAT total_malloced 5242800

开始测试…
set success!耗时:8191
get success!耗时:4411
开始测试…
set success!耗时:545
get success!耗时:386
开始测试…
set success!耗时:48
get success!耗时:36
开始测试…
set success!耗时:5
get success!耗时:3

STAT pid 18869
STAT uptime 104
STAT time 1405923953
STAT version 1.4.20
STAT libevent 1.4.14b-stable
STAT pointer_size 32
STAT rusage_user 1.197817
STAT rusage_system 2.439629
STAT curr_connections 5
STAT total_connections 12
STAT connection_structures 11
STAT reserved_fds 20
STAT cmd_get 71104
STAT cmd_set 71104
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 71104
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 touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 3233311
STAT bytes_written 3316748
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 262144
STAT hash_is_expanding 0
STAT malloc_fails 0
STAT bytes 4881036
STAT curr_items 71104
STAT total_items 71104
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 0
STAT crawler_reclaimed 0

你可能感兴趣的:(memcached,缓存)