1. 程式人生 > >Memcache在linux環境下的部署

Memcache在linux環境下的部署

Memcache用到了libevent這個庫用於Socket的處理,所以還需要安裝libevent,libevent的最新版本是libevent-1.3。(如果你的系統已經安裝了libevent,可以不用安裝)
下載:http://www.monkey.org/~provos/libevent-1.3.tar.gz
下載:http://www.danga.com/memcached/dist/memcached-1.2.2.tar.gz
1.先安裝libevent。這個東西在配置時需要指定一個安裝路徑,即./configure –prefix=/usr/local/libevent/                                                                     (./configure --prefix=/home/admin/edu-cms/cache/libevent );然後make;然後make install;
2.再安裝memcached,只是需要在配置時需要指定libevent的安裝路徑即
./configure  --with-libevent=/usr/local/libevent
(./configure  --prefix=/home/admin/edu-cms/cache/memcached   --with-libevent=/home/admin/edu-cms/cache/libevent)
然後make;然後make install;
這樣就完成了Linux下Memcache伺服器端的安裝。 
如果中間出現報錯,請仔細檢查錯誤資訊,按照錯誤資訊來配置或者增加相應的庫或者路徑。
安裝完成後會memcached的工作目錄是/usr/local/memcached/
 


檢查服務:


1、檢視啟動的memcache服務:
netstat -lp | grep memcached
2、檢視memcache的程序號(根據程序號,可以結束memcache服務:“kill -9 程序號”)
ps -ef | grep memcached 

memcached的基本設定:
1.啟動Memcache的伺服器端:
# /usr/local/memcached/bin/memcached -d -m 10 -u root -l 10.0.0.204 -p 12000 -c 256 -P /tmp/memcached.pid
-d選項是啟動一個守護程序,
-m是分配給Memcache使用的記憶體數量,單位是MB,我這裡是10MB,
-u是執行Memcache的使用者,我這裡是root,
-l是監聽的伺服器IP地址,如果有多個地址的話,我這裡指定了伺服器的IP地址192.168.51.207,
-p是設定Memcache監聽的埠,我這裡設定了12000,最好是1024以上的埠,
-c選項是最大執行的併發連線數,預設是1024,我這裡設定了256,按照你伺服器的負載量來設定,
-P是設定儲存Memcache的pid檔案,我這裡是儲存在 /tmp/memcached.pid,
2.如果要結束Memcache程序,執行:
# kill `cat /tmp/memcached.pid`
也可以啟動多個守護程序,不過埠不能重複。
最後,設定下開始啟動:
#echo "/usr/local/memcached/bin/memcached -d -m 10 -u root -l 10.0.0.204 -p 12000 -c 256 -P /tmp/memcached.pid" >> /etc/rc.local
啟動memcache可能會發生的問題:
/usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file

#一般對於這種依賴的庫找不到的情況,在linux中可以通過設定LD_DEBUG環境變數來獲得更多的資訊
#LD_DEBUG=help ls
Valid options for the LD_DEBUG environment variable are:
  libs       display library search paths
 reloc      display relocation processing
 files      display progress for input file
 symbols    display symbol table processing
 bindings   display information about symbol binding
 versions   display version dependencies
 all        all previous options combined
 statistics display relocation statistics
 unused     determined unused DSOs
 help       display this help message and exit
 To direct the debugging output into a file instead of standard output
a filename can be specified using the LD_DEBUG_OUTPUT environment variable.
#這裡由於是庫檔案依賴有問題,則使用libs引數
#LD_DEBUG=libs /usr/local/memcached/bin/memcached -h                                                            
    13293:    find library=libevent-2.0.so.5 [0]; searching                                                                      
    13293:     search cache=/etc/ld.so.cache
    13293:     search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/l)
    13293:      trying file=/lib/tls/i686/sse2/libevent-2.0.so.5                                                                 
    13293:      trying file=/lib/tls/i686/libevent-2.0.so.5                                                                      
    13293:      trying file=/lib/tls/sse2/libevent-2.0.so.5                                                                      
    13293:      trying file=/lib/tls/libevent-2.0.so.5                                                                           
    13293:      trying file=/lib/i686/sse2/libevent-2.0.so.5                                                                     
    13293:      trying file=/lib/i686/libevent-2.0.so.5                                                                          
    13293:      trying file=/lib/sse2/libevent-2.0.so.5                                                                          
    13293:      trying file=/lib/libevent-2.0.so.5                                                                              
    13293:      trying file=/usr/lib/tls/i686/sse2/libevent-2.0.so.5                                                             
    13293:      trying file=/usr/lib/tls/i686/libevent-2.0.so.5                                                                  
    13293:      trying file=/usr/lib/tls/sse2/libevent-2.0.so.5                                                                  
    13293:      trying file=/usr/lib/tls/libevent-2.0.so.5                                                                       
    13293:      trying file=/usr/lib/i686/sse2/libevent-2.0.so.5                                                                 
    13293:      trying file=/usr/lib/i686/libevent-2.0.so.5                                                                     
    13293:      trying file=/usr/lib/sse2/libevent-2.0.so.5                                                                      
    13293:      trying file=/usr/lib/libevent-2.0.so.5                                                                            
 /usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
#可以看到在載入/usr/lib/libevent-2.0.so.5檔案時出現了問題,先在系統中查詢該檔案
#find /usr/ -name " libevent-2.0.so*"
/usr/local/libevent/lib/libevent-2.0.so.5
/usr/local/libevent/lib/libevent-2.0.so.5.0.1
#通過查詢發現在/usr/local/libevent/lib目錄下有該檔案,通過軟連結方式可以解決
# ln -s /usr/local/libevent/lib/libevent-2.0.so.5 /usr/lib/libevent-2.0.so.5