天天看點

memcache的安裝和操作使用

【背景】

memcached是一個開源的緩存服務,記憶體式的,資料以key/value值存在預先配置設定好的記憶體中。重新開機就丢失的。在記憶體中,是以存取速度快。 采用libevent事件模型。

memcached 是服務程式

memcache 是用戶端程式 如作為php的子產品

【應用場景】

1 mysql的緩存

2 session的緩存

【安裝】

安裝簡單,易用

【安裝步驟】

官網下載下傳

http://memcached.org/  官網

<a href="http://www.memcached.org/files/memcached-1.4.29.tar.gz" target="_blank">http://www.memcached.org/files/memcached-1.4.29.tar.gz</a>

<code>tar</code> <code>-xvf  memcached-1.4.29.</code><code>tar</code><code>.gz </code>

<code>cd</code> <code>memcached-1.4.29</code>

<code>.</code><code>/configure</code> <code>--prefix=</code><code>/usr/local/memcached</code>

<code>make</code>

<code>make</code> <code>install</code>

可執行檔案/usr/local/memcached/bin/memcached (可将其将加入環境變量)

如果不指定安裝目錄,預設安裝在/usr/local 下面(則預設的可執行檔案在/usr/local/bin/memcached)

【啟動服務與停止服務】

檢視幫助資訊

/usr/local/memcached/bin/memcached -h

檢視版本資訊

/usr/local/memcached/bin/memcached -V

常用的啟動參數:

-p &lt;num&gt;      TCP port number to listen on (default: 11211)

-u &lt;username&gt; assume identity of &lt;username&gt; (only when run as root)

-l &lt;addr&gt;     interface to listen on (default: INADDR_ANY, all addresses)

-d            run as a daemon

-m &lt;num&gt;      max memory to use for items in megabytes (default: 64 MB)

-P &lt;file&gt;     save PID in &lt;file&gt;, only used with -d option

-t &lt;num&gt;      number of threads to use (default: 4)

-c &lt;num&gt;      max simultaneous connections (default: 1024)

-M            return error on memory exhausted (rather than removing items)

啟動2個執行個體:

<code>/usr/local/memcached/bin/memcached</code>  <code>-p 11211 -u root -c 1024 -m 16m -d -P </code><code>/var/run/memcached_11211</code><code>.pid</code>

<code>/usr/local/memcached/bin/memcached</code>  <code>-p 11212 -u root -c 1024 -m 16m -d -P </code><code>/var/run/memcached_11212</code><code>.pid</code>

驗證:

<code>ps</code> <code>-ef |</code><code>grep</code> <code>memcached</code>

<code>netstat</code> <code>-tulnp |</code><code>grep</code> <code>mem</code>

帶日志并背景啟動:

<code>memcached -p 11211 -uroot -c 1024 -m 16m -P </code><code>/var/runmemcached_11212</code><code>.pid -vv &amp;&gt; </code><code>/var/log/memcached_11211</code><code>.log &amp;</code>

停止memcached

1 可以強制全部殺死

<code>pkill memcached</code>

2 強制殺死一個

<code>kill</code> <code>-9 pid</code>

3 正規路徑停止memcached

<code>kill</code> <code>`</code><code>cat</code> <code>/var/run/memcached_11211</code><code>.pid`</code>

【linux指令下簡單操作memcached】

注意: 一般不常用,隻為調試和學習用。

方式1 telnet

方式2 printf或結合nc指令  --- 推薦使用

設定一個key

printf "set key1 0 0 5\r\n12345\r\n"|nc 127.0.0.1 11211

擷取一個key

printf "get key1\r\n"|nc 127.0.0.1 11211 

echo stats | nc localhost  11211

echo stats items | nc 127.0.0.1 11211

實時檢視狀态

watch -n1 -d  "echo stats | nc localhost  11211"

檢視設定

printf  "stats settings\r\n" | nc 127.0.0.1 11212

