rhel5 memcache 部署
bbs.sinounix.comRHEL 5 MEMCACHE 部署1. 什么是Memcache 陳建 cjhp1314@hotmail.comMemcached 是高性能的
bbs.sinounix.com
RHEL 5 MEMCACHE 部署
1. 什么是Memcache 陳建 cjhp1314@hotmail.com
Memcached 是高性能的,分布式的內(nèi)存對象緩存系統(tǒng),用于在動態(tài)應(yīng)用中減少數(shù)據(jù)庫負(fù)載,提升訪問速度, 目前全世界不少人使用這個緩存項目來構(gòu)建自己大負(fù)載的網(wǎng)站,來分擔(dān)數(shù)據(jù)庫的壓力。它可以應(yīng)對任意多個連接,使用非阻塞的網(wǎng)絡(luò)IO 。它的工作機(jī)制是在內(nèi)存中開辟一塊空間,然后建立一個HashTable ,Memcached 管理這些HashTable 。
許多Web 應(yīng)用都將數(shù)據(jù)保存到RDBMS 中,應(yīng)用服務(wù)器從中讀取數(shù)據(jù)并在瀏覽器中顯示。但隨著數(shù)據(jù)量的增大、訪問的集中,就會出現(xiàn)RDBMS 的負(fù)擔(dān)加重、數(shù)據(jù)庫響應(yīng)惡化、網(wǎng)站顯示延遲等重大影響。
Memcached 是高性能的分布式內(nèi)存緩存服務(wù)器。一般的使用目的是,通過緩存數(shù)據(jù)庫查詢結(jié)果,減少數(shù)據(jù)庫訪問次數(shù),以提高動態(tài)Web 應(yīng)用的速度、提高可擴(kuò)展性。
2. Memcache 工作原理
首先 memcached 是以守護(hù)程序方式運行于一個或多個服務(wù)器中,隨時接受客戶端的連接操作,客戶端可以由各種語言編寫,目前已知的客戶端 API 包括 Perl/PHP/Python/Ruby/Java/C#/C 等等??蛻舳嗽谂c memcached 服務(wù)建立連接之后,接下來的事情就是存取對象了,每個被存取的對象都有一個唯一的標(biāo)識符 key,存取操作均通過這個 key 進(jìn)行,保存到 memcached 中的對象實際上是放置內(nèi)存中的,并不是保存在 cache 文件中的,這也是為什么 memcached 能夠如此高效快速的原因。注意,這些對象并不是持久的,服務(wù)停止之后,里邊的數(shù)據(jù)就會丟失。
與許多 cache 工具類似,Memcached 的原理并不復(fù)雜。它采用了C/S的模式,在 server 端啟動服務(wù)進(jìn)程,在啟動時可以指定監(jiān)聽的 ip,自己的端口號,所使用的內(nèi)存大小等幾個關(guān)鍵參數(shù)。一旦啟動,服務(wù)就一直處于可用狀態(tài)。Memcached 的目前版本是通過C 實現(xiàn),采用了單進(jìn)程,單線程,異步I/O,基于事件 (event_based) 的服務(wù)方式. 使用 libevent 作為事件通知實現(xiàn)。多個 Server 可以協(xié)同工作,但這些 Server 之間是沒有任何通訊聯(lián)系的,每個 Server 只是對自己的數(shù)據(jù)進(jìn)行管理。Client 端通過指定 Server 端的 ip 地址(通過域名應(yīng)該也可以) 。需要緩存的對象或數(shù)據(jù)是以 key->value 對的形式保存在Server 端。key 的值通過 hash 進(jìn)行轉(zhuǎn)換,根
,bbs.sinounix.com
據(jù) hash 值把 value傳遞到對應(yīng)的具體的某個 Server 上。當(dāng)需要獲取對象數(shù)據(jù)時,也根據(jù) key 進(jìn)行。首先對 key 進(jìn)行 hash,通過獲得的值可以確定它被保存在了哪臺 Server 上,然后再向該 Server 發(fā)出請求。Client 端只需要知道保存 hash(key) 的值在哪臺服務(wù)器上就可以了。
簡單來說,memcache 的工作就是在專門的機(jī)器的內(nèi)存里維護(hù)一張巨大的 hash 表,來存儲經(jīng)常被讀寫的一些數(shù)組與文件,從而極大的提高網(wǎng)站的運行效率。
3. 安裝部署
1. 本機(jī)環(huán)境
SYSTEM RHEL 5.4 32bit
PHP php-5.3.2
NYSQL mysql-5.1.45。
2. 所需軟件包
libevent 事件通訊軟件 (最新版libevent-1.4.14-stable) http://www.monkey.org/~provos/libevent/
memcache php擴(kuò)展組件,memcache 客戶端(最新版memcache-2.2.5) http://pecl.php.net/package/memcache
memcached memcache服務(wù)端(最新版 memcached-1.4.0) http://danga.com/memcached/dist/
3. 安裝
1. 安裝libevent
#tar -zxf libevent-1.4.14a-stable.tar.gz
#cd libevent-1.4.14-stable/
#./configure --prefix=/usr/local/libevent
#make && make install
#系統(tǒng)可能已經(jīng)安裝有rpm 包的libevent ,但建議使用源碼包的libevent
2. 安裝memcached
#tar -zxf memcached-1.4.0.tar.gz
,bbs.sinounix.com
#cd memcached-1.4.0
#./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent #make && make install
#建立memcache 用戶
#useradd –M memcache
#啟動服務(wù)
#memcached -d -m 10 -u memcache -l 192.168.10.3 -p 11211 -c 256 -P /tmp/memcached.pid 參數(shù)說明
-p 指定端口號(默認(rèn)11211)
-m 指定最大使用內(nèi)存大小(默認(rèn)64MB )
-t 線程數(shù)(默認(rèn)4)
-l 連接的IP 地址, 默認(rèn)是本機(jī)
-d 選項是啟動一個守護(hù)進(jìn)程
-d restart|stop重啟|關(guān)閉正在運行的memcached 服務(wù)
-m 最大內(nèi)存使用,單位MB 。默認(rèn)64MB
-M 內(nèi)存耗盡時返回錯誤,而不是刪除項
-c 選項是最大運行的并發(fā)連接數(shù),默認(rèn)是1024,按照你服務(wù)器的負(fù)載量來設(shè)定 -f 塊大小增長因子,默認(rèn)是1.25
-n 最小分配空間,key value flags默認(rèn)是48
-P 是設(shè)置保存Memcache 的pid 文件
設(shè)置開機(jī)自啟動memcache
vi /etc/rc.local
/usr/local/memcached/bin/memcached -d -m 100 -u memcache -l 192.168.10.3 -p 11211 -c 256 -P /tmp/memcached.pid
關(guān)閉 memcache
# kill `cat /tmp/memcached.pid`
3. 安裝memcache php擴(kuò)展模塊
#tar -zxf memcache-2.2.5.tgz
#cd memcache-2.2.5
#find / -name phpize #確定phpize 位置,生成編譯環(huán)境
,bbs.sinounix.com
#/usr/local/php/bin/phpize
#./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
#make && make install
此時你會看到如下提示:
Installing shared
extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
你修改php.ini ,做如下2步操作:
; extension_dir = "./"
將上面這行修改為
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/" 再新增加一行,內(nèi)容如下:
extension=memcache.so
#重啟apache 服務(wù)
#/usr/local/apache-2.15/bin/apachectl restart
測試是否安裝成功
#編輯首頁信息,簡單測試了如下2種情況
vi index.php
phpinfo()
?>
,bbs.sinounix.com
vi index.php
$mem = new Memcache;
$mem->connect("192.168.10.3", 11211)or die ("Could not connect"); $mem->set('key', 'This is a test!', 0, 60);
$val = $mem->get('key');
echo $val;
?>
#未啟用memcache 時
bbs.sinounix.com
#su – memcache
#/usr/local/bin/memcached –d (不加參數(shù)表示以默認(rèn)方式啟動)
bbs.sinounix.com
4. 安裝memcache 小工具
#wget #unzip memcachephp.zip
#mv memcache.php /usr/local/apache-2.15/htdocs/
#vi /usr/local/apache-2.15/htdocs/memcache.php
根據(jù)你的配置修改:
bbs.sinounix.com
bbs.sinounix.com
4. 常見問題:
1. /usr/local/bin/memcached: error while loading shared libraries:
libevent-1.4.so.1: cannot open shared object file: No such file or directory , 缺少庫文件的解決方法。
1.1. 首先 find / -name libevent-1.4.so.1 找到缺少的鏈接文件到底在那兒。
#find / -name libevent-1.4.so.1
/usr/local/src/libevent-1.4.14-stable/.libs/libevent-1.4.so.1
/usr/local/libevent/lib/libevent-1.4.so.1
1.2. 查看出錯原因在哪
#LD_DEBUG=libs /usr/local/bin/memcached -v
10617: find library=libevent-1.4.so.1 [0]; searching 10617: search cache=/etc/ld.so.cache
10617: search
path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib (system search path)
10617: trying
file=/lib/tls/i686/sse2/libevent-1.4.so.1
10617:
trying file=/lib/tls/i686/libevent-1.4.so.1
,bbs.sinounix.com
10617: trying file=/lib/tls/sse2/libevent-1.4.so.1 10617: trying file=/lib/tls/libevent-1.4.so.1
10617: trying file=/lib/i686/sse2/libevent-1.4.so.1 10617: trying file=/lib/i686/libevent-1.4.so.1
10617: trying file=/lib/sse2/libevent-1.4.so.1
10617: trying file=/lib/libevent-1.4.so.1
10617: trying
file=/usr/lib/tls/i686/sse2/libevent-1.4.so.1
10617: trying
file=/usr/lib/tls/i686/libevent-1.4.so.1
10617: trying
file=/usr/lib/tls/sse2/libevent-1.4.so.1
10617: trying file=/usr/lib/tls/libevent-1.4.so.1 10617: trying
file=/usr/lib/i686/sse2/libevent-1.4.so.1
10617: trying file=/usr/lib/i686/libevent-1.4.so.1 10617: trying file=/usr/lib/sse2/libevent-1.4.so.1 10617: trying file=/usr/lib/libevent-1.4.so.1
10617:
/usr/local/bin/memcached: error while loading shared libraries:
libevent-1.4.so.1: cannot open shared object file: No such file or directory
1.3. 通過trying file=/usr/lib/libevent-1.4.so.1得知我們需要做個軟連接到這,因
為這個文件的真實位置在 /usr/local/lib/libevent-1.4.so.2
#ln -s /usr/local/libevent/lib/libevent-1.4.so.1 /usr/lib/libevent-1.4.so.1
1.4. 錯誤消除,類似的問題都可以這樣解決。
5. Memcache 其他相關(guān)資料,轉(zhuǎn)自互聯(lián)網(wǎng),感謝原作者
1. Memcahce 協(xié)議(中英對照)
源自 http://www.gaobo.info/read.php/447.htm
協(xié)議
memcached 的客戶端使用TCP 鏈接 與 服務(wù)器通
)一個運行中的memcached 服務(wù)器監(jiān)視一些(可
器,讀取回應(yīng),最后關(guān)閉連接。 Protocol Clients of memcached communicate with server available; details are below under "UDP protocol.") (configurable) port; clients connect to that port, 訊。(UDP 接口也同樣有效,參考后文的 “UDP協(xié)議”through TCP connections. (A UDP interface is also 設(shè)置)端口??蛻舳诉B接這些端口,發(fā)送命令到服務(wù)A given running memcached server listens on some