天天看點

CentOs 6.3 下安裝啟動memcache

CentOs 6.3 下安裝memcache:

1.安裝memcached時,memcached需要libevent支援,是以首先安裝libevent.編譯的時候提示需要指定libevent庫,可以看看系統是否安裝了libevent庫:rpm -qa|grep libevent (libevent-1.4.13-4.el6.i686)如果是系統自帶安裝的,此時可以再自動安裝一下 yum -y install libevent(自動選擇y)

2.因為libevent 這個包是系統預設安裝的,沒有安裝相應的開發所用的頭檔案。是以,還要使用如下指令來安裝:yum install libevent-devel。

3.whereis libevent 或者 #ls -al /usr/lib | grep libevent 就可以看到多個libevent清單檔案

4. #wget https://memcached.googlecode.com/files/memcached-1.4.15.tar.gz 從網上下載下傳tar.gz的memcache的安裝包 ,也可以自己手動離線下載下傳。

5.安裝:

    # tar -zxvf memcached-1.4.8.tar.gz

    # cd memcached-1.4.8

    # ./configure –help (看看幫助檔案)

    # ./configure –prefix=/usr/memcache(源碼安裝:指定安裝路徑)

    # make

    # make install

    (此時如果出現checking for libevent directory… configure: error: libevent is required. You can get it from http://www.monkey.org/~provos/libevent/

If it’s already installed, specify its path using –with-libevent=/dir/,因為libevent 這個包是系統預設安裝的,沒有安裝相應的開發所用的頭檔案。用上邊的yum install libevent-devel後,編譯安裝)

6.啟動:/usr/local/memcache/bin/memcached -d -m 128 -l 192.168.1.1 -p 11211 -u root (如果報:啟動報error while loading shared libraries: libevent-1.4.s解決辦法:原因是找不到libevent-1.4.so.2類庫,解決辦法如下:使用LD_DEBUG=help ./memcached -v來确定 加載的類庫路徑,方法如下:

    ln -s /usr/local/lib/libevent-1.4.so.2 /lib/libevent-1.4.so.2)

----------------------------------------------------

啟動關閉memcache:

1.進入預設安裝目錄(上面安裝的時候,改路徑了),啟動:/usr/local/memcached/bin/memcached -d -c 10240 -m 1024 -u root

    -d 選項是啟動一個守護程序,

    -m 是配置設定給Memcache使用的記憶體數量,機關是MB,這裡是1024MB,預設是64MB

    -u 是運作Memcache的使用者,這裡是root

    -l 是監聽的伺服器IP位址,預設應該是本機

    -p 是設定Memcache監聽的端口,預設是11211,最好是1024以上的端口

    -c 選項是最大運作的并發連接配接數,預設是1024,這裡設定了10240,按照你伺服器的負載量來設定

    -P 是設定儲存Memcache的pid檔案位置

    -h 列印幫助資訊

    -v 輸出警告和錯誤資訊

    -vv 列印用戶端的請求和傳回資訊

2.擷取運作狀态:使用ps -ef|grep memcached (ps:process status 功能說明:報告程式狀況。)或者使用echo stats | nc localhost 11211(可以檢視出pid,nc指令在發行版帶)

3.停止:kill -9 pid  (-9表示強制殺死,pid 為程序的程序辨別符)

------------

另外一個啟動關閉例子:

1.啟動:memcached -d -m 10 -u root -l 192.168.0.122 -p 11200 -c 256 -P /tmp/memcached.pid

2.擷取運作狀态 watch "echo stats | nc 192.168.1.123 11200" (實時狀态)或者 echo stats | nc 192.168.1.123 11200

3.關閉:kill `cat /tmp/memcached.pid`

----------------------------------------------------

連接配接到 memcached:

1.telnet ip 端口,如telnet 192.168.100.11 11211 ,注:如果沒有telnet ,安裝:yum install telnet 即可,telnet 127.0.0.1 11211 可以進入:

    [[email protected] 桌面]# telnet 127.0.0.1 11211

    Trying 127.0.0.1...

    Connected to 127.0.0.1.

    Escape character is '^]'.

    stats  輸入stats 可以看到 檢視memcached狀态資訊

    此時如果要跳出telnet使用ctrl+] 即可,再輸入quit

2.stats檢視狀态,

3.flush_all:清楚緩存

檢視memcached狀态資訊:

STAT pid 22459                             程序ID

STAT uptime 1027046                        伺服器運作秒數

STAT time 1273043062                       伺服器目前unix時間戳

STAT version 1.4.4                         伺服器版本

STAT pointer_size 64                       作業系統字大小(這台伺服器是64位的)

STAT rusage_user 0.040000                  程序累計使用者時間

STAT rusage_system 0.260000                程序累計系統時間

STAT curr_connections 10                   目前打開連接配接數

STAT total_connections 82                  曾打開的連接配接總數

STAT connection_structures 13              伺服器配置設定的連接配接結構數

STAT cmd_get 54                            執行get指令總數

STAT cmd_set 34                            執行set指令總數

STAT cmd_flush 3                           指向flush_all指令總數

STAT get_hits 9                            get命中次數

STAT get_misses 45                         get未命中次數

STAT delete_misses 5                       delete未命中次數

