Memcached 编译安装

memcached 是基于 libevent,先安装 libevent,libevent-devel

# yum -y install libevent libevent-devel
# cd /lamp
# wget -c http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
# tar zxvf memcached-1.4.15.tar.gz
# cd memcached-1.4.15
# ./configure --prefix=/usr/local/memcached
# make && make install
# useradd -M -d /dev/null -s /usr/sbin/nologin memcached

useradd创建一个不可以登陆、没有家目录的用户
-M:不创建家目录
-d:指定用户登入时的启始目录
-s:指定用户登陆时使用的shell,nologin就是登陆不了

因为系统不能用root运行memcached软件

#后台运行
/usr/local/memcached/bin/memcached –umemcached &
# netstat –tunpl|grep :11211
# telnet 192.168.10.1 11211
# stats

关闭程序

# ps -ef|grep memcached
(ps aux|grep memcached)
# kill -9 memcached进程号

写入自启动:

# vi /etc/rc.local
/usr/local/memcached/bin/memcached –umemcache &

(完)

—————————————————————————————-
CentOS安装memcached服务

Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、数据库驱动网站的速度。下面我们来一步步安装memcached。
1、首先为PHP安装扩展memcache。参考如下教程:

2、安装libevent

# yum -y install libevent-devel

3、下载memcached

# wget -c http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz
# tar xzf memcached-1.4.9.tar.gz
# cd memcached-1.4.9

4、开始编译安装memcached

# ./configure --prefix=/usr/local/memcached
# make
# make install

5、配置memcached
1)下载启动脚本

# wget http://www.centos.bz/wp-content/uploads/2011/11/memcached -O /etc/init.d/memcached
# chmod +x /etc/init.d/memcached

2)创建文件/usr/local/memcached/memcached.conf,并写入以下代码。

PORT=”11211″
USER=”memcached”
MAXCONN=”2048″
CACHESIZE=”64″
OPTIONS=””

PORT=”11211″:指定端口为11211
USER=”memcached”:以memcached用户运行
MAXCONN=”2048″:最大连接数
CACHESIZE=”64″:设置缓存内存为64M

3)建立用户memcached,加入启动项,并启动memcached

groupadd memcached
useradd -g memcached memcached
chkconfig –add memcached
chkconfig –level 2345 memcached on
service memcached start

—————————————————————————————-

6、查看Memcached状态

# printf "stats\r\n" | nc 127.0.0.1 11211

STAT pid 3025
STAT uptime 4120500
STAT time 1228021767
STAT version 1.2.6
STAT pointer_size 32
STAT rusage_user 433.463103
STAT rusage_system 1224.515845
STAT curr_items 1132460
STAT total_items 8980260
STAT bytes 1895325386
STAT curr_connections 252
STAT total_connections 547850
STAT connection_structures 1189
STAT cmd_get 13619685
STAT cmd_set 8980260
STAT get_hits 6851607
STAT get_misses 6768078
STAT evictions 0
STAT bytes_read 160396238246
STAT bytes_written 260080686529
STAT limit_maxbytes 2147483648
STAT threads 1
END

7、模拟top命令,查看Memcached状态:

# watch "printf 'stats\r\n' | nc 127.0.0.1 11211"
或者
# watch "echo stats | nc 127.0.0.1 11211"

—————————————————————————————-

配置环境变量

# cd ~
# vi .bash_profile

MEMCACHED_HOME=/usr/local/memcached
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$MEMCACHED_HOME/lib

刷新环境变量

# source .bash_profile

编写memcached服务脚本

# vi /etc/init.d/memcached

复制内容到 /etc/init.d/memcached
———————————————————

#!/bin/sh 
# 
# Startup script for the server of memcached 
# 
# processname: memcached 
# pidfile: /usr/local/memcached/memcached.pid 
# logfile: /usr/local/memcached/memcached_log.txt 
# memcached_home: /usr/local/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="127.0.0.1"              # 绑定侦听的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

———————————————————

设置memcached岁系统启动

# chkconfig --add memcached
# chkconfig --level 2345 memcached on

启动memcached

# service memcached start

启动的时候实际上是调用了下面的这个命令,以守护进程的方式来启动memcached

# /usr/local/memcached/bin/memcached -d -m 64 -u root -l 192.168.1.201 -p 11211 -c 1024 -P /usr/local/memcached/memcached.pid

查看memcached是否启动:\

# ps -ef | grep memcached

Leave a Reply