Chinaunix首页 | 论坛 | 博客
  • 博客访问: 221267
  • 博文数量: 71
  • 博客积分: 1649
  • 博客等级: 上尉
  • 技术积分: 725
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-25 11:20
文章分类

全部博文(71)

文章存档

2012年(1)

2011年(11)

2010年(59)

我的朋友

分类: LINUX

2010-09-01 10:17:09

Linux下缓存服务器-memcached

环境:

    libevent

    php支持

    我此次安装的memcache是在lamp环境下:RedHat 5.4-bit64 ApacheMySQLPHP的安装路径都在/usr/local下;源代码包都放在/usr/local/src

相关软件:

    memcached .

    memcache

    libevent

安装libevent

    cd /usr/local/src/
    tar zxvf libevent-1.3d.tar.gz 
    cd libevent-1.3d
    ./configure 
    make && make install
    ln -s /usr/local/lib/libevent-1.3d.so.1 /usr/lib64/libevent-1.3d.so.1

    如果是32位系统的话则改为:

    ln -s /usr/local/lib/libevent-1.3d.so.1 /usr/lib/libevent-1.3d.so.1

安装Memcached

    cd /usr/local/src

    tar vzxf memcached-1.4.5.tar.gz 
    cd memcached-1.4.5
    ./configure --prefix=/usr/local/memcached
    make && make install

启动服务
    cd /usr/local/memcached/bin/
    ./memcached -d -m 1024 -p 11211 -u root

    参数说明 -m 指定缓存空间大小单位兆 ;-p指定监听端口 一般为11211 -u指定用哪个用户来运行 一般是root

安装memcache PHP模块

    cd /usr/local/src/
    tar zxvf memcache-2.2.5.tgz 
    cd memcache-2.2.5

    /usr/local/php/bin/phpize 

   (这步可能会提示错误

[root@localhost memcache-2.2.5]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519
Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF
environment variable is set correctly and then rerun this script.

 

如果出现这种情况yum install autoconf就可以了

这样会装上两个包:imake       autoconf)
    ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
    make && make install

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

    extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-XXXXXXXX(X代表数字)

     然后修改/usr/local/php/lib/下的php.ini

     找到extension_dir 这一行

     如果是extension_dir = "./"

     则修改为

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

     如果是其他路径则添加一行

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

     或者把"/usr/local/php/lib/php/extensions/no-debug-non-zts-XXXXXXXX/"下的文件拷到原来制定的目录下也可以

     因为我原来添加过Apache的扩展模块,我的路径是extension_dir = "/usr/local/apache/modules/"

所以我又添加了一行新的。

最后一步在/usr/local/php/lib/(这个路径要根据实际情况,这个文件是在编译PHP时自己设定的)下的php.ini添加一行extension=memcache.so

     (本人试验了好几次,有时候extension_dir = "XXXXXXXXXXXXXX"extension=memcache.so会自动添加,有时候需要手动大家改之前最好先看下是否已被自动添加)

测试脚本

    安装完成了写个PHP脚本测试一下吧:

 


$memcache = new Memcache; //创建一个memcache对象
$memcache->connect('localhost', 11211) or die ("Could not connect"); //连接Memcached服务器;localhost可以换成ip 11211是启动时指定的端口
$memcache->set('key', 'This is a test'); //设置一个变量到内存中,名称是key 值是This is a test

$get_value = $memcache->get('key'); //从内存中取出key的值
echo $get_value;
?>

如果输出This is a test!表示环境搭建成功。

 

启动参数详解

 

[root@localhost ~]#/usr/local/memcached/bin/memcached -h

memcached 1.4.5

-p       TCP port number to listen on (default: 11211) #设置监听的TCP端口号

-U       UDP port number to listen on (default: 11211, 0 is off) #设置监听的UDP端口号。

-s      UNIX socket path to listen on (disables network support) #关闭网络支持,使用Unix Socket

-a      access mask for UNIX socket, in octal (default: 0700) #设置Unix Socket的访问掩码

-l   interface to listen on (default: INADDR_ANY, all addresses)

-d            run as a daemon #以后台守护进程方式运行

-r            maximize core file limit

-u assume identity of (only when run as root)

-m       max memory to use for items in megabytes (default: 64 MB) #设置最大内存使用量

-M            return error on memory exhausted (rather than removing items)

-c       max simultaneous connections (default: 1024)

-k            lock down all paged memory.  Note that there is a

              limit on how much memory you may lock.  Trying to

              allocate more than that would fail, so be sure you

              set the limit correctly for the user you started

              the daemon with (not for -u user;

              under sh this is done with 'ulimit -S -l NUM_KB').

-v            verbose (print errors/warnings while in event loop)#显示详细的运行状态,在前台启动

-vv           very verbose (also print client commands/reponses) #显示更详细的运行状态。

-vvv          extremely verbose (also print internal state transitions)

-h            print this help and exit #打印此帮助信息

-i            print memcached and libevent license

-P      save PID in , only used with -d option #指定PID文件

-f    chunk size growth factor (default: 1.25)

-n     minimum space allocated for key+value+flags (default: 48)

-L            Try to use large memory pages (if available). Increasing

              the memory page size could reduce the number of TLB misses

              and improve the performance. In order to get large pages

              from the OS, memcached will allocate the total item-cache

              in one large chunk.

-D      Use as the delimiter between key prefixes and IDs.

              This is used for per-prefix stats reporting. The default is

              ":" (colon). If this option is specified, stats collection

              is turned on automatically; if not, then it may be turned on

              by sending the "stats detail on" command to the server.

-t       number of threads to use (default: 4)

-R            Maximum number of requests per event, limits the number of

              requests process for a given connection to prevent

              starvation (default: 20)

-C            Disable use of CAS

-b            Set the backlog queue limit (default: 1024)

-B            Binding protocol - one of ascii, binary, or auto (default)

-I            Override the size of each slab page. Adjusts max item size

              (default: 1mb, min: 1k, max: 128m)

 

 

 

阅读(1877) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~