操作系統(tǒng):CentOS release 6.8 (Final)
成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比北鎮(zhèn)網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式北鎮(zhèn)網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋北鎮(zhèn)地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。虛擬機(jī):VM
任務(wù):服務(wù)與客戶端在一臺(tái)虛擬機(jī)主安裝和配置。
Memcached 是一個(gè)高性能的分布式內(nèi)存對(duì)象緩存系統(tǒng),用于動(dòng)態(tài)Web應(yīng)用以減輕數(shù)據(jù)庫(kù)負(fù)載。它通過(guò)在內(nèi)存中緩存數(shù)據(jù)和對(duì)象來(lái)減少讀取數(shù)據(jù)庫(kù)的次數(shù),從而提高動(dòng)態(tài)、數(shù)據(jù)庫(kù)驅(qū)動(dòng)網(wǎng)站的速度。
Memcached服務(wù)安裝
[root@localhost ~]# yum install libevent libevent-devel nc –y
[root@localhost ~]# rpm -qa libevent libevent-devel nc
libevent-1.4.13-4.el6.x86_64
libevent-devel-1.4.13-4.el6.x86_64
nc-1.84-24.el6.x86_64
#可以通過(guò)yum安裝或編譯安裝,yum安裝的版本比較低,可以從官網(wǎng)上下載安裝下載最新版本編譯安裝http://memcached.org/downloads或者h(yuǎn)ttp://pecl.php.net/package/memcached
[root@localhost ~]# yum install memcached –y
[root@localhost ~]# which memcached
/usr/bin/memcached
[root@localhost ~]# memcached -m 16m -p 11211 -d -u root -c 8192 #啟動(dòng)第一個(gè)實(shí)例
[root@localhost ~]# lsof -i:11211 #查看啟動(dòng)情況
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
memcached 1857 root 26u IPv4 14225 0t0 TCP *:memcache (LISTEN)
memcached 1857 root 27u IPv6 14226 0t0 TCP *:memcache (LISTEN)
memcached 1857 root 28u IPv4 14229 0t0 UDP *:memcache
memcached 1857 root 29u IPv6 14230 0t0 UDP *:memcache
[root@localhost ~]# ps -ef|grep memcached|grep -v grep #查看進(jìn)程
root 1857 1 0 19:14 ? 00:00:00 memcached -m 16m -p 11211 -d -u root -c 8192
[root@localhost ~]# memcached -m 16m -p 11212 -d -u root -c 8192 #啟動(dòng)第二個(gè)實(shí)例
[root@localhost ~]# ps -ef|grep memcached|grep -v grep
root 1857 1 0 19:14 ? 00:00:00 memcached -m 16m -p 11211 -d -u root -c 8192
root 1868 1 0 19:15 ? 00:00:00 memcached -m 16m -p 11212 -d -u root -c 8192
[root@localhost ~]# lsof -i :11211
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
memcached 1737 root 26u IPv4 12689 0t0 TCP *:memcache (LISTEN)
memcached 1737 root 27u IPv6 12690 0t0 TCP *:memcache (LISTEN)
memcached 1737 root 28u IPv4 12693 0t0 UDP *:memcache
memcached 1737 root 29u IPv6 12694 0t0 UDP *:memcache
[root@localhost ~]# lsof -i :11212
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
memcached 1739 root 26u IPv4 12698 0t0 TCP *:11212 (LISTEN)
memcached 1739 root 27u IPv6 12699 0t0 TCP *:11212 (LISTEN)
memcached 1739 root 28u IPv4 12702 0t0 UDP *:11212
memcached 1739 root 29u IPv6 12703 0t0 UDP *:11212
[root@localhost ~]# tail -2 /etc/rc.local
[root@localhost ~]# vi /etc/rc.local #開(kāi)機(jī)可以自動(dòng)啟動(dòng)兩個(gè)實(shí)例
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
memcached -m 16m -p 11211 -d -u root -c 8192
memcached -m 16m -p 11212 -d -u root -c 8192
------------------------------------------------------------------------
memcached參數(shù)說(shuō)明:
-d選項(xiàng)是啟動(dòng)一個(gè)守護(hù)進(jìn)程
-m是分配給Memcache使用的內(nèi)存數(shù)量,單位是MB,我這里是16MB
-u是運(yùn)行Memcache的用戶,我這里是root
-l是監(jiān)聽(tīng)的服務(wù)器IP地址
-p是設(shè)置Memcache監(jiān)聽(tīng)的端口,最好是1024以上的端口
-c選項(xiàng)是大運(yùn)行的并發(fā)連接數(shù),默認(rèn)是1024,按照你服務(wù)器的負(fù)載量來(lái)設(shè)定
-P是設(shè)置保存Memcache的pid文件
其他參數(shù)省略說(shuō)明。
[root@localhost ~]# chkconfig --level 2345 memcached on #開(kāi)機(jī)啟動(dòng)服務(wù)
#向memcached中寫(xiě)入數(shù)據(jù)
[root@localhost ~]# printf "set key1 0 0 6\r\n skyboy\r\n"|nc 127.0.0.1 11211
CLIENT_ERROR bad data chunk
ERROR
[root@localhost ~]# printf "set key1 0 0 6\r\nskyboy\r\n"|nc 127.0.0.1 11211
STORED
[root@localhost ~]# printf "set key2 0 0 5\r\nskyboy\r\n"|nc 127.0.0.1 11211
CLIENT_ERROR bad data chunk
ERROR
[root@localhost ~]# printf "get key1\r\n"|nc 127.0.0.1 11211
VALUE key1 0 6
skyboy
END
[root@localhost ~]# printf "delete key1\r\n"|nc 127.0.0.1 11211
DELETED
[root@localhost ~]# printf "get key1\r\n"|nc 127.0.0.1 11211
END
通過(guò)telnet命令寫(xiě)入數(shù)據(jù)
Connection closed by foreign host.
[root@localhost ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set user01 0 0 7
oldgirl
STORED
get user01
VALUE user01 0 7
oldgirl
END
delete user01
DELETED
get user01
END
quit
Connection closed by foreign host.
安裝Memcached客戶端
http://pecl.php.net/package/memcache#下載所有版本地址
[root@localhost tools]#wgethttp://pecl.php.net/get/memcache-2.2.4.tgz
[root@localhost tools]# tar zxf memcache-2.2.4.tgz
[root@localhost tools]# cd memcache-2.2.4
[root@localhost memcache-2.2.4]#
#phpize是用來(lái)擴(kuò)展PHP擴(kuò)展模塊的,通過(guò)phpize可以建立PHP的外掛模塊。
[root@localhost memcache-2.2.4]# /application/php/bin/phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
[root@localhost memcache-2.2.4]# ./configure --enable-memcache \
--with-php-config=/application/php/bin/php-config
[root@localhost memcache-2.2.4]# make
[root@localhost memcache-2.2.4]# make install
Installing shared extensions: /application/php5.3.27/lib/php/extensions/no-debug-non-zts-20090626/
[root@localhost memcache-2.2.4]# ls /application/php/lib/php/extensions/no-debug-non-zts-20090626/
memcache.so #生成了.so模塊
[root@localhost memcache-2.2.4]#cd /application/php/lib/
[root@localhost lib]# vi php.ini #在末尾添加以下兩行
extension_dir="/application/php/lib/php/extensions/no-debug-non-zts-20090626/"
extension=memcache.so
[root@localhost lib]# /application/php/sbin/php-fpm -t #檢查語(yǔ)法
[19-Jan-2017 19:50:52] NOTICE: configuration file /application/php5.3.27/etc/php-fpm.conf test is successful
[root@localhost lib]# /application/php/sbin/php-fpm #啟動(dòng)服務(wù)
[root@localhost lib]# ps -ef|grep php-fpm|grep -v grep
root 4624 1 0 19:51 ? 00:00:00 php-fpm: master process (/application/php5.3.27/etc/php-fpm.conf)
nginx 4625 4624 0 19:51 ? 00:00:00 php-fpm: pool www
nginx 4626 4624 0 19:51 ? 00:00:00 php-fpm: pool www
重啟fpm
[root@localhost ~]# pkill php-fpm
[root@localhost ~]# ps -ef|grep php-fpm|grep -v grep
[root@localhost ~]# /application/php/sbin/php-fpm
[root@localhost ~]# ps -ef|grep php-fpm|grep -v grep
root 2186 1 0 23:39 ? 00:00:00 php-fpm: master process (/application/php5.3.27/etc/php-fpm.conf)
nginx 2187 2186 0 23:39 ? 00:00:00 php-fpm: pool www
nginx 2188 2186 0 23:39 ? 00:00:00 php-fpm: pool www
測(cè)試Memcached客戶端安裝是否成功?http://192.168.222.130/phpinfo.php
Phpinfo.php文件內(nèi)容如下:
Phpinfo();
?>
測(cè)試LNMP環(huán)境是否連接Memcached服務(wù)成功?
[root@localhost www]# vi op_mem.php
$memcache=new memcache;
$memcache->connect('192.168.222.130',11211) or die ("Could not connect Mc Server");
$memcache->set('key','學(xué)習(xí)Memcached');
$get=$memcache->get('key');
echo $get;
?>
[root@localhost www]# /application/php/bin/php op_mem.php
學(xué)習(xí)Memcached
或者瀏覽器訪問(wèn):http://192.168.222.130/op_mem.php
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。