转载文章,转自
http://www.sunchis.com/html/db/memcached/2011/0526/333.html
1、 由于memcached是基于libevent的,因此需要安装libevent,libevent- devel
- # yum install libevent libevent-devel -y
2、 下载并解压memcached-1.4.5
memcached官方网站是:http://memcached.org/
- # cd /root
- # wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
- # tar -xvzf memcached-1.4.5.tar.gz
3、 编译安装memcached-1.4.5
- # cd memcached-1.4.5
- # ./configure --prefix =/etc/memcached
- # make
- # make install
4、 配置环境变量
进入用户宿主目录,编辑.bash_profile,为系统环境变量LD_LIBRARY_PATH增加新的目录,需要增加的内容如下:
# vi .bash_profile
- MEMCACHED_HOME =/etc/memcached
- export LD_LIBRARY_PATH =$LD_LIBRARY_PATH:$MEMCACHED_HOME/lib
刷新用户环境变量:# source .bash_profile
5、 编写memcached服务启停脚本
# cd /etc/init.d
vi memcached,脚本内容如下:
- #!/bin/sh
- #
- # Startup script for the server of memcached
- #
- # processname: memcached
- # pidfile: /etc/memcached/memcached.pid
- # logfile: /etc/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=/etc/memcached
- cmd=${basedir}/bin/memcached
- pidfile="$basedir/${prog}.pid"
- #logfile="$basedir/memcached_log.txt"
- # 设置memcached启动参数
- ipaddr="192.168.1.201" # 绑定侦听的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
6、 设置memcached随系统启动
- # chkconfig --add memcached
- # chkconfig --level 35 memcached on
- # service memcached start
- //启动的时候实际上是调用了下面的这个命令,以守护进程的方式来启动memcached
- /etc/memcached/bin/memcached -d -m 64 -u root -l 192.168.1.201
- \-p 11211 -c 1024 -P /etc/memcached/memcached.pid
查看memcached是否启动:
# ps -ef | grep memcached