---
title: "Centos:Memcache安装配置【转】"
author: "Hobo86"
date: "2013-06-17T15:43:22Z"
tags: []
categories: ["操作系统"]
slug: "centos-memcache"
draft: false
lastmod: "2013-06-17T15:42:52Z"
id: 1839994
url: "https://hobo86.iteye.com/blog/1839994"
---

原文：[http://www.bootf.com/442.html](http://www.bootf.com/442.html)

本教程将介绍如何在CentOS中安装Memcache缓存服务。Memcache是一个与php兼容的内存高速缓存插件，不仅可以缓存变量等对象，而且可以与MySQL配合，缓存数据查询。由于Memcache在内存中缓存数据，因此它的读取写入速度非常之快，能为大容量快速变化的动态数据提供高速缓存。

由于编译安装Memcache步骤相对复杂一些，因此本文将以CentOS系统下yum直接安装Memcache为例进行讲解，这种安装方法快捷简便。

**1、由于CentOS系统默认源没有memcache安装包，因此需要导入第三方的源。执行如下两条命令：**

`1` `[root@www ~]# wget http://soft.bootf.com/rpm/epel-release-5-4.noarch.rpm`

`2` `[root@www ~]# rpm -ivh epel-release-5-4.noarch.rpm`

注：网上大部分资料均是人云亦云要求yum使用RPMForge源。但经过VPS管理百科实际测试，此源里不包含memcached包，因此无法正常安装。按照VPS管理百科提供的源与方法安装即可。

**2、查看已经安装的源**

`01` `[root@www ~]# yum repolist`

`02` `Loaded plugins: fastestmirror`

`03` `Loading mirror speeds from cached hostfile`

`04` `* base: centos.ustc.edu.cn`

`05` `* epel: mirrors.ustc.edu.cn`

`06` `* extras: centos.ustc.edu.cn`

`07` `* rpmforge: fr2.rpmfind.net`

`08` `* updates: centos.ustc.edu.cn`

`09` `repo id` `repo name status`

`10` `base CentOS-5 - Base 2,705`

`11` `epel Extra Packages for` `Enterprise Linux 5 - i386 5,579`

`12` `extras CentOS-5 - Extras 282`

`13` `updates CentOS-5 - Updates 455`

`14` `repolist: 20,115`

能够找到epel包，说明安装成功。

**3、yum安装Memcache服务器与php扩展**

`1` `[root@www ~]# yum install memcached php-pecl-memcache`

此时应该能正常安装这两个包，而不出现无法找到的情况。

**4、安装成功后，检测php是否正常加载了memcache模块：**

`1` `[root@www ~]# php -m|grep memcache`

`2` `memcache`

返回了“memcache”表示已经安装。

**5、设置memcached服务开机自动启动**

`1` `[root@www ~]# chkconfig --level 2345 memcached on`

**6、启动memcached服务并重启Apache**

`1` `[root@www ~]# /etc/init.d/memcached start`

`2` `启动 memcached：[确定]`

`3` `[root@www ~]# /etc/init.d/httpd restart`

`4` `停止 httpd：[确定]`

`5` `启动 httpd：[确定]`

**7、测试php支持memcache是否正常**

在apache的网站根目录建立 memcache.php 文件

`1` `vi` `memcache.php`

内容如下：

`1` `<?php`

`2` `$memcache` `= new` `Memcache();`

`3` `$memcache->connect('127.0.0.1', 11211);`

`4` `$memcache->set('key', 'Memcache test successful!', 0, 60);`

`5` `$result` `= $memcache->get('key');`

`6` `unset($memcache);`

`7` `echo` `$result;`

`8` `?>`

如果一切正常，访问此页面，应该正常返回“Memcache test successful”，至此，Memcached与php扩展memcache安装成功。

Memcached的默认端口为11211，因此在php中使用此端口即可。下面顺便给出个清除memcache所有缓存内容的方法：

执行：

`1` `[root@www ~]# nc localhost 11211`

然后输入：

`1` `flush_all`

`2` `quit`

即可。