STAT delete_hits 1                         delete命中次數

STAT incr_misses 0                         incr未命中次數

STAT incr_hits 0                           incr命中次數

STAT decr_misses 0                         decr未命中次數

STAT decr_hits 0                           decr命中次數

STAT cas_misses 0                          cas未命中次數

STAT cas_hits 0                            cas命中次數

STAT cas_badval 0                          使用擦拭次數

STAT auth_cmds 0

STAT auth_errors 0

STAT bytes_read 15785                      讀取位元組總數

STAT bytes_written 15222                   寫入位元組總數

STAT limit_maxbytes 1048576                配置設定的記憶體數(位元組)

STAT accepting_conns 1                     目前接受的連結數

STAT listen_disabled_num 0                

STAT threads 4                             線程數

STAT conn_yields 0

STAT bytes 0                               存儲item位元組數

STAT curr_items 0                          item個數

STAT total_items 34                        item總數

STAT evictions 0                           為擷取空間删除item的總數

-----------------------

網上文章:Memcached的代理伺服器軟體:magent使用小記(http://blog.s135.com/post/393)

magent是一款開源的Memcached代理伺服器軟體,其項目網址為:http://code.google.com/p/memagent/

  一、安裝步驟:

  1、編譯安裝libevent:

wget http://monkey.org/~provos/libevent-1.4.9-stable.tar.gz

tar zxvf libevent-1.4.9-stable.tar.gz

cd libevent-1.4.9-stable/

./configure --prefix=/usr

make && make install

cd ../

  2、編譯安裝Memcached:

wget http://danga.com/memcached/dist/memcached-1.2.6.tar.gz

tar zxvf memcached-1.2.6.tar.gz

cd memcached-1.2.6/

./configure --with-libevent=/usr

make && make install

cd ../

  3、編譯安裝magent:

mkdir magent

cd magent/

wget http://memagent.googlecode.com/files/magent-0.5.tar.gz

tar zxvf magent-0.5.tar.gz

/sbin/ldconfig

sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile

make

cp magent /usr/bin/magent

cd ../

  二、使用執行個體:

memcached -m 1 -u root -d -l 127.0.0.1 -p 11211

memcached -m 1 -u root -d -l 127.0.0.1 -p 11212

memcached -m 1 -u root -d -l 127.0.0.1 -p 11213

magent -u root -n 51200 -l 127.0.0.1 -p 12000 -s 127.0.0.1:11211 -s 127.0.0.1:11212 -b 127.0.0.1:11213

  1、分别在11211、11212、11213端口啟動3個Memcached程序,在12000端口開啟magent代理程式;

  2、11211、11212端口為主Memcached,11213端口為備份Memcached;

  3、連接配接上12000的magent,set key1和set key2,根據雜湊演算法,key1被寫入11212和11213端口的Memcached,key2被寫入11212和11213端口的Memcached;

  4、當11211、11212端口的Memcached死掉,連接配接到12000端口的magent取資料,資料會從11213端口的Memcached取出;

  5、當11211、11212端口的Memcached重新開機複活,連接配接到12000端口,magent會從11211或11212端口的Memcached取資料,由于這兩台Memcached重新開機後無資料,是以magent取得的将是空值,盡管11213端口的Memcached還有資料(此問題尚待改進)。

  三、整個測試流程:

[[email protected] ~]# telnet 127.0.0.1 12000

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

stats

memcached agent v0.4

matrix 1 -> 127.0.0.1:11211, pool size 0

matrix 2 -> 127.0.0.1:11212, pool size 0

END

set key1 0 0 8

zhangyan

STORED

set key2 0 0 8

zhangyan

STORED

quit

Connection closed by foreign host.

[[email protected] ~]# telnet 127.0.0.1 11211

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

get key1

END

get key2

VALUE key2 0 8

zhangyan

END

quit

Connection closed by foreign host.

[[email protected] ~]# telnet 127.0.0.1 11212

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

get key1

VALUE key1 0 8

zhangyan

END

get key2

END

quit

Connection closed by foreign host.

[[email protected] ~]# telnet 127.0.0.1 11213

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

get key1

VALUE key1 0 8

zhangyan

END

get key2

VALUE key2 0 8

zhangyan

END

quit

Connection closed by foreign host.

模拟11211、11212端口的Memcached死掉

[[email protected] ~]# ps -ef | grep memcached

root      6589     1  0 01:25 ?        00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11211

root      6591     1  0 01:25 ?        00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11212

root      6593     1  0 01:25 ?        00:00:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11213

root      6609  6509  0 01:44 pts/0    00:00:00 grep memcached

[[email protected] ~]# kill -9 6589

[[email protected] ~]# kill -9 6591

[[email protected] ~]# telnet 127.0.0.1 12000

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

get key1

VALUE key1 0 8

zhangyan

END

get key2

VALUE key2 0 8

zhangyan

END

quit

Connection closed by foreign host.

模拟11211、11212端口的Memcached重新開機複活

[ro[email protected] ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11211

[ro[email protected] ~]# memcached -m 1 -u root -d -l 127.0.0.1 -p 11212

[[email protected] ~]# telnet 127.0.0.1 12000

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

get key1

END

get key2

END

quit

Connection closed by foreign host.

繼續閱讀