jxwjq 发表于 2018-12-24 13:10:49

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

概述
  Memcached是一套 开源的高性能分布式内存对象缓存系统,它将所有的数据 都存储在内存中,因为在内存中会统一维护一张巨大的Hash表,所以支持任意存储类型的数据。
  Memcached是典型的C/S架构,因此需要安装Memcached服务端与MemcachedAPI客户端。Memcached服务端是用C语言编写的,而Memcached API客户端可以用任何语言来编写。常用典型架构如图所示:
http://i2.运维网.com/images/blog/201807/30/69aaf18c8bfb9b15726a578de4ec1e31.jpg
  当Web客户端发送请求到Web服务器的应用程序时,应用程序会通过调用MemcachedAPI客户端程序库接口连接Memcached服务器,进而查询数据。如果此时的Web客户端所求的数据已经在Memcached服务端中缓存,则Memcached服务端会将数据返回给Web客户端;如果数据不存在,则会将Web客户端请求发送至Mysql数据库,由数据库将请求的数据返回给Memcached以及Web客户端,与此同时Memcached服务器也会将数据进行保存,以方便用户下次请求使用。

实验步骤

安装Memcached服务器
  1.安装Libevent

# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/
# cd /opt/libevent-2.1.8-stable/
# ./configure --prefix=/usr/local/libevent
#make && make install
  2.安装Memcached

# tar zxvf memcached-1.5.6.tar.gz -C /opt/
# cd /opt/memcached-1.5.6/
# ./configure \
--prefix=/usr/local/memcached \
--with-libevent=/usr/local/libevent/
# make && make install
# ln -s /usr/local/memcached/bin/* /usr/local/bin/ #软链接
  3.开启memcached服务

# memcached -d -m 32m -p 11211 -u root
# 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.验证数据操作

# 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架构过程。

# yum install autoconf -y
# tar zxvf memcache-2.2.7.tgz -C /opt/
# cd /opt/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
#
./configure \
--enable-memcache \
--with-php-config=/usr/local/php5/bin/php-config
# make && make install

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

# 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/
# vim index.php

~      
# service httpd restart
  此段代码的作用是在客户端连接Memcached服务器,设置名为'key'的键的值为Memcache test Successfull,并读取显示。显示成功,则表示服务器与客户端协同工作正常。使用浏览器进行访问,测试结果如图所示。
http://i2.运维网.com/images/blog/201807/30/bba5561adea4e55b9f14e8c3a62c1bd5.jpg



页: [1]
查看完整版本: 在CentOS7上部署Memcached高性能内存缓存对象