緩存數據庫Memcache

第1章 緩存數據庫Memcache

1.1 爲何用緩存數據庫

image.png 

1.2 Memcached介紹

image.png 

1.3 Memcached在企業中使用場景

1.3.1 應用場景一

image.png

1.3.2 應用場景二

image.png 

1.4 CookiesSession

image.png 

1.5 Memcached分佈式緩存集羣

image.png

1.5.1 普通哈希算法

image.png

1.5.2 一致性哈希算法

image.png

第2章 安裝Memcached

2.1 服務端配置(Memcached

服務端環境php

[root@cache01~]# cat /etc/redhat-release html

CentOS Linux release 7.2.1511 (Core) nginx

[root@cache01~]# uname -rweb

3.10.0-327.el7.x86_64redis

[root@cache01~]# getenforce 算法

Disabled數據庫

[root@cache01~]# systemctl status firewalld.service vim

firewalld.service - firewalld - dynamic firewall daemon瀏覽器

   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)緩存

   Active: inactive (dead)

[root@cache01~]# hostname -I

10.0.0.21 172.16.1.21

 

Memcache用到了libevent這個庫用於Socket的處理

yum install libevent libevent-devel nc -y


安裝Memcached

yum -y install memcached


查看配置文件

cat /etc/sysconfig/memcached

 

[root@cache01 ~]# cat /etc/sysconfig/memcached

PORT="11211"

USER="memcached"                        

MAXCONN="1024"                        默認最大併發1024

CACHESIZE="64"                         內存用於以MB爲單位的項目(默認爲64 MB)

OPTIONS=""

查看啓動文件

cat /usr/lib/systemd/system/memcached.service

 

啓動服務

systemctl start memcached.service

 

[root@cache01 ~]# ss -lntup |grep 11211

udp    UNCONN     0      0         *:11211                 *:*                   users:(("memcached",pid=15119,fd=28))

udp    UNCONN     0      0        :::11211                :::*                   users:(("memcached",pid=15119,fd=29))

tcp    LISTEN     0      128       *:11211                 *:*                   users:(("memcached",pid=15119,fd=26))

tcp    LISTEN     0      128      :::11211                :::*                   users:(("memcached",pid=15119,fd=27))

 

注:memcached能夠同時啓動多個實例,端口不一致便可。

memcached -m 16m -p 11212 -d -u root -c 8192

參數說明:

- m           max內存用於以MB爲單位的項目(默認爲64 MB)

- p            監聽TCP端口號(默認:11211)

- d            做爲守護進程運行

- u            假設<用戶名>的身份(只有在做爲根運行時)

- c            最大併發鏈接(默認:1024)

2.2 Memcached使用

memcached存儲方式:

key   <->   value

   name  <->   wuhaung

寫入數據

printf "set key008 0 0 10\r\nwuhuang987\r\n"|nc 10.0.0.21 11211

[root@cache01 ~]# printf "set key008 0 5 10\r\nwuhuang987\r\n"|nc 10.0.0.21 11211

STORED

讀取數據

printf "get key008\r\n"|nc 10.0.0.21 11211

[root@cache01 ~]# printf "get key008\r\n"|nc 10.0.0.21 11211

VALUE key008 0 10

wuhuang987

END

刪除數據

printf "delete key008\r\n"|nc 10.0.0.21 11211

 

image.png 

