高性能内存对象缓存Memcached

高性能内存对象缓存Memcached

Memcached简介

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

1、学会安装Memcacahed服务

1.1安装Libevent

Libevent是一款跨平台的事件处理接口的封装,可以兼容多个操作系统的事件访问。Memcached的安装依赖于Libevent,因此需要先完成Libevent的安装。

挂载源代码包

[root@localhost ~]# mount.cifs //192.168.32.1/gongxiang /abc/
Password for root@//192.168.32.1/gongxiang:  
[root@localhost ~]# cd /abc/
[root@localhost abc]# ls
all_locales-1.4.18-20090526.tar.gz   magent-0.5.tar.gz
apachenk                             memcache-2.2.7.tgz
apache.sh                            memcached-1.5.6.tar.gz
apr-1.4.6.tar.gz                     mongodb-linux-x86_64-rhel70-4.0.0.tgz
apr-1.5.0.tar.gz                     mysql
apr-util-1.4.1.tar.gz                mysql-5.5.24.tar.gz
apr-util-1.5.4.tar.gz                nginx-1.15.0.tar.gz
awstats-7.0.zip                      nginx-1.2.8.tar.gz
awstats-7.6.tar.gz                   nginx-1.6.0.tar.gz
dage.jpg                             openssl-1.0.1c.tar.gz
dage.jpg.jpg                         pcre-8.39.tar.gz
desktop.ini                          php
Discuz_X2.5_SC_UTF8.zip              php-5.4.5.tar.bz2
dnf-dnf-1.1.7-1.tar.gz               pxe.sh.txt
epel-release-latest-7.noarch.rpm     Red Hat Enterprise 6.5 x86_64.iso
game.jpg                             rhel-server-7.3-x86_64-dvd.iso
gd-2.0.35.tar.gz                     sarg-2.3.7.tar.gz
hping-2.0.0-1.rc3.2.el6.rf.i686.rpm  shell.sh.txt
httpd-2.4.2.tar.gz                   snmpd.conf.txt
httpd-2.4.33.tar.gz                  squid-4.1.tar.gz
inotify-tools-3.14.tar.gz            squirrelmail-webmail-1.4.22.tar.gz
john-1.8.0.tar.gz                    xy.jpg
john,mimapojie                       ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz
libevent-2.1.8-stable.tar.gz         ZendOptimizer-3.3.0a-linux-glibc21-i386.tar.gz
LNMP
[root@localhost ~] tar xf libevent-2.1.8-stable.tar.gz -C /opt/
[root@localhost libevent-2.1.8-stable] ./configure -prefix/-s/loca//ibeve
[root@localhost libevent-2.1.8-stable] make && make install

1.2安装Memccached
采用源代码的方式进行Memcached的编译安装,安装时指定Libevent的安装路径

[root@localhost ~] tar xf memcached-1.5.6.tar.gz -C /opt/
[root@localhost libevent-2.1.8-stable]# cd /opt/memcached-1.5.6/
[root@localhost memcached-1.5.6]# ./configure \
> --prefix=/usr/local/memcached \
> --with-libevent=/usr/local/libevent/
[root@localhost ~] make && make install

#将Memcached自带的命令建立一个软连接让系统能够识别
[root@localhost memcached-1.5.6] ln -s /usr/local/memcached/bin/* /usr/local/bin/

#其中启动Memcached时,-d选项的作用是以守护进程的方式运行Memcached服务;-m是为Memcached分配为32MB的内存,应根据企业需要进行调整: -u 指定运行的用户账号
[root@localhost memcached-1.5.6] memcached -d -m 32m -p 11211 -u root

查看端口进程有没有开启

[root@localhost memcached-1.5.6] netstat -ntap | grep memc
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      21394/memcached     
tcp6       0      0 :::11211                :::*                    LISTEN      21394/memcached  

#关闭防火墙
[root@localhost ~] systemctl stop firewalld.service
[root@localhost ~] setenforce 0

2.1 Memcached API客户端

为了使得程序可以直接调用Memcached库和接口,
可以使用Memcached扩展组
件将Memcached添加为PHP的一个模块。此扩展使用了Libmemcached库提供的API与Memcached服务端进行交互

安装autoconf

[root@localhost ~]# yum install autoconf -y

解压Memcache安装包

[root@localhost ~]# tar zxvf /abc/memcache-2.2.7.tgz -C /opt/
package.xml
memcache-2.2.7/config.m4
memcache-2.2.7/config9.m4
memcache-2.2.7/config.w32
memcache-2.2.7/CREDITS
memcache-2.2.7/example.php
memcache-2.2.7/memcache.c
memcache-2.2.7/memcache_queue.c
memcache-2.2.7/memcache_session.c
memcache-2.2.7/memcache_standard_hash.c
memcache-2.2.7/memcache_consistent_hash.c
memcache-2.2.7/memcache.dsp
memcache-2.2.7/php_memcache.h
memcache-2.2.7/memcache_queue.h
memcache-2.2.7/README
memcache-2.2.7/memcache.php

#进入Memcache文件
[root@localhost ~] cd /opt/memcache-2.2.7/

#我们想编译安装的话要用到configure但是Memcache客户端文件里面没有configure配置文件所以就要用到php里面的configure配置模块
[root@localhost memcache-2.2.7] ls
acinclude.m4    config.h.in   CREDITS          memcache_consistent_hash.c  memcache_standard_hash.c
aclocal.m4      config.m4     example.php      memcache.dsp                missing
autom4te.cache  config.sub    install-sh       memcache.php                mkinstalldirs
build           configure     ltmain.sh        memcache_queue.c            php_memcache.h
config9.m4      configure.in  Makefile.global  memcache_queue.h            README

#直接运行php里面bin文件里面phpize配置
[root@localhost memcache-2.2.7]# /usr/local/php/bin/phpize 
Configuring for:
PHP Api Version:         20121113
Zend Module Api No:      20121212
Zend Extension Api No:   220121212
[root@localhost memcache-2.2.7]# ls
acinclude.m4    config.h.in   CREDITS          memcache_consistent_hash.c  memcache_standard_hash.c
aclocal.m4      config.m4     example.php      memcache.dsp                missing
autom4te.cache  config.sub    install-sh       memcache.php                mkinstalldirs
build           configure     ltmain.sh        memcache_queue.c            php_memcache.h
config9.m4      configure.in  Makefile.global  memcache_queue.h            README
config.guess    config.w32    memcache.c       memcache_session.c          run-tests.php

#再编译运行configure
[root@localhost memcache-2.2.7]# ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config
[root@localhost memcache-2.2.7]# make && make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/
#make install运行后会有一段路径显示出来如上记住如上的路径

进入php底下php.ini配置文件,搜索并修改下面一行,再新增一行(如果没有就在解压包下执行cp php.ini-development /usr/local/php5/php.ini

ln -s /usr/local/php5/bin/* /usr/local/bin/

ln -s /usr/local/php5/sbin/* /usr/local/sbin/)

用客户端连接测试

vi /usr/local/httpd/htdocs/index.php //编写测试页面,测试memcached工作是否正常

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

http://192.168.32.152/index.php //输入客户端地址测试是否成功

猜你喜欢

转载自blog.51cto.com/13645280/2152415
今日推荐