您好,登錄后才能下訂單哦!
操作系統:CentOS release 6.8 (Final)
虛擬機:VM
任務:服務與客戶端在一臺虛擬機主安裝和配置。
Memcached 是一個高性能的分布式內存對象緩存系統,用于動態Web應用以減輕數據庫負載。它通過在內存中緩存數據和對象來減少讀取數據庫的次數,從而提高動態、數據庫驅動網站的速度。
Memcached服務安裝
[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
#可以通過yum安裝或編譯安裝,yum安裝的版本比較低,可以從官網上下載安裝下載最新版本編譯安裝http://memcached.org/downloads或者http://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 #啟動第一個實例
[root@localhost ~]# lsof -i:11211 #查看啟動情況
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 #查看進程
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 #啟動第二個實例
[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 #開機可以自動啟動兩個實例
#!/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參數說明:
-d選項是啟動一個守護進程
-m是分配給Memcache使用的內存數量,單位是MB,我這里是16MB
-u是運行Memcache的用戶,我這里是root
-l是監聽的服務器IP地址
-p是設置Memcache監聽的端口,最好是1024以上的端口
-c選項是最大運行的并發連接數,默認是1024,按照你服務器的負載量來設定
-P是設置保存Memcache的pid文件
其他參數省略說明。
[root@localhost ~]# chkconfig --level 2345 memcached on #開機啟動服務
#向memcached中寫入數據
[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
通過telnet命令寫入數據
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]#wget http://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是用來擴展PHP擴展模塊的,通過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 #檢查語法
[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 #啟動服務
[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
測試Memcached客戶端安裝是否成功?http://192.168.222.130/phpinfo.php
Phpinfo.php文件內容如下:
<?php
Phpinfo();
?>
測試LNMP環境是否連接Memcached服務成功?
[root@localhost www]# vi op_mem.php
<?php
$memcache=new memcache;
$memcache->connect('192.168.222.130',11211) or die ("Could not connect Mc Server");
$memcache->set('key','學習Memcached');
$get=$memcache->get('key');
echo $get;
?>
[root@localhost www]# /application/php/bin/php op_mem.php
學習Memcached
或者瀏覽器訪問:http://192.168.222.130/op_mem.php
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。