memcached 配置、安裝

memcached安裝配置
1.下載地址:

http://www.memcached.org/


2.Memcache的安裝先決條件:先安裝libevent

  Libevent介紹:libevent是一個事件觸發的網絡庫,適用於windows、linux、bsd等多種平臺,內部使用select、epoll、kqueue等系統調用管理事件機制。著名的用於apache的php緩存庫memcached據說也是libevent based,而且libevent在使用上可以做到跨平臺,而且根據libevent官方網站上公佈的數據統計,似乎也有着非凡的性能。

  下載地址:http://monkey.org/~provos/libevent/

http://libevent.org/

3.需要有gcc編譯器

gcc -v 查看(yum install gcc方式可安裝)


4.libevent安裝(系統自帶的時候就不用安裝了。。)

[root@localhost]#tar -zxvf  libevent-2.0.19-stable.tar.gz

[root@localhost]#cd  libevent-2.0.19-stable
[root@localhost]#./configure --prefix=/usr/local/libevent

[root@localhost]#make

[root@localhost]#make install

測試是否安裝成功

[root@localhost]# ls /usr/local/libevent/lib |grep libevent


5.安裝memcached

[root@localhost]#tar -zxvf  memcached-1.2.6.tar.gz

[root@localhost]#cd  memcached-1.2.6

//指定安裝路徑到/usr/local/memcached/目錄下,同時指定libevent的安裝位置,系統自帶libevent的時候就不用指定了。。
[root@localhost]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent

[root@localhost]#make

[root@localhost]#make install

測試memcache:

[root@localhost]# ls -al/usr/local/memcached/bin


6.啓動memcache

啓動Memcache的服務器端:

[root@localhost]# /usr/local/memcached/bin/memcached -d -m 100 -u root -l 124.248.33.50 -p 12000 -c 1024-P /tmp/memcached.pid

memcached啓動參數描述:

-d :啓動一個守護進程,

-m:分配給Memcache使用的內存數量,單位是MB,默認是64MB,這裏是100MB

-u :運行Memcache的用戶,這裏是root

-l  :監聽的服務器IP地址,這裏指定了服務器的IP地址124.248.33.50

-p :設置Memcache監聽的端口,默認是11211    注:-p(p爲小寫),這裏使用12000

-c :設置最大併發連接數,默認是1024

-P :設置保存Memcache的pid文件   注:-P(P爲大寫),這裏是保存在/tmp/memcached.pid,

Memcache可以搭建備份的。主要是-l -p -b參數的設置,具體沒試驗過


2)、如果要結束Memcache進程,執行:

[root@localhost]# kill cat /tmp/memcached.pid


7、  監測是否啓動成功方式

我們可以使用telnet來對我們的memcache服務器進行訪問

例如:telnet 124.248.33.50 12000

連接上後,直接敲擊stats命令看當前緩存服務器狀態


8、  設定memcache的telnet訪問限制

請限定telnet的訪問,使之只能在中轉機上訪問

9、  memcache的常見概念

memcached會預先分配內存,memcached分配內存方式稱之爲allocator,首先,這裏有3個概念:
1 slab
2 page
3 chunk
解釋一下,一般來說一個memcahced進程會預先將自己劃分爲若干個slab,每個slab下又有若干個page,每個page下又有多個chunk,如果我們把這3個咚咚看作是object得話,這是兩個一對多得關係。再一般來說,slab得數量是有限得,幾個,十幾個,或者幾十個,這個跟進程配置得內存有關。而每個slab下得page默認情況是1m,也就是說如果一個slab佔用100m得內存得話,那麼默認情況下這個slab所擁有得page得個數就是100,而chunk就是我們得數據存放得最終地方。

10、  Memcache的常用命令

 Memcache常見的命令都在協議文件上:安裝文件的的doc目錄下的protocol.txt文件中有詳細說明

 1)、stats 查詢狀態命令:

2)、version 查詢版本號

3)、quit 退出命令

4)、stats items 顯示各個slab的信息,包括chunk的大小、數目、使用情況等:stats slabs5)、顯示各個slab中item的數目和最老item的年齡(最後一次訪問距離現在的秒數):
6)、stats malloc 顯示內存分配