檢視slabs

printf  "stats slabs\r\n" | nc 127.0.0.1 11212     

【lamp 與lnmp中安裝memcache子產品的環境】

確定環境已經安裝好 lnmp

檢視php的配置資訊

/usr/local/php/bin/php -i |grep configure

可以确定是lnmp還是php-fpm環境

或者通過

ls /usr/local/php 目錄下是否有sbin目錄,有則是lnmp環境,使用了sbin/php-fpm方式啟動php

檢視此時是否已經安裝了memcache子產品

[root@slave html]# cat phpinfo.php 

&lt;?php

phpinfo();

?&gt;

或者

/usr/local/php/bin/php -m

且此時不能看到memcache子產品

【為lnmp的php安裝memcache子產品】

下載下傳php的memcache子產品

<a href="http://pecl.php.net/package/memcache" target="_blank">http://pecl.php.net/package/memcache</a>

<code>tar</code> <code>zxvf memcache-2.2.7.tgz</code>

<code>cd</code> <code>memcache-2.2.7</code>

<code>/usr/local/php/bin/phpize</code>

<code>.</code><code>/configure</code> <code>--with-php-config=</code><code>/usr/local/php/bin/php-config</code> 

<code>make</code> 

結果:

[root@slave memcache-2.2.7]# make install

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

【memcache在php中生效-修改php.ini配置檔案】

extension_dir = "./"

修改為

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626"

并添加一行

extension = memcache.so

重新開機php-fpm

killall php-fpm

啟動

/usr/local/php/sbin/php-fpm

或者知指定配置檔案進行啟動。

/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini  -y /usr/local/php/etc/php-fpm.conf

-c &lt;path&gt;|&lt;file&gt; Look for php.ini file in this directory

-y, --fpm-config &lt;file&gt;

推薦使用(安全重新開機的方法)

<code>預設php-fpm.conf是沒有的需要自己建立,裡面就是配置是管理php-fpm程序的。需要自己建立(且pid預設是none的,需要打開pid那行的配置檔案,啟用pid檔案)</code>

<code>#/usr/local/php/bin/php -m |grep mem</code>

<code>memcache</code>

且phpinfo頁面可以看到memcache子產品的詳細資訊

【一段小程式php連接配接memcache】

連接配接資料庫隻需要指定IP:port就可以了。

[root@slave html]# cat  test_conn_memcache.php

<code>&lt;?php  </code>

<code>//連接配接Memcache  </code>

<code>$mem</code> <code>= </code><code>new</code> <code>Memcache;  </code>

<code>$mem</code><code>-&gt;addServer(</code><code>"192.168.100.10"</code><code>, 11211);  </code>

<code>//儲存資料  </code>

<code>$mem</code><code>-&gt;set(</code><code>'key1'</code><code>, </code><code>'This is first value'</code><code>, 0, 60);  </code>

<code>$val</code> <code>= </code><code>$mem</code><code>-&gt;get(</code><code>'key1'</code><code>);  </code>

<code>echo</code> <code>"Get key1 value: "</code> <code>. </code><code>$val</code> <code>.</code><code>"&lt;br&gt;"</code><code>;  </code>

<code>//儲存數組資料  </code>

<code>$arr</code> <code>= </code><code>array</code><code>(</code><code>'aaa'</code><code>, </code><code>'bbb'</code><code>, </code><code>'ccc'</code><code>, </code><code>'ddd'</code><code>);  </code>

<code>$mem</code><code>-&gt;set(</code><code>'key2'</code><code>, </code><code>$arr</code><code>, 0, 60);  </code>

<code>$val2</code> <code>= </code><code>$mem</code><code>-&gt;get(</code><code>'key2'</code><code>);  </code>

<code>echo</code> <code>"Get key2 value: "</code><code>;  </code>

<code>print_r(</code><code>$val2</code><code>);  </code>

