Memcached筆記——(一)安裝&常規錯誤&監控

來源:互聯網
上載者:User
08年的時候接觸過 Memcached,當時還對它的用戶端產品嗤之以鼻,畢竟手工代碼沒有各種ORM原生XML配置方便。儘管如此,Memcached現在已經成了伺服器架構裡不可或缺的一部分。

相關連結:
Memcached筆記——(一)安裝&常規錯誤&監控
Memcached筆記——(二)XMemcached&Spring整合
Memcached筆記——(三)Memcached使用總結
Memcached筆記——(四)應對高並發攻擊


一、下載
1. Libevent
簡單的說就是一個事件觸發的網路程式庫,Memcached離不開它。
Shell代碼 wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz
wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz

2. Memcached
今天的主角
Shell代碼 wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz
wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz

二、安裝
1. Libevent
解壓縮
Shell代碼 tar zxvf libevent-2.0.17-stable.tar.gz
tar zxvf libevent-2.0.17-stable.tar.gz

編譯、安裝
Shell代碼 ./configure --prefix=/usr && make && make install
./configure --prefix=/usr && make && make install 

這裡一定要注意指定 --prefix,後面配置memcached的時候就有必要用到。
2. Memcached
解壓
Shell代碼 tar zxvf memcached-1.4.13.tar.gz
tar zxvf memcached-1.4.13.tar.gz

編譯、安裝
Shell代碼 ./configure --with-libevent=/usr/lib && make && make install
./configure --with-libevent=/usr/lib && make && make install

這裡一定要指定 libevent的路徑,否則啟動的時候就有找不到libevent的so檔案的錯誤。
啟動
Shell代碼 memcached -d -m 512 -p 11211 -u root -c 256 -P /var/run/memcached.pid
memcached -d -m 512 -p 11211 -u root -c 256 -P /var/run/memcached.pid

參數
引用
-p <num> TCP port number to listen on (default: 11211)
-U <num> UDP port number to listen on (default: 11211, 0 is off)
-l <addr> interface to listen on (default: INADDR_ANY, all addresses)
<addr> may be specified as host:port. If you don't specify
a port number, the value you specified with -p or -U is
used. You may specify multiple addresses separated by comma
or by using -l multiple times
-d run as a daemon
-u <username> assume identity of <username> (only when run as root)
-m <num> max memory to use for items in megabytes (default: 64 MB)
-M return error on memory exhausted (rather than removing items)
-c <num> max simultaneous connections (default: 1024)
-v verbose (print errors/warnings while in event loop)
-P <file> save PID in <file>, only used with -d option

要關掉memcached
Shell代碼 kill -9 `cat /var/run/memcached.pid`
kill -9 `cat /var/run/memcached.pid`  

是否正常。Telnet上去看看
Shell代碼 telnet xxx.xxx.xxx.xxx 11211
telnet xxx.xxx.xxx.xxx 11211

然後輸入 Shell代碼 stats
stats

接著就能看到:
引用
STAT pid 3021
STAT uptime 3621
STAT time 1331261509
STAT version 1.4.13
STAT libevent 2.0.17-stable
STAT pointer_size 64
STAT rusage_user 0.000000
STAT rusage_system 0.000999
STAT curr_connections 6
STAT total_connections 7
STAT connection_structures 7
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 72
STAT bytes_written 1038
STAT limit_maxbytes 52428800
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
STAT reclaimed 0
END

上面狀況說明Memcached服務正常。
還可以試試get、set、delete、replace
引用 set foo 0 0 3 (儲存命令)
bar (資料)
STORED (結果)
get foo (取得命令)
VALUE foo 0 3 (資料)
bar (資料)

輸入 Shell代碼 quit
quit
退出。

三、系統服務
參照Nginx的系統服務,自己寫了一個Memcached的系統服務指令碼。
先構建 /etc/init.d/memcahed這個檔案,然後賦予其可執行許可權:
Shell代碼 touch /etc/init.d/memcached chmod +x /etc/init.d/memcached
touch /etc/init.d/memcachedchmod +x /etc/init.d/memcached

