centos下安装memcached

来源:互联网 发布:js冲锋枪图片 编辑:程序博客网 时间:2024/05/20 18:43

作者:tonyvicky
来自:LinuxSir.Org
摘要:由于数据库存储的数据量越来越大,查询速度也就变的越来越慢,因此就有了缓存服务器应用的必要,本文是介绍Memcached的安装以及简单的使用。

本文只介绍memcached的PHP的API,想查看其他关于Memcached的API文档案,请访问 http://www.danga.com/memcached/

目录

    一、环境需求
    二、下载相关软件
    三、安装和配置

     

      1、安装Memcached
      2、安装memcache PHP模块
      3、测试脚本

    四、关于本文


++++++++++++++++++++++++++++++++++++++++
正文
++++++++++++++++++++++++++++++++++++++++

一、环境需求
安装Memcached需要libevent库的支持,所以请在安装Memcached之前检查有没有安装libevent。测试环境还需要PHP的支持,本文假设PHP已经安装到/usr/local/php目录下,也就是在编译PHP的时候使用perfix参数指定目录(--prefix=/usr/local/php)

二、下载相关软件

Memcached下载地址:http://www.danga.com/memcached/
memcache PHP模块下载地址: http://pecl.php.net/package/memcache 推荐使用1.5版
libevent 下载地址: http://www.monkey.org/~provos/libevent/

本文不再讲述如何安装libevent

三、安装和配置

1、安装Memcached

 

root@tonyvicky:# tar vxzf memcached-1.1.12.tar.gz
root@tonyvicky:# cd memcached-1.1.12
root@tonyvicky:# ./configure --prefix=/usr/local/memcached
root@tonyvicky:# make
root@tonyvicky:# make install

 

安装完之后要启动服务

 

root@tonyvicky:# cd /usr/local/memcached/bin
root@tonyvicky:# ./memcached -d -m 50 -p 11211 -u root

 

参数说明 -m 指定使用多少兆的缓存空间;-p 指定要监听的端口; -u 指定以哪个用户来运行

2、安装memcache PHP模块

 

root@tonyvicky:# tar vxzf memcache-1.5.tgz
root@tonyvicky:# cd memcache-1.5
root@tonyvicky:# /usr/local/php/bin/phpize
root@tonyvicky:# ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
root@tonyvicky:# make
root@tonyvicky:# make install

 

安装完后会有类似这样的提示:

 

Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/

 

把这个记住,然后修改php.ini,把

 

extension_dir = "./"

 

修改为

 

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/"

 

并添加一行

 

extension=memcache.so

 

3、测试脚本

自己写一个PHP程序测试一下吧

<?php
$memcache
= new Memcache; //创建一个memcache对象
$memcache->connect('localhost', 11211) or die ("Could not connect"); //连接Memcached服务器
$memcache->set('key', 'test'); //设置一个变量到内存中,名称是key 值是test
$get_value = $memcache->get('key'); //从内存中取出key的值
echo $get_value;
?>
来源:http://www.linuxsir.org/main/?q=node/184
原创粉丝点击