您好,登錄后才能下訂單哦!
1、nginx與memcached整合
#安裝memcached支持的事務庫libevent
wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz tar zxf libevent-2.0.22-stable.tar.gz cd libevent-2.0.22-stable ./configure --prefix=/usr/local/libevent make && make install echo $? cd ..
#接下來安裝memcached:
wget http://www.memcached.org/files/memcached-1.4.35.tar.gz tar zxf memcached-1.4.35.tar.gz cd memcached-1.4.35 ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent make && make install echo $? cd ..
#運行memcached
[root@nginx ~]# /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數據,測試整合是否成功
[root@nginx ~]# telnet 192.168.146.132 11211 Trying 192.168.146.132... Connected to 192.168.146.132. Escape character is '^]'. add /user1.html 0 0 7 iamlisi STORED quit
#訪問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.tgz tar zxvf memcache-2.2.7.tgz cd memcache-2.2.7 /usr/local/php/bin/phpize ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir make && make install echo $? 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 <?php print_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,p_w_picpath/webp,p_w_picpath/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 = '<h2>'. $user['uname'].'</h2>'; 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 connection 36: Client using the ascii protocol <36 add /user1.html 1 300 122 >36 STORED <36 connection closed. <36 new auto-negotiating client connection 36: 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/tools wget https://github.com/replay/ngx_http_consistent_hash/archive/master.zip unzip master.zip
#查看nginx編譯參數
[root@LNMP tools]# nginx -V nginx version: nginx/1.8.1 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure 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源碼目錄,添加模塊安裝
[root@LNMP tools]# cd nginx-1.8.1 [root@LNMP 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/ [root@LNMP nginx-1.8.1]#make && make install
#模擬memcache集群,區分端口啟動三個memcached
memcached -d -u nobody -p 11211 -vv memcached -d -u nobody -p 11212 -vv memcached -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中反向代理到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.ini memcache.hash_strategy=consistent #添加,使用一致性哈希
#重新啟動php-fpm
pkill -9 php-fpm netstat -lntup|grep 9000 php-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 = '<h2>'. $user['uname'].'</h2>'; echo $html; $mem->add($uri,$html,0,10); $mem->close(); }
#測試數據,memcached會在一個端口寫入數據和查詢數據:
<36 new auto-negotiating client connection 36: Client using the ascii protocol <36 get /user1.html >36 END <36 connection closed. <36 new auto-negotiating client connection 36: Client using the ascii protocol <36 add /user1.html 0 10 87 >36 STORED <37 new auto-negotiating client connection 37: Client using the ascii protocol <37 get /user1.html >37 END <37 connection closed. <40 new auto-negotiating client connection 40: Client using the ascii protocol <40 get /user2.html >40 END <40 connection closed. <40 new auto-negotiating client connection 40: Client using the ascii protocol <40 add /user2.html 0 10 40 >40 STORED <41 new auto-negotiating client connection 41: 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則沒有亂碼!暫時沒找到原因,有知道的忘告知,萬分感謝!!!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。