进入下载目录
[html] view plain copy print?- cd /usr/local
安装libevent
[html] view plain copy print?
- wget https://github.com/libevent/libevent/archive/release-1.4.12-stable.tar.gz
由于memcached是基于libevent的,因此需要安装libevent,libevent-devel。
[html] view plain copy print?- tar -zxvf libevent-1.4.12-stable.tar.gz
- cd libevent-1.4.12
- ./configure --prefix=/usr/local/libevent
- make
- make install
安装memcached
[html] view plain copy print?
- wget http://www.memcached.org/files/memcached-1.4.25.tar.gz
[html] view plain copy print?
- tar -zxvf memcached-1.4.25.tar.gz
- cd memcached-1.4.25
- ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent --enable-64bit --enable-threads
- make
- make install
PS:
--enable-64bit 64位系统需要这个参数
--enable-threads 支持多线程
启动memcache服务
[html] view plain copy print?- /usr/local/memcached/bin/memcached -d -m 200 -u root -l 127.0.0.1 -p 11211 -c 1000 -P /var/run/memcached.pid
- 相关解释如下:
- -d选项是启动一个守护进程,
- -m是分配给Memcache使用的内存数量,单位是MB,这里是200MB
- -u是运行Memcache的用户,如果当前为 root 的话,需要使用此参数指定用户。
- -l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址127.0.0.1
- -p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口
- -c选项是最大运行的并发连接数,默认是1024,这里设置了256
- -P是设置保存Memcache的pid文件,我这里是保存在 /var/run/memcached.pid
- 停止Memcache进程:
- # kill `cat/var/run/memcached.pid`
- 也可以启动多个守护进程,但是端口不能重复
开机自启动
首先请登陆你的Linux服务器在/etc/rc.d/init.d/目录下创建一个文件
[html] view plain copy print?- touch /etc/init.d/memcached
给启动脚本赋予执行权限
[html] view plain copy print?
- chmod +x /etc/init.d/memcached
接着用vi memcached来编辑这个文件
接着在memcached里面输入如下内容
[html] view plain copy print?
- #!/bin/bash
- # Startup script for the memcached
- # chkconfig: 2345 55 45
- # description: The memcached daemon is a network memory cache service.
- # processname: memcached
- # pidfile: /var/run/memcached.pid
- EXEC=/usr/local/memcached/bin/memcached
- PIDFILE=/var/run/memcached.pid
- RETVAL=0
- prog="memcached"
- # Source function library.
- . /etc/init.d/functions
- # Source networking configuration.
- . /etc/sysconfig/network
- # Check that networking is up.
- [ ${NETWORKING} = "no" ] && exit 0
- [ -x $EXEC ] || exit 0
- # Start memcached daemons functions.
- start() {
- if [ -e $PIDFILE ];then
- echo "memcached already running...."
- exit 1
- fi
- echo -n $"Starting $prog: "
- daemon $EXEC -d -p 11211 -u root -m 128 -c 1024 -P $PIDFILE
- RETVAL=$?
- echo
- [ $RETVAL = 0 ] && touch /var/lock/subsys/memcached
- return $RETVAL
- }
- # Stop memcahced daemons functions.
- stop() {
- echo -n $"Stopping $prog: "
- killproc memcached
- RETVAL=$?
- echo
- [ $RETVAL = 0 ] && rm -f /var/lock/subsys/memcached $PIDFILE
- }
- # See how we were called.
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- restart|reload)
- stop
- start
- ;;
- status)
- status $prog
- RETVAL=$?
- ;;
- condrestart)
- [ -f /var/lock/subsys/memcached ] && restart || :
- ;;
- *)
- echo $"Usage: $prog {start|stop|restart|reload|status|condrestart}"
- exit 1
- esac
- exit $RETVAL
添加服务
[html] view plain copy print?- chkconfig --add memcached
设置开机启动
[html] view plain copy print?- chkconfig --level 35 memcached on
15套Java架构师、集群、高可用、高可扩展、高性能、高并发、性能优化、spring boot、Redis、ActiveMQ、Nginx、Mycat、Netty、Jvm大型分布式项目实战视频教程点击打开链接
查看是否设置成功
[html] view plain copy print?
- chkconfig --list | grep memcached