2.3 客戶端部署(web服務器Memcache

web01wordpress準備好(能夠訪問),接下來在這臺機器安裝memcache客戶端。

安裝PHP memcache 擴展插件

cd /server/tools

wget http://pecl.php.net/get/memcache-2.2.5.tgz

tar zxvf memcache-2.2.5.tgz

cd memcache-2.2.5

/application/php/bin/phpize

./configure --enable-memcache --with-php-config=/application/php/bin/php-config --with-zlib-dir

make && make install

 

查看是否安裝成功

[root@web01 memcache-2.2.5]# ls /application/php-5.5.32/lib/php/extensions/no-debug-non-zts-20121212/

memcache.so                                <--- memcache.so表示插件安裝成功

 

配置memcache客戶端使其生效

sed -i '$a extension=memcache.so' /application/php/lib/php.ini

 

檢測語法,重啓php服務

pkill php

/application/php/sbin/php-fpm -t

/application/php/sbin/php-fpm

/application/php/bin/php -m|grep memcache          

參數說明:-m查看PHP支持哪些模塊

 

編寫測試Memcache文件(PHP代碼測試)

[root@web01 blog]# cat >>/application/nginx/html/blog/mc.php<<'EOF'

<?php

$memcache = new Memcache;

$memcache->connect('10.0.0.21', 11211) or die ("Could not connect");

$memcache->set('key20180314', 'hello,world');

$get_value = $memcache->get('key20180314');

echo $get_value;

?>

EOF

服務端驗證成功

[root@cache01 ~]# printf "get key20180314\r\n"|nc 10.0.0.21 11211

VALUE key20180314 0 11

hello,world

END

2.4 web管理Memcached

2.4.1 配置web管理Memcached

下載pip網站程序

tar xf memadmin-1.0.12.tar.gz -C /application/nginx/html/blog/

瀏覽器訪問http://blog.etiantian.org/memadmin

 

image.png 

image.png 

image.pngimage.png 

2.4.2 使用Memcache緩存WordPress博文數據

WordPress會自動檢查在wp-content目錄下是否有object-cache.php文件,若是有,直接調用它做爲WordPress對象緩存機制。

注意:object-cache.php此類文件是由開發完成,並不是運維的工做,其餘的網站作緩存也是由開發寫程序。

2.4.3 Memcached Session共享

方法1

經過程序實現,web01只須要往memcahcesessionweb02memcahcesession,看成普通數據讀寫(更具備通用性)

方法2

經過php的配置文件,php默認將session存儲在文件中,修改成存儲在memcached

使用這個功能,須要使用phpsession函數

修改php配置(設置session共享)

sed -i 's#session.save_handler = files#session.save_handler = memcache#;$a session.save_path = "tcp://10.0.0.21:11211"' /application/php/lib/php.ini

 

原配置:

1 session.save_handler = files

2 session.save_path = "/tmp"

 

修改成:

[root@web01 ~]# vim /application/php/lib/php.ini

1 session.save_handler = memcache

2 session.save_path = "tcp://10.0.0.21:11211"

 

修改完成以後要重啓php服務

pkill php

/application/php/sbin/php-fpm -t

/application/php/sbin/php-fpm

2.5 Memcached在集羣中session共享存儲的優缺點

2.5.1 優勢

1)讀寫速度上會比普通文件files速度快不少。

2)能夠解決多個服務器共用session的難題。

2.5.2 缺點

1session數據都保存在memory中,持久化方面有所欠缺,但對session數據來講不是問題。

2)通常是單臺,若是部署多臺,多臺之間數據沒法同步。經過hash算法分配依然有session丟失的問題。

2.5.3 替代方案

1)能夠用其餘的持久化系統存儲session,例如redisttserver來替代memcached.

2)高性能併發場景,cookies效率比session要好不少,所以,大網站都會用cookies解決會話共享的問題.

3)一些很差的方法:lvs-p,nginx  ip_hash,不推薦使用.

2.6 DedeCMS使用memcache問題

2.6.1 問題

配置session共享後訪問不了DedeCMS後臺。

緣由:bbsblogsession是存在數據庫的表中,而DedeCMSsession是存在一個目錄下的文件中。

2.6.2 解決方法

運維的工做:準備環境

PHP默認把session存放在數據庫(或緩存中),而不是存放在文件中

 

開發的工做:用環境

修改文件一:

[root@web01 include]# pwd

/application/nginx/html/www/include

[root@web01 include]# vim common.inc.php

135 //Session保存路徑

136 $enkey = substr(md5(substr($cfg_cookie_encode,0,5)),0,10);

137 //$sessSavePath = DEDEDATA."/sessions_{$enkey}";

138 $sessSavePath = "tcp://10.0.0.21:11211";

139 if ( !is_dir($sessSavePath) ) mkdir($sessSavePath);

 

修改文件二:

[root@web01 include]# vim vdimgck.php

24 $enkey = substr(md5(substr($cfg_cookie_encode,0,5)),0,10);

25 //$sessSavePath = DEDEDATA."/sessions_{$enkey}";

26 $sessSavePath = "tcp://10.0.0.21:11211";

27 if ( !is_dir($sessSavePath) ) mkdir($sessSavePath);


DedeCMS直接使用memcache的共享,解決問題(即便用memcecha緩存DedeCMS的數據)

相關文章
相關標籤/搜索