<code>echo</code> <code>"&lt;br&gt;"</code><code>;  </code>

<code>//删除資料  </code>

<code>$mem</code><code>-&gt;</code><code>delete</code><code>(</code><code>'key1'</code><code>);  </code>

<code>echo</code> <code>"After delete key1 ,Get key1 value: "</code> <code>. </code><code>$val</code> <code>. </code><code>"&lt;br&gt;"</code><code>;  </code>

<code>//關閉連接配接  </code>

<code>$mem</code><code>-&gt;close();  </code>

<code>?&gt;</code>

或如下簡單的程式

<code>&lt;?php</code>

<code>$memcache</code> <code>= </code><code>new</code> <code>Memcache;             </code><code>//建立一個memcache對象</code>

<code>$memcache</code><code>-&gt;connect(</code><code>'localhost'</code><code>, 11211) </code><code>or</code> <code>die</code> <code>(</code><code>"Could not connect"</code><code>); </code><code>//連接配接Memcached伺服器$memcache-&gt;</code>

<code>set(</code><code>'key'</code><code>, </code><code>'test'</code><code>);        </code><code>//設定一個變量到記憶體中,名稱是key 值是test</code>

<code>$get_value</code> <code>= </code><code>$memcache</code><code>-&gt;get(</code><code>'key'</code><code>);  </code><code>//從記憶體中取出key的值</code>

<code>echo</code> <code>$get_value</code><code>;?&gt;</code>

<a href="http://s1.51cto.com/wyfs02/M02/85/85/wKioL1enJW3z0IkOAABJv6ivvnE798.jpg" target="_blank"></a>

注意: php-fpm的日志路徑/usr/local/php/var/log/php-fpm.log

【監控memcached的使用狀态】

1 結合zabbix和shell

記憶體大小

命中率

2 使用開源的圖形軟體memadmin

<a href="http://www.junopen.com/memadmin" target="_blank">http://www.junopen.com/memadmin</a>

安裝(lnmp 或者 lamp環境可運作)

tar xvf memadmin-1.0.12.tar.gz -C /usr/local/nginx/html/

(可能要稍稍配置一下nginx.conf檔案内容)

連接配接

http://192.168.100.13/memadmin/index.php?action=set.con

大緻所的stats資訊:

參數值描述

pid2773memcache伺服器程序ID

uptime13263伺服器已運作秒數

time1470577520伺服器目前Unix時間戳

version1.4.13memcache版本

libevent1.4.13-stablelibevent版本

pointer_size64作業系統指針大小

rusage_user0.321951程序累計使用者時間

rusage_system0.446932程序累計系統時間

curr_connections10目前連接配接數量

total_connections58Memcached運作以來連接配接總數

connection_structures12Memcached配置設定的連接配接結構數量

reserved_fds20内部使用的FD數

cmd_get   44get指令請求次數

cmd_set     26set指令請求次數

cmd_flush0flush指令請求次數

cmd_touch0touch指令請求次數

get_hits25get指令命中次數

get_misses19get指令未命中次數

delete_misses0delete指令未命中次數

delete_hits12delete指令命中次數

incr_misses0incr指令未命中次數

incr_hits0incr指令命中次數

decr_misses0decr指令未命中次數

decr_hits0decr指令命中次數

cas_misses0cas指令未命中次數

cas_hits0cas指令命中次數

cas_badval0使用擦拭次數

touch_hits0touch指令命中次數

touch_misses0touch指令未命中次數

auth_cmds0認證指令處理的次數

auth_errors0認證失敗數目

bytes_read3121讀取總位元組數

bytes_written23943發送總位元組數

limit_maxbytes16777216配置設定的記憶體總大小(位元組)

accepting_conns1接受新的連接配接

listen_disabled_num0失效的監聽數

threads     4目前線程數

conn_yields0連接配接操作主動放棄數目

hash_power_level16hash表等級

