Memcached 是一套高效能的記憶體物件快取系統,安裝前必須先具備 libevent 相依庫。以下從下載、編譯到啟動,完整走一遍流程,並附上常見錯誤的排除方式。
先準備 memcached 與 libevent 的原始碼,官方下載點分別為 memcached 的 danga 網站與 libevent 的 monkey.org。
# tar xvf libevent-1.4.4-stable.tar.gz # cd libevent-1.4.4-stable # ./configure --prefix= /opt/cuix/3rdParty/libevent-1.4.4 # make # make install #cd /opt/cuix/3rdParty/libevent-1.4.4/lib
若 lib 目錄下出現 libevent-1.4.so.2、libevent.a、libevent.so 等檔案,即代表安裝成功。
# tar xvf memcached-1.2.5.tar.gz # cd memcached-1.2.5 # ./configure --with-libevent=/opt/cuix/3rdParty/libevent-1.4.4 --prefix=/opt/cuix/3rdParty/memcached-1.2.5 # make # make install # cd 3rdParty/memcached-1.2.5/bin
若 bin 目錄下存在 memcached 與 memcached-debug 兩個檔案,代表安裝完成。
# cd /opt/cuix/3rdParty/memcached-1.2.5/bin # ./memcached -h
若能列出 -p、-m、-c、-l 等參數說明,即表示安裝成功且路徑正確。常見參數含義如下:
-p <num> TCP port number to listen on (default: 11211) -m <num> max memory to use for items in megabytes, default is 64 MB -c <num> max simultaneous connections, default is 1024 -l <ip_addr> interface to listen on, default is INDRR_ANY -d run as a daemon -u <username> assume identity of <username> (only when run as root) -P <file> save PID in <file>, only used with -d option
若執行時出現以下訊息:
memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory
代表系統找不到 libevent-1.4.so.2。解法有兩種:一是把該檔複製或軟連結到任一列出的 lib 目錄;二是修改 .profile,加入:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib:/opt/cuix/3rdParty/libevent-1.4.4/lib
./memcached -d -m 10 -u cuix -l 10.3.2.36 -p 18887 -c 256 -P /tmp/memcached.pid
參數說明:-d 以守護進程啟動;-m 指定可用記憶體(MB);-u 指定執行用戶;-l 指定監聽 IP;-p 指定監聽端口(建議 1024 以上);-c 為最大並發連線數,預設 1024,可依伺服器負載調整;-P 指定 pid 檔位置。
停止服務時,可用 ps -e 找到 memcached 進程號,再以 kill pid 結束即可。