標籤:nginx+php+memcached+mysql
1、nginx與memcached整合
#安裝memcached支援的事務庫libevent
wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gztar zxf libevent-2.0.22-stable.tar.gz cd libevent-2.0.22-stable./configure --prefix=/usr/local/libeventmake && make installecho $?cd ..
#接下來安裝memcached:
wget http://www.memcached.org/files/memcached-1.4.35.tar.gztar zxf memcached-1.4.35.tar.gzcd memcached-1.4.35./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libeventmake && make installecho $?cd ..
#運行memcached
[[email protected] ~]# /usr/local/memcached/bin/memcached -d -u nobody -vv
#配置nginx設定檔nginx.conf,定位user的uri交給memcached做緩衝
#添加location定位:
location ~* user { set $memcached_key "$uri"; #設定memcached的key為uri memcached_pass 192.168.146.132:11211; #連結memcached error_page 404 /callback.php; #錯誤定位 }
#載入nginx配置
nginx -s reload
#在memcached中寫入一條URI資料,測試整合是否成功
[[email protected] ~]# telnet 192.168.146.132 11211Trying 192.168.146.132...Connected to 192.168.146.132.Escape character is ‘^]‘.add /user1.html 0 0 7iamlisi STOREDquit
#訪問http://192.168.146.132/user1.html如能看到iamlisi,那麼nginx與memcache整合成功了!
2、整合PHP與memcahced
#安裝PHP擴充模組memcache
wget http://pecl.php.net/get/memcache-2.2.7.tgztar zxvf memcache-2.2.7.tgzcd memcache-2.2.7/usr/local/php/bin/phpize ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dirmake && make installecho $?cd ..
pkill php-fpm #殺死php-fpm進程
php-fpm #啟動php-fpm
http://192.168.146.132/test.php能看到memcache模組就成功了
#測試PHP與memcached
vim /usr/local/nginx/html/callback.php<?phpprint_r($_SERVER);?>php
#訪問http://1982.168.146.132/user2.html 此uri在memcached中不存在,就會回調到callback.php處理請求,結果如下:
Array( [USER] => nobody [HOME] => / [FCGI_ROLE] => RESPONDER [SCRIPT_FILENAME] => /usr/local/nginx/html/callback.php [QUERY_STRING] => [REQUEST_METHOD] => GET [CONTENT_TYPE] => [CONTENT_LENGTH] => [SCRIPT_NAME] => /callback.php [REQUEST_URI] => /user2.html [DOCUMENT_URI] => /callback.php [DOCUMENT_ROOT] => /usr/local/nginx/html [SERVER_PROTOCOL] => HTTP/1.1 [REQUEST_SCHEME] => http [GATEWAY_INTERFACE] => CGI/1.1 [SERVER_SOFTWARE] => nginx/1.12.1 [REMOTE_ADDR] => 192.168.146.1 [REMOTE_PORT] => 14187 [SERVER_ADDR] => 192.168.146.132 [SERVER_PORT] => 80 [SERVER_NAME] => localhost [REDIRECT_STATUS] => 200 [HTTP_HOST] => 192.168.146.132 [HTTP_CONNECTION] => keep-alive [HTTP_UPGRADE_INSECURE_REQUESTS] => 1 [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36 [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 [HTTP_ACCEPT_ENCODING] => gzip, deflate [HTTP_ACCEPT_LANGUAGE] => zh-CN,zh;q=0.8 [PHP_SELF] => /callback.php [REQUEST_TIME] => 1503552110)
3、使用PHP讓memcached連結MySQL查詢key與value並存入memcached,
#接下來我們寫個PHP程式,連結資料庫,讓memcached找不到的uri就回調給PHP,然後PHP去連結資料庫,尋找資料後給memcached:
cat html/callback.php<?php//print_r($_SERVER);//擷取UID用來做key$uri = $_SERVER[‘REQUEST_URI‘];//分析出uir中的uid號$uid = substr($uri,5,strpos($uri,‘.‘)-5);//echo $uid;//連結資料庫,查詢並寫入memcached$conn = mysql_connect(‘localhost‘,‘root‘,‘123.com‘);$sql = ‘use test‘;mysql_query($sql,$conn);$sql = ‘set names utf8‘;mysql_query($sql,$conn);$sql = ‘select * from user where uid=‘.$uid;$rs = mysql_query($sql,$conn);echo ‘from mysql query<br />‘;$user = mysql_fetch_assoc($rs);if (empty($user)) { echo ‘no this user‘;} else {// print_r($user); $html = ‘<h1>‘. $user[‘uname‘].‘</h1>‘; echo $html; $mem = new memcache(); $mem->connect(‘192.168.146.132‘,11211); $mem->add($uri,$html,0,300); $mem->close();}
#此處我們要在資料庫中建立test庫和user表,並寫入uid和uname欄位資料:
#注意:callback.php檔案調用的庫和表,欄位要對應建立
mysql> create database test;mysql> use test;mysql> create table user( -> uid int(11) not null, -> uname varchar(255) not null, -> primary key (uid));mysql> insert into user (uid,uname) values(1,‘i am memcached‘);mysql> insert into user (uid,uname) values(2,‘dslajfflsaj;gljdaslgjlajfdalsjf‘);
#此時訪問uir,首先會轉寄到memcached處理,memcached中沒有key就會回調給callback.php處理,然後PHP連結資料庫,查詢uri資料,然後memcached會寫入key與value,並將資料返回給用戶端。
http://192.168.146.132/user1.html
#memcached顯示資料動態:
<36 new auto-negotiating client connection36: Client using the ascii protocol<36 add /user1.html 1 300 122>36 STORED<36 connection closed.<36 new auto-negotiating client connection36: Client using the ascii protocol<36 get /user1.html>36 sending key /user1.html>36 END<36 connection closed.
4、配置memcached群架,nginx和PHP使用一致性雜湊(ip-hash)
#當使用memcached群集時,會遇到資料寫入memcached不一致,因為nginx模式使用round-robin(輪詢)方式訪問memcached伺服器,就會造成寫入和讀出資料不在同一伺服器的問題,為此nginx提供了ip-hash基於Ip的一致性雜湊演算法,它會記錄訪問IP,下次訪問時同一ip會訪問上次記錄的伺服器。
官方參考文檔:https://www.nginx.com/resources/wiki/modules/consistent_hash/
#下載nginx hash模組並安裝:
cd /root/toolswget https://github.com/replay/ngx_http_consistent_hash/archive/master.zipunzip master.zip
#查看nginx編譯參數
[[email protected] tools]# nginx -Vnginx version: nginx/1.8.1built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013TLS SNI support enabledconfigure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_ssl_module --add-module=/root/tools/nginx-1.8.1/nginx-rtmp-module
#進入nginx源碼目錄,添加模組安裝
[[email protected] tools]# cd nginx-1.8.1[[email protected] nginx-1.8.1]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_ssl_module --add-module=/root/tools/nginx-1.8.1/nginx-rtmp-module --add-module=/root/tools/ngx_http_consistent_hash-master/[[email protected] nginx-1.8.1]#make && make install
#類比memcache叢集,區分連接埠啟動三個memcached
memcached -d -u nobody -p 11211 -vvmemcached -d -u nobody -p 11212 -vvmemcached -d -u nobody -p 11213 -vv
#nginx設定檔http段添加upstram模組:
upstream mcserver { consistent_hash $request_uri; #指定使用雜湊演算法,針對memcached,請參考文檔 server 192.168.146.132:11211; server 192.168.146.132:11212; server 192.168.146.132:11213; }
#並在location中反向 Proxy到mcserver叢集:
具體參考文檔:http://nginx.org/en/docs/http/ngx_http_memcached_module.html#memcached_pass
location ~* /user { set $memcached_key "$uri"; #將uri配置為memcached的key memcached_pass mcserver; #代理到memcached叢集 error_page 404 = /callback.php; #memcached中找不到key將回調到此檔案 }
#重新載入nginx配置
nginx -s reload
#修改php配置為hash查詢
#具體參考文擋:http://cn2.php.net/manual/en/memcache.ini.php#ini.memcache.hash-strategy
vim /usr/local/php/etc/php.inimemcache.hash_strategy=consistent #添加,使用一致性雜湊
#重新啟動php-fpm
pkill -9 php-fpmnetstat -lntup|grep 9000php-fpm
#修改回調代碼,建立memcached叢集連結
cat html/callback.php<?php//print_r($_SERVER);//擷取UID用來做key$uri = $_SERVER[‘REQUEST_URI‘];//分析出uir中的uid號$uid = substr($uri,5,strpos($uri,‘.‘)-5);//建立memcached叢集連結$mem = new memcache();$mem->addServer(‘192.168.146.132‘,11211);$mem->addServer(‘192.168.146.132‘,11212);$mem->addServer(‘192.168.146.132‘,11213);//連結資料庫,查詢並寫入memcached$conn = mysql_connect(‘localhost‘,‘root‘,‘123.com‘);$sql = ‘use test‘;mysql_query($sql,$conn);$sql = ‘set names utf8‘;mysql_query($sql,$conn);$sql = ‘select * from user where uid=‘.$uid;$rs = mysql_query($sql,$conn);echo ‘from mysql query<br />‘;$user = mysql_fetch_assoc($rs);if (empty($user)) { echo ‘no this user‘;} else { $html = ‘<h1>‘. $user[‘uname‘].‘</h1>‘; echo $html; $mem->add($uri,$html,0,10); $mem->close();}
#測試資料,memcached會在一個連接埠寫入資料和查詢資料:
<36 new auto-negotiating client connection36: Client using the ascii protocol<36 get /user1.html>36 END<36 connection closed.<36 new auto-negotiating client connection36: Client using the ascii protocol<36 add /user1.html 0 10 87>36 STORED<37 new auto-negotiating client connection37: Client using the ascii protocol<37 get /user1.html>37 END<37 connection closed.<40 new auto-negotiating client connection40: Client using the ascii protocol<40 get /user2.html>40 END<40 connection closed.<40 new auto-negotiating client connection40: Client using the ascii protocol<40 add /user2.html 0 10 40>40 STORED<41 new auto-negotiating client connection41: Client using the ascii protocol<41 get /user2.html>41 END<41 connection closed.
#到此nginx+PHP+memcached+MySQL並現象memcached群架一致性雜湊演算法完成!
#途中遇到一個問題:在MySQL中寫入中文資料,php調用後第一次顯示正常,第二次存入memcached後調用就亂碼了,我用Google和Firefox瀏覽器都是亂碼,而用360和IE則沒有亂碼!暫時沒找到原因,有知道的忘告知,萬分感謝!!!
本文出自 “80後小菜鳥” 部落格,請務必保留此出處http://zhangxinqi.blog.51cto.com/9668428/1961992
nginx+PHP+memcached+MySQL+ip-hash做memcached叢集