hash_bytes524288目前hash表大小

hash_is_expanding0hash表正在擴充

expired_unfetched0已過期但未擷取的對象數目

evicted_unfetched0已驅逐但未擷取的對象數目

bytes132目前存儲占用的位元組數

curr_items1目前存儲的資料總數

total_items24啟動以來存儲的資料總數

evictions0LRU釋放的對象數目

reclaimed1已過期的資料條目來存儲新資料的數目

差別:

curr_items              Memcached 目前存儲的内容數量    

total_items             Memcached 啟動以來存儲過的内容總數    

【memcached的叢集】

因為mencache的各各節點是獨立的,資料不共享,是以需要通過程式排程算法進行memcached分布式的使用.

如:memcache代理程式進行url hash算法 決定到某個node

【用作lamp的session緩存--針對小站點】

vim  /usr/local/php/etc/php.ini

修改兩項就可以

 session.save_handler = files

改成

 session.save_handler = memcache

;session.save_path = "/tmp"

改成呢個

session.save_path = "tcp://ip:port"

【運維管理memcache常用的指令】

1 測試增加鍵值并指派

set指令 

文法: 

command &lt;key&gt; &lt;flags&gt; &lt;expiration time&gt; &lt;bytes&gt;

&lt;value&gt;

參數說明如下:

command set/add/replace

key     key 用于查找緩存值

flags     可以包括鍵值對的整型參數,客戶機使用它存儲關于鍵值對的額外資訊

expiration time     在緩存中儲存鍵值對的時間長度(以秒為機關,0 表示永遠)

bytes     在緩存中存儲的位元組點

value     存儲的值(始終位于第二行)

set userid 0 0 5   --&gt;執行

12345  —&gt;第二行設定内容value

STORED --&gt;傳回

2 擷取value  

get mykey mykey1  擷取多個key的值  

get userid   --&gt;執行擷取userid的值

VALUE userid 0 5  --&gt;userid的資訊

12345  --&gt;userid的值

END  ---&gt;傳回值

# echo -e  "get a" | nc 127.0.0.1 11211

VALUE a 0 5

22222

END

# echo -e  "set a 0 0 5\r\n22222\r\n" | nc 127.0.0.1 11211

STORED

ERROR    ---&gt; echo指令的結束自帶\n 導緻。

最好是使用printf進行輸出。因為printf和echo是有差別的,因為prinf是不自帶換行符的,而且memcache中的換行符隻是支援 \r\n.是以使用echo 會自帶的\n在内部會産生一個ERROR的錯誤。

[root@ops-cuizhiliang001 ~]# printf  "set a 0 0 5\r\n33333\r\n" | nc 127.0.0.1 11211

3 add 差別set

僅當緩存中不存在鍵時,add 指令才會向緩存中添加一個鍵值對。如果緩存中已經存在鍵,則之前的值将仍然保持相同,并且您将獲得響應 NOT_STORED。

下面是使用 add 指令的标準互動:

set userid 0 0 5

get userid    

VALUE userid 0 5

add userid 0 0 5   ---&gt;執行 

11111   ---&gt; value

NOT_STORED  ---&gt;傳回值,

get userid

VALUE userid 0 5 

22222   ---&gt;因為執行add的時候userid是存在的,所有值不變,為原來的值。

4 flush_all

重新整理掉所有keys的值,清空。注意是清空緩存的值,key是不删除的。将目前所有緩存資料設定為過期,但不會釋放記憶體

5 delete 删除執行的key

VALUE userid 0 4

1234

delete userid   --&gt;執行删除操作

DELETED   ---&gt;傳回值

6 stats 檢視資訊

printf "stats\r\n" |nc 127.0.0.1 11211

本文轉自殘劍部落格51CTO部落格,原文連結http://blog.51cto.com/cuidehua/1835401如需轉載請自行聯系原作者

cuizhiliang

下一篇: awk