linux memcached 安裝
1.下載包
1.1.到http://memcached.org/下載linux下的memcached的安裝包.
1.2.到http://libevent.org/ 下載安裝memcached前比需要的包libevent-2.0.22我安裝的是這個版本.
2.安裝libevent-2.0.22
[root@hnxz-vm-dg-s1 ~]# tar -zxvf libevent-2.0.22-stable.tar.gz [root@hnxz-vm-dg-s1 ~]# cd libevent-2.0.22-stable [root@hnxz-vm-dg-s1 libevent-2.0.22-stable]# pwd /root/libevent-2.0.22-stable [root@hnxz-vm-dg-s1 libevent-2.0.22-stable]# ./configure --prefix=/usr/local/libevent [root@hnxz-vm-dg-s1 libevent-2.0.22-stable]# make && make install
3.安裝memcached
[root@hnxz-vm-dg-s1 ~]# tar -zxvf memcached-1.4.25.tar.gz [root@hnxz-vm-dg-s1 ~]# cd memcached-1.4.25 [root@hnxz-vm-dg-s1 memcached-1.4.25]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/lib --enable-64bit [root@hnxz-vm-dg-s1 memcached-1.4.25]# make && make install
--with-libevent=??? 此路徑可以使用命令查找lib位置
whereis libevent
4.測試是否成功安裝memcached:
[root@hnxz-vm-dg-s1 memcached-1.4.25]# ls -al /usr/local/memcached/bin/ total 360 drwxr-xr-x 2 root root 4096 Jan 13 17:24 . drwxr-xr-x 5 root root 4096 Jan 13 17:24 .. -rwxr-xr-x 1 root root 360434 Jan 13 17:24 memcached [root@hnxz-vm-dg-s1 memcached-1.4.25]#
成功的話,會輸出memcached 可執行程序
5.啟動memcached
[root@hnxz-vm-dg-s1 bin]# ll total 352 -rwxr-xr-x 1 root root 360434 Jan 13 17:24 memcached [root@hnxz-vm-dg-s1 bin]# ./memcached -d -m 246 -l 10.207.238.61 -p 11211 -u root
如果中間出現報錯,請仔細檢查錯誤信息,按照錯誤信息來配置或者增加相應的庫或者路徑。
我在啟動過程中報這個錯誤找不到lib包,錯誤如下:
memcached: error while loading shared libraries: libevent-2.0.so.5: cannot
使用命令,即可查看缺失的lib包位置以及錯誤的詳細信息;
LD_DEBUG=libs ./memcached -v 2>&1 > /dev/null | less
[root@hnxz-vm-dg-s1 bin]# ll total 352 -rwxr-xr-x 1 root root 360434 Jan 13 17:24 memcached [root@hnxz-vm-dg-s1 bin]# LD_DEBUG=libs ./memcached -v 2>&1 > /dev/null | less [root@hnxz-vm-dg-s1 bin]#
最終確認為找不到libevent-2.0.so.5 使用ln -s軟連接一個
ln -s /usr/local/lib/libevent.so /usr/lib64/libevent-2.0.so.5
6.給出memcached啟動命令參數詳解.
-d選項是啟動一個守護進程, -m是分配給Memcache使用的內存數量,單位是MB,我這里是10MB, -u是運行Memcache的用戶,我這里是root, -l是監聽的服務器IP地址,如果有多個地址的話,我這里指定了服務器的IP地址192.168.0.200, -p是設置Memcache監聽的端口,我這里設置了12000,最好是1024以上的端口, -c選項是最大運行的并發連接數,默認是1024,我這里設置了256,按照你服務器的負載量來設定, -P是設置保存Memcache的pid文件,我這里是保存在 /tmp/memcached.pid, -p 使用的TCP端口。默認為11211 -m 最大內存大小。默認為64M -vv 用very vrebose模式啟動,調試信息和錯誤輸出到控制臺 -d 作為daemon在后臺啟動
如果要結束Memcache進程,執行:
# kill `cat /tmp/memcached.pid`
或是 通過ps -ef | grep memcached 找到pid ,然后kill
也可以啟動多個守護進程,不過端口不能重復。
# ps -ef | grep memcached
root 28914 1 0 07:27 ? 00:00:00 memcached -d -m 128 -l localhost -p 11211 -u root (啟動ok)
6.測試memcached
#telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
對memecached的管理可以參考這篇文章http://www.blogjava.net/hao446tian/archive/2012/04/11/373773.html
常用指令:
stats
memcached的作者給我們提供查看運行情況的命令。主要是“stats”,使用方法為 “telnet ip 端口號”,登錄后使用“stats”命令。
以下是引用片段:
pid = process id uptime = number of seconds since the process was started time = current time version = memcached version rusage_user = seconds the cpu has devoted to the process as the user rusage_system = seconds the cpu has devoted to the process as the system curr_items = total number of items currently in memcache total_items = total number of items that have passed through the cache bytes = total number of bytes currently in use by curr_items curr_connections = total number of open connections to memcached connection_structures = ??? cmd_get = total GET commands issued to the server cmd_set = total SET commands issued to the server get_hits = total number of times a GET command was able to retrieve and return data get_misses = total number of times a GET command was unable to retrieve and return data bytes_read = total number of bytes input into the server bytes_written = total number of bytes written by the server limit_maxbytes = total storage bytes available to the server.
著重說一下幾個對觀測很有用的項。 limit_maxbytes、bytes memcached在存儲的時候是可以設置失效時間的,但如果存儲已經滿了,那舊數據即使沒有到過期時間,也會被移除。所以需要觀察memcached存儲是否已經滿了,同時這對擴容也是有意義的參考。limit_maxbytes即總的存儲大小,而bytes 就是已經使用的大小,從這兩個數據就可以看出在memcached啟動時,我們為它分配的內存是否足夠使用。 cmd_get、cmd_set memcached啟動后,我們對它一共做了多少次讀取操作呢?從這兩個參數可以觀察出來。 get_hits、get_misses 使用memcached后,我們需要評估我們使用的策略是否合理。不能夠使用中間緩存后,后端的數據庫還是有較大的訪問量,這樣的話中間緩存就變得沒有意義了。get_hits表示命中了多少次讀取,即來memcached取到了多少有效數據;get_m isses表示沒有命中的次數,即此次來取數據的時候,memcached并沒有你所查詢的數據。如果沒有清零統計數據的話,cmd_get = get_hits + get_misses。