7)、flush_all 清空緩存數據(其實是將所有緩存數據標記爲過期)

如果要結束Memcache進程,執行:kill cat pid文件路徑



11.java的測試代碼

需要支持包memcached.jar、java_memcached-release_2.6.3.jar

package exp.vo;
import java.util.Date;
import com.danga.MemCached.*;

public class BasicTest {
 private static final String POOL_NAME="test_pool";
 protected static MemCachedClient mcc;
 static {
  //設置緩存服務器列表,當使用分佈式緩存的時,可以指定多個緩存服務器
  String[] servers =
  {
     "124.248.33.50:12000",
  };
  //與服務器列表中對應的各服務器的權重
  Integer[] weights = {3};

  //創建Socked連接池
  SockIOPool pool = SockIOPool.getInstance(POOL_NAME);

  //向連接池設定服務器和權重
  pool.setServers( servers );
  pool.setWeights( weights );
  
  //連接池參數
  pool.setInitConn( 5 );
  pool.setMinConn( 5 );
  pool.setMaxConn( 250 );
  pool.setMaxIdle( 1000 * 60 * 60 * 6 );

  // set the sleep for the maint thread
  // it will wake up every x seconds and
  // maintain the pool size
  pool.setMaintSleep( 30 );

  // set some TCP settings
  // disable nagle
  // set the read timeout to 3 secs
  // and don't set a connect timeout
  pool.setNagle( false );
  pool.setSocketTO( 3000 );
  pool.setSocketConnectTO( 0 );

  // initialize the connection pool
  pool.initialize();


  // lets set some compression on for the client
  // compress anything larger than 64k

  mcc=new MemCachedClient(POOL_NAME);
  mcc.setCompressEnable( true );
  mcc.setCompressThreshold( 64 * 1024 );
 }

 public static void main(String[] args) throws Exception{
  /System.currentTimeMillis()正式用時需刪掉。不知道怎麼偏移的~~~
  mcc.set("msg","Hello,world!",new Date(System.currentTimeMillis()+1300));
  Thread.sleep(500);
  System.out.println(mcc.get("msg")); 
 }
}

一些telnet的命令

Command Description Example
get Reads a value get mykey
set Set a key unconditionally set mykey 0 60 5
add Add a new key add newkey 0 60 5
replace Overwrite existing key replace key 0 60 5
append Append data to existing key append key 0 60 15
prepend Prepend data to existing key prepend key 0 60 15
incr Increments numerical key value by given number incr mykey 2
decr Decrements numerical key value by given number decr mykey 5
delete Deletes an existing key delete mykey
flush_all Invalidate specific items immediately flush_all
Invalidate all items in n seconds flush_all 900
stats Prints general statistics stats
Prints memory statistics stats slabs
Prints memory statistics stats malloc
Print higher level allocation statistics stats items
  stats detail
  stats sizes
Resets statistics stats reset
version Prints server version. version
verbosity Increases log level verbosity
quit Terminate telnet session quit

1.安裝libevent

[root@localhost source]# tar zxf libevent-2.0.19-stable.tar.gz
[root@localhost source]# cd libevent-2.0.19-stable
[root@localhost libevent-2.0.19-stable]# ./configure --prefix=/opt/libevent
[root@localhost libevent-2.0.19-stable]# make
[root@localhost libevent-2.0.19-stable]# make install
[root@localhost libevent-2.0.19-stable]# echo '/opt/libevent/lib' > /etc/ld.so.conf.d/libevent.conf
[root@localhost libevent-2.0.19-stable]# ldconfig

2.安裝memcached

[root@localhost source]# tar zxf memcached-1.4.5.tar.gz
[root@localhost source]# cd memcached-1.4.5
[root@localhost memcached-1.4.5]# ./configure --prefix=/opt/memcached --with-libevent=/opt/libevent
[root@localhost memcached-1.4.5]# make
[root@localhost memcached-1.4.5]# make install

3.啓動memcached

[root@localhost ~]# /opt/memcached/bin/memcached -d -m 1024 -u root -p 12000



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章