緩存數據庫Memcache
1.1 為什麽用緩存數據庫
1.2 Memcached介紹
1.3 Memcached在企業中使用場景
1.3.1 應用場景一
1.3.2 應用場景二
1.4 Cookies和Session
1.5 Memcached分布式緩存集群
1.5.1 普通哈希算法
1.5.2 一致性哈希算法
第2章 安裝Memcached
2.1 服務端配置(Memcached)
服務端環境 [root@cache01~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@cache01~]# uname -r 3.10.0-327.el7.x86_64 [root@cache01~]# getenforce Disabled [root@cache01~]# systemctl status firewalld.service ● 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
查看配置文件 cat /etc/sysconfig/memcached
[root@cache01 ~]# cat /etc/sysconfig/memcached PORT="11211" USER="memcached" MAXCONN="1024" 默認最大並發1024 CACHESIZE="64" 內存用於以 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 |
2.3 客戶端部署(web服務器Memcache)
將web01的wordpress準備好(可以訪問),接下來在這臺機器安裝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 |
2.4.2 使用Memcache緩存WordPress博文數據
WordPress會自動檢查在wp-content目錄下是否有object-cache.php文件,如果有,直接調用它作為WordPress對象緩存機制。
註意:object-cache.php此類文件是由開發完成,並非運維的工作,其他的網站做緩存也是由開發寫程序。
2.4.3 Memcached Session共享
方法1
通過程序實現,web01只需要往memcahce寫session,web02從memcahce讀session,當作普通數據讀寫(更具有通用性)
方法2
通過php的配置文件,php默認將session存儲在文件中,修改為存儲在memcached中
使用這個功能,需要使用php的session函數
修改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 缺點
1)session數據都保存在memory中,持久化方面有所欠缺,但對session數據來說不是問題。
2)一般是單臺,如果部署多臺,多臺之間數據無法同步。通過hash算法分配依然有session丟失的問題。
2.5.3 替代方案
1)可以用其他的持久化系統存儲session,例如redis,ttserver來替代memcached.
2)高性能並發場景,cookies效率比session要好很多,因此,大網站都會用cookies解決會話共享的問題.
3)一些不好的方法:lvs-p,nginx ip_hash,不推薦使用.
2.6 DedeCMS使用memcache問題
2.6.1 問題
配置session共享後訪問不了DedeCMS後臺。
原因:bbs和blog的session是存在數據庫的表中,而DedeCMS的session是存在一個目錄下的文件中。
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的數據) |
緩存數據庫Memcache