memcached指令碼如下:
Shell代碼 #!/bin/bash # v.0.0.1 # create by snowolf at 2012.5.25 # # memcached - This shell script takes care of starting and stopping memcached. # # chkconfig: - 90 10 # description: Memcache provides fast memory based storage. # processname: memcached memcached_path="/usr/local/bin/memcached" memcached_pid="/var/run/memcached.pid" memcached_memory="1024" # Source function library. . /etc/rc.d/init.d/functions [ -x $memcached_path ] || exit 0 RETVAL=0 prog="memcached" # Start daemons. start() { if [ -e $memcached_pid -a ! -z $memcached_pid ];then echo $prog" already running...." exit 1 fi echo -n $"Starting $prog " # Single instance for all caches $memcached_path -m $memcached_memory -l 0.0.0.0 -p 11211 -u root -d -P $memcached_pid RETVAL=$? [ $RETVAL -eq 0 ] && { touch /var/lock/subsys/$prog success $"$prog" } echo return $RETVAL } # Stop daemons. stop() { echo -n $"Stopping $prog " killproc -d 10 $memcached_path echo [ $RETVAL = 0 ] && rm -f $memcached_pid /var/lock/subsys/$prog RETVAL=$? return $RETVAL } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status $prog RETVAL=$? ;; restart) stop start ;; *) echo $"Usage: $0 {start|stop|status|restart}" exit 1 esac exit $RETVAL
#!/bin/bash# v.0.0.1# create by snowolf at 2012.5.25## memcached  - This shell script takes care of starting and stopping memcached.## chkconfig: - 90 10# description: Memcache provides fast memory based storage.# processname: memcachedmemcached_path="/usr/local/bin/memcached"memcached_pid="/var/run/memcached.pid"memcached_memory="1024"# Source function library.. /etc/rc.d/init.d/functions[ -x $memcached_path ] || exit 0RETVAL=0prog="memcached"# Start daemons.start() {    if [ -e $memcached_pid -a ! -z $memcached_pid ];then        echo $prog" already running...."        exit 1    fi    echo -n $"Starting $prog "    # Single instance for all caches    $memcached_path -m $memcached_memory -l 0.0.0.0 -p 11211 -u root -d -P $memcached_pid    RETVAL=$?    [ $RETVAL -eq 0 ] && {        touch /var/lock/subsys/$prog        success $"$prog"    }    echo    return $RETVAL}# Stop daemons.stop() {    echo -n $"Stopping $prog "    killproc -d 10 $memcached_path    echo    [ $RETVAL = 0 ] && rm -f $memcached_pid /var/lock/subsys/$prog    RETVAL=$?    return $RETVAL}# See how we were called.case "$1" in        start)            start            ;;        stop)            stop            ;;        status)            status $prog            RETVAL=$?            ;;        restart)            stop            start            ;;        *)            echo $"Usage: $0 {start|stop|status|restart}"            exit 1esacexit $RETVAL

注意這幾行配置,請根據實際情況配置memcached執行檔案路徑,以及Memcached使用記憶體大小:
引用 memcached_path="/usr/local/bin/memcached"
memcached_memory="1024"

追加為系統服務:
Shell代碼 chkconfig --add memcached chkconfig memcached on
chkconfig --add memcachedchkconfig memcached on


然後就可以通過 service memcached start|stop|status|restart控制memcached了。

四、常規錯誤
一開始沒有指定libevent路徑安裝memcached的時候,啟動memcached就報這個錯誤:
引用 memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory

其實這個檔案就在 /usr/lib下。錯就錯在Linux是64bit系統,如果沒有指定libevent路徑,memcached就會去 /usr/lib64下去找。
找到這個檔案
Shell代碼 whereis libevent-2.0.so.5
whereis libevent-2.0.so.5

引用 libevent-2.0.so: /usr/lib/libevent-2.0.so.5
Shell代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.