1. 程式人生 > >高性能內存對象緩存Memcached

高性能內存對象緩存Memcached

配置 ini elm dex 如果 gre 沒有 ats locale

高性能內存對象緩存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 //輸入客戶端地址測試是否成功

高性能內存對象緩存Memcached