---
title: "memcached liunx安装"
author: "bjameng"
date: "2015-04-11T20:27:43Z"
tags: []
categories: ["操作系统"]
slug: "memcached-liunx"
draft: false
lastmod: "2015-04-11T20:27:43Z"
id: 2201362
url: "https://bjameng.iteye.com/blog/2201362"
---

最近在公司服务器上安装memcached安了很多次，每次都得上网来查一下步骤，今天干脆写个详细点的步骤记录一下，留作备忘。

 

**一、检查libevent**

首先检查系统中是否安装了libevent

Shell代码  [![收藏代码](http://chenzhou123520.iteye.com/images/icon_star.png)]()

1. rpm -qa|grep libevent

如果安装了则查看libevent的安装路径，后续安装时需要用到

Shell代码  [![收藏代码](http://chenzhou123520.iteye.com/images/icon_star.png)]()

1. rpm -ql libevent

如果没有安装，则先安装libevent

 

安装libevent：

1.首先下载libevent安装包

Shell代码  [![收藏代码](http://chenzhou123520.iteye.com/images/icon_star.png)]()

1. wget http://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gz

2.解压缩安装包

Shell代码  [![收藏代码](http://chenzhou123520.iteye.com/images/icon_star.png)]()

1. tar zxvf libevent-1.4.12-stable.tar.gz -C /usr/local/

3.进入解压后的目录

Shell代码  [![收藏代码](http://chenzhou123520.iteye.com/images/icon_star.png)]()

1. cd libevent-1.4.12-stable/

4.配置编译、安装

Shell代码  [![收藏代码](http://chenzhou123520.iteye.com/images/icon_star.png)]()

1. ./configure -prefix=/usr/libevent
2. make
3. make install

安装完libevent之后为了让动态链接库为系统所共享，需要执行以下动态链接库的管理命令ldconfig

不过在执行ldconfig命令之前需要注意以下libevent的安装目录。具体参见：[http://chenzhou123520.iteye.com/blog/1925196](http://chenzhou123520.iteye.com/blog/1925196)

 

**安装完libevent后正式开始安装memcached**

1.首先是下载memcached的安装包

Shell代码  [![收藏代码](http://chenzhou123520.iteye.com/images/icon_star.png)]()

1. wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz

2.修改安装包的执行操作权限

Shell代码  [![收藏代码](http://chenzhou123520.iteye.com/images/icon_star.png)]()

1. chmod 777 memcached-1.4.15.tar.gz

3.解压安装包到指定目录

Shell代码  [![收藏代码](http://chenzhou123520.iteye.com/images/icon_star.png)]()

1. tar zxvf memcached-1.4.15.tar.gz -C /usr/local

4.进入解压后的目录

Shell代码  [![收藏代码](http://chenzhou123520.iteye.com/images/icon_star.png)]()

1. cd /usr/local/memcached-1.4.15/

5.配置、编译、安装

注：configure时需要指定libevent的安装路径

Shell代码  [![收藏代码](http://chenzhou123520.iteye.com/images/icon_star.png)]()

1. ./configure -with-libevent=/usr/libevent/ -prefix=/usr/local/memcached
2. make
3. make install

6.安装成功后启动一下看安装是否成功

Shell代码  [![收藏代码](http://chenzhou123520.iteye.com/images/icon_star.png)]()

1. /usr/local/memcached/bin/memcached -d -m 2048 -p 11211 -u root

启动参数介绍如下：

-d选项是启动一个守护进程，

-m是分配给Memcache使用的内存数量，单位是MB，这里是10MB，

-u是运行Memcache的用户，这里是root，

-l是监听的服务器IP地址，如果有多个地址的话，这里指定了服务器的IP地址192.168.0.200，

-p是设置Memcache监听的端口，这里设置了12000，最好是1024以上的端口，

-c选项是最大运行的并发连接数，默认是1024，这里设置了256，按照服务器的负载量来设定，

-P是设置保存Memcache的pid文件，我这里是保存在 /tmp/memcached.pid，也可以启动多个守护进程，不过端口不能重复。

 

如果要给memcached上配置日志，参见：[http://chenzhou123520.iteye.com/blog/1925209](http://chenzhou123520.iteye.com/blog/1925209)

 

PS:附上一个memcached启动的shell脚本

Shell代码  [![收藏代码](http://chenzhou123520.iteye.com/images/icon_star.png)]()

01. #!/bin/sh
02. echo "Start to start memcached server $(date)..."
03. MEMCACHED=/usr/local/memcached/bin/memcached
04. usage()
05. {
06. echo "usage: \`basename $0\` port"
07. }
08. if \[ -n "$1" ]
09. then
10. {
11. pid=\`ps aux|grep memcached|grep "$1" |grep -v grep|awk '{print $2}'\`
12. if \[ -n "$pid" ]
13. then
14. {
15. sleep 2
16. echo "kill memcached which port is $1 begin"
17. echo "pid:$pid"
18. kill -9 $pid
19. echo "kill memcached which port is $1 end"
20. sleep 2
21. }
22. fi
23. echo "begin to start memcached in port $1"
24. LOG\_FILE=/var/log/memcached/memcached\_$1.log
25. rm -f $LOG\_FILE
26. $MEMCACHED -d -m 2048 -p $1 -u root -vv &gt;&gt; $LOG\_FILE 2&gt;&1
27. echo "start memcached end"
28. tail -f $LOG\_FILE
29. }
30. else
31. {
32. usage
33. exit 1
34. }
35. fi

把该脚本命名为startup.sh

执行：./startup.sh 11211

其中11211为memcached的启动端口，也可以通过自定义的端口来启动