在CentOS7上部署Memcached高性能内存缓存对象

概述

Memcached是一套 开源的高性能分布式内存对象缓存系统,它将所有的数据 都存储在内存中,因为在内存中会统一维护一张巨大的Hash表,所以支持任意存储类型的数据。

Memcached是典型的C/S架构,因此需要安装Memcached服务端与MemcachedAPI客户端。Memcached服务端是用C语言编写的,而Memcached API客户端可以用任何语言来编写。常用典型架构如图所示:
在CentOS7上部署Memcached高性能内存缓存对象

当Web客户端发送请求到Web服务器的应用程序时,应用程序会通过调用MemcachedAPI客户端程序库接口连接Memcached服务器,进而查询数据。如果此时的Web客户端所求的数据已经在Memcached服务端中缓存,则Memcached服务端会将数据返回给Web客户端;如果数据不存在,则会将Web客户端请求发送至Mysql数据库,由数据库将请求的数据返回给Memcached以及Web客户端,与此同时Memcached服务器也会将数据进行保存,以方便用户下次请求使用。

实验步骤

安装Memcached服务器

1.安装Libevent

[root@localhost Mem]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/
[root@localhost Mem]# cd /opt/libevent-2.1.8-stable/
[root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent
[root@localhost libevent-2.1.8-stable]#make && make install

2.安装Memcached

[root@localhost Mem]# tar zxvf memcached-1.5.6.tar.gz -C /opt/
[root@localhost Mem]# cd /opt/memcached-1.5.6/
[root@localhost Mem]# ./configure \
--prefix=/usr/local/memcached \
--with-libevent=/usr/local/libevent/
[root@localhost memcached-1.5.6]# make && make install

[root@localhost memcached-1.5.6]# ln -s /usr/local/memcached/bin/* /usr/local/bin/ #软链接

3.开启memcached服务

[root@localhost memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root
[root@localhost memcached-1.5.6]# netstat -anpt | grep memc
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      11330/memcached     
tcp6       0      0 :::11211                :::*                    LISTEN      11330/memcached 
参数注解:
-d 以守护进程的方式运行memcached服务
-m 为memcached分配32MB的内存
-p 端口号
-u 指定运行的用户账号

4.验证数据操作

[root@localhost memcached-1.5.6]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
add username 0 0 7      #添加键值数据
1234567
STORED
get username       #查看数据
VALUE username 0 7
1234567
END
flush_all          #清除所有缓存数据
OK
get username
END            #清楚成功
quit          #退出
Connection closed by foreign host.
安装Memcached API客户端

1.安装客户端--需要在LAMP架构的基础上进行,这里不再详细编写LNMP架构过程。

[root@localhost ~]# yum install autoconf -y
[root@localhost LAMP-7]# tar zxvf memcache-2.2.7.tgz -C /opt/
[root@localhost LAMP-7]# cd /opt/memcache-2.2.7/
[root@localhost memcache-2.2.7]# /usr/local/php5/bin/phpize #增加为PHP的模块后再对memcache进行配置编译

Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
[root@localhost memcache-2.2.7]#
./configure \
--enable-memcache \
--with-php-config=/usr/local/php5/bin/php-config
[root@localhost memcache-2.2.7]# make && make install

注意配置Memcached API时,memcached源码包中默认没有configure配置脚本,需要使用PHP的phpize脚本生成配置脚本configure。

[root@localhost memcache-2.2.7]# vim /usr/local/php5/php.ini
; extension_dir = "ext"
extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/"    #添加
extension = memcache.so   添加

2.在客户端去检测服务端是否可以连接

1)编写测试页面,测试Memcached API功能。

root@localhost memcache-2.2.7]# cd /usr/local/httpd/htdocs/
[root@localhost htdocs]# vim index.php 

<?php
$memcache = new Memcache();
$memcache->connect('192.168.126.138',11211);
$memcache->set('key','Memcache test Successfull!',0,60);
$result = $memcache->get('key');
unset($memcache);
echo $result;
?>
~      

[root@localhost htdocs]# service httpd restart

此段代码的作用是在客户端连接Memcached服务器,设置名为'key'的键的值为Memcache test Successfull,并读取显示。显示成功,则表示服务器与客户端协同工作正常。使用浏览器进行访问,测试结果如图所示。
在CentOS7上部署Memcached高性能内存缓存对象

猜你喜欢

转载自blog.51cto.com/13642258/2151980