一、引言
寫有關NoSQL數據庫有關的文章已經有一段時間了,可以高興的說,Redis暫時就算寫完了,從安裝到數據類型,在到集群,幾乎都寫到了。如果以后有了心得,再補充吧。然后就是MongoDB了,有關MongoDB的文章也寫了一些了,安裝、配置和基本使用都寫了,然后就是一些高級的課題了。最近由於工作比較忙的原因,寫東西就慢了,MongoDB的文章也暫緩了。今天正好有時間,我就把在Linux環境下安裝和配置Memcached的過程寫下來。其實這個文章不是今天寫的了,寫了有幾天了,只是利用今天把文章發出來。
二、Memcached簡介
Memcached是一個自由開源的,高性能,分布式內存對象緩存系統。是以LiveJournal旗下Danga Interactive公司的Brad Fitzpatric為首開發的一款軟件。現在已成為mixi、hatena、Facebook、Vox、LiveJournal等眾多服務中提高Web應用擴展性的重要因素。
Memcached是一種基於內存的key-value存儲,用來存儲小塊的任意數據(字符串、對象)。這些數據可以是數據庫調用、API調用或者是頁面渲染的結果。Memcached簡潔而強大。它的簡潔設計便於快速開發,減輕開發難度,解決了大數據量緩存的很多問題。它的API兼容大部分流行的開發語言。 本質上,它是一個簡潔的key-value存儲系統。和Redis是有些類似的,但是也有很大的不同了。
三、Memcached在Linux上的安裝
今天我們開始講如何在Linux系統上安裝和配置 Memcached 服務,過程很簡單,馬上開始我們的安裝吧。
1、准備安裝環境
yum install gcc wget make cmake libtool autoconf
2、下載相應的庫和Memcached源碼
libevent,因為Memcached依賴這個Libevent庫,所以必須先下載安裝
官網地址:http://libevent.org/
3、先安裝 Libevent 庫文件
首先檢查系統中是否安裝了libevent
[root@linux memcached]# pwd /root/software/download/memcached [root@linux memcached]# rpm -qa |grep libevent [root@linux memcached]# //無值說明libevent沒有安裝
如果安裝了則查看libevent的安裝路徑,后續安裝時需要用到
[root@linux bin]# rpm -ql libevent
未安裝軟件包 libevent
[root@linux bin]#
如果沒有安裝,則先安裝libevent
3.1、下載libevent
[root@linux download]# ls libevent memcached mongodb redis [root@linux download]# cd libevent/ [root@linux libevent]# wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz [root@linux libevent]# ls libevent-2.1.8-stable.tar.gz [root@linux libevent]#
3.2、tar 解壓Libevent庫文件
[root@linux libevent]# pwd /root/software/download/libevent [root@linux libevent]# tar zxvf libevent-2.1.8-stable.tar.gz [root@linux libevent]# ls libevent-2.1.8-stable libevent-2.1.8-stable.tar.gz
3.3、cd 進入根目錄
[root@linux libevent]# cd libevent-2.1.8-stable [root@linux libevent-2.1.8-stable]# pwd /root/software/download/libevent/libevent-2.1.8-stable [root@linux libevent-2.1.8-stable]# ./configure -h/-help //在根目錄下面查看一下配置文件,方便以后使用
3.4、設置安裝路徑,安裝目錄可以自定義。我的安裝目錄是:/root/application/program/memcached/libevent
[root@linux libevent-2.1.8-stable]# ./configure --prefix=/root/application/program/memcached/libevent checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for a thread-safe mkdir -p... /usr/bin/mkdir -p checking for gawk... gawk checking whether make sets $(MAKE)... yes checking whether make supports nested variables... yes checking whether make supports nested variables... (cached) yes checking for style of include used by make... GNU checking for gcc... gcc ...... ...... checking size of pthread_t... 8 checking for library containing ERR_remove_thread_state... -lcrypto checking that generated files are newer than configure... done configure: creating ./config.status config.status: creating libevent.pc config.status: creating libevent_openssl.pc config.status: creating libevent_pthreads.pc config.status: creating libevent_core.pc config.status: creating libevent_extra.pc config.status: creating Makefile config.status: creating config.h config.status: creating evconfig-private.h config.status: evconfig-private.h is unchanged config.status: executing depfiles commands config.status: executing libtool commands [root@linux libevent-2.1.8-stable]#
3.5、編譯和安裝 make && make install
[root@linux libevent-2.1.8-stable]# make && make install
4、安裝Memcached源文件
//這不可以不執行,默認有相應的權限。如果沒有,修改安裝包的執行操作權限,chmod 777 memcached-1.4.15.tar.gz
4.1、下載 memcached-1.4.15 源程序
[root@linux memcached]# pwd /root/software/download/memcached [root@linux memcached]# wget memcached 的地址 [root@linux memcached]# ls memcached-1.4.15.tar.gz [root@linux memcached]#
4.2、解壓Memcached 壓縮文件
[root@linux memcached]# pwd /root/software/download/memcached [root@linux memcached]# tar zxvf memcached.tar.gz memcached-1.4.15/ memcached-1.4.15/Makefile.am memcached-1.4.15/trace.h memcached-1.4.15/config.guess memcached-1.4.15/cache.c memcached-1.4.15/util.c memcached-1.4.15/assoc.c .... .... memcached-1.4.15/doc/xml2rfc/rfc2629-other.ent memcached-1.4.15/doc/xml2rfc/reference.RFC.0768.xml memcached-1.4.15/doc/xml2rfc/rfc2629-xhtml.ent memcached-1.4.15/doc/xml2rfc/rfc2629-noinc.xsl memcached-1.4.15/doc/xml2rfc/rfc2629-refchk.xsl memcached-1.4.15/doc/xml2rfc/rfc2629.dtd memcached-1.4.15/doc/protocol.txt memcached-1.4.15/doc/Makefile memcached-1.4.15/compile [root@linux memcached]#
4.3、進入到 memcached 根目錄
[root@linux memcached]# ls memcached-1.4.15 memcached-1.4.15.tar.gz [root@linux memcached]# cd memcached-1.4.15 [root@linux memcached-1.4.15]# pwd /root/software/download/memcached/memcached-1.4.15
4.4、配置安裝目錄
[root@linux memcached-1.4.15]# pwd /root/software/download/memcached/memcached-1.4.15 [root@linux memcached]# ./configure -h/-help //可以查看memcached 配置文件,方便使用 [root@linux memcached-1.4.15]# ./configure --prefix=/root/application/program/memcached/memcachedfile --with-libevent=/root/application/program/memcached/libevent checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking target system type... x86_64-unknown-linux-gnu checking for a BSD-compatible install... /usr/bin/install -c checking whether build environment is sane... yes checking for a thread-safe mkdir -p... /usr/bin/mkdir -p .... .... checking for xml2rfc... no checking for xsltproc... /usr/bin/xsltproc configure: creating ./config.status config.status: creating Makefile config.status: creating doc/Makefile config.status: creating config.h config.status: executing depfiles commands [root@linux memcached-1.4.15]#
4.5、編譯和安裝 make && make install
[root@linux memcached-1.4.15]# make && make install
5、啟動Memcached服務環境
//啟動程序必須在安裝Memcached服務的目錄的Bin目錄下執行操作 [root@linux bin]# pwd /root/application/program/memcached/memcachedfile/bin [root@linux bin]# ./memcached -m 64 -p 11211 -u nobody -vvv 前端 slab class 1: chunk size 96 perslab 10922 slab class 2: chunk size 120 perslab 8738 slab class 3: chunk size 152 perslab 6898 slab class 4: chunk size 192 perslab 5461 slab class 5: chunk size 240 perslab 4369 slab class 6: chunk size 304 perslab 3449 .... .... <29 server listening (udp) <30 server listening (udp) <29 server listening (udp) <30 server listening (udp) <29 server listening (udp) <30 server listening (udp) <29 server listening (udp) <30 server listening (udp) //當前處於阻塞狀態,說明Memcached啟動成功,但是該窗口不能關閉,否則服務也會關閉 [root@linux bin]# ./memcached -m 64 -p 11211 -u nobody -d 后台啟動 [root@linux bin]# //查看memcache是否開啟: [root@linux ~]# netstat -ntpl|grep memcache
6、關閉Memcached服務環境
[root@linux ~]# kill PID(Memcached服務的進程號) [root@linux ~]# kill 12177
7、配置
啟動參數介紹如下:和上面的命令不對應
-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,也可以啟動多個守護進程,不過端口不能重復。
8、我們可以查看memcached命令的幫助文件
1 [root@linux bin]# pwd 2 /root/application/program/memcached/memcachedfile/bin 3 [root@linux bin]# ./memcached -h/-help 4 memcached 1.4.15 5 -p <num> TCP port number to listen on (default: 11211) 6 -U <num> UDP port number to listen on (default: 11211, 0 is off) 7 -s <file> UNIX socket path to listen on (disables network support) 8 -a <mask> access mask for UNIX socket, in octal (default: 0700) 9 -l <addr> interface to listen on (default: INADDR_ANY, all addresses) 10 <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 11 -d run as a daemon 12 -r maximize core file limit 13 -u <username> assume identity of <username> (only when run as root) 14 -m <num> max memory to use for items in megabytes (default: 64 MB) 15 -M return error on memory exhausted (rather than removing items) 16 -c <num> max simultaneous connections (default: 1024) 17 -k lock down all paged memory. Note that there is a limit on how much memory you may lock. Trying to allocate more than that would fail, so be sure you set the limit correctly for the user you started the daemon with (not for -u <username> user;under sh this is done with 'ulimit -S -l NUM_KB'). 18 -v verbose (print errors/warnings while in event loop) 19 -vv very verbose (also print client commands/reponses) 20 -vvv extremely verbose (also print internal state transitions) 21 -h print this help and exit 22 -i print memcached and libevent license 23 -P <file> save PID in <file>, only used with -d option 24 -f <factor> chunk size growth factor (default: 1.25) 25 -n <bytes> minimum space allocated for key+value+flags (default: 48) 26 -L Try to use large memory pages (if available). Increasing the memory page size could reduce the number of TLB misses and improve the performance. In order to get large pages from the OS, memcached will allocate the total item-cache in one large chunk. 27 -D <char> Use <char> as the delimiter between key prefixes and IDs.This is used for per-prefix stats reporting. The default is ":" (colon). If this option is specified, stats collection is turned on automatically; if not, then it may be turned on by sending the "stats detail on" command to the server. 28 -t <num> number of threads to use (default: 4) 29 -R Maximum number of requests per event, limits the number of requests process for a given connection to prevent starvation (default: 20) 30 -C Disable use of CAS 31 -b Set the backlog queue limit (default: 1024) 32 -B Binding protocol - one of ascii, binary, or auto (default) 33 -I Override the size of each slab page. Adjusts max item size (default: 1mb, min: 1k, max: 128m) 34 -o Comma separated list of extended or experimental options 35 - (EXPERIMENTAL) maxconns_fast: immediately close new connections if over maxconns limit 36 - hashpower: An integer multiplier for how large the hash table should be. Can be grown at runtime if not big enough.Set this based on "STAT hash_power_level" before a restart. 37 [root@linux bin]#
9、我們使用telnet命令來連接Linux系統上的Memcached服務。
9.1、在Window環境下通過telnet連接Memcached服務
1 C:\Users\Administrator>telnet192.168.127.130 11211 2 3 //當前處於黑屏狀態,等待輸入命令,直接輸入stats,直接回車。 4 stats 5 STAT pid 12177 6 STAT uptime 619 7 STAT time 1522221744 8 STAT version 1.4.15 9 STAT libevent 2.1.8-stable 10 STAT pointer_size 64 11 STAT rusage_user 0.005197 12 STAT rusage_system 0.067566 13 STAT curr_connections 10 14 STAT total_connections 12 15 STAT connection_structures 11 16 STAT reserved_fds 20 17 STAT cmd_get 0 18 STAT cmd_set 0 19 STAT cmd_flush 0 20 STAT cmd_touch 0 21 STAT get_hits 0 22 STAT get_misses 0 23 STAT delete_misses 0 24 STAT delete_hits 0 25 STAT incr_misses 0 26 STAT incr_hits 0 27 STAT decr_misses 0 28 STAT decr_hits 0 29 STAT cas_misses 0 30 STAT cas_hits 0 31 STAT cas_badval 0 32 STAT touch_hits 0 33 STAT touch_misses 0 34 STAT auth_cmds 0 35 STAT auth_errors 0 36 STAT bytes_read 37 37 STAT bytes_written 21 38 STAT limit_maxbytes 67108864 39 STAT accepting_conns 1 40 STAT listen_disabled_num 0 41 STAT threads 4 42 STAT conn_yields 0 43 STAT hash_power_level 16 44 STAT hash_bytes 524288 45 STAT hash_is_expanding 0 46 STAT bytes 0 47 STAT curr_items 0 48 STAT total_items 0 49 STAT expired_unfetched 0 50 STAT evicted_unfetched 0 51 STAT evictions 0 52 STAT reclaimed 0 53 END 54 55 //能連接上,說明memcache成功啟用,可使用stats命令查看當前狀態
9.2、在Linux環境下使用telnet命令連接Memcached服務
1 [root@linux bin]# telnet 192.168.127.130 11211 2 Trying 192.168.127.130... 3 Connected to 192.168.127.130. 4 Escape character is '^]'. 5 //當前窗口處於等待狀態,可以直接輸入命令執行 6 stats //直接運行該命令 7 STAT pid 12177 8 STAT uptime 619 9 STAT time 1522221744 10 STAT version 1.4.15 11 STAT libevent 2.1.8-stable 12 STAT pointer_size 64 13 STAT rusage_user 0.005197 14 STAT rusage_system 0.067566 15 STAT curr_connections 10 16 STAT total_connections 12 17 STAT connection_structures 11 18 STAT reserved_fds 20 19 STAT cmd_get 0 20 STAT cmd_set 0 21 STAT cmd_flush 0 22 STAT cmd_touch 0 23 STAT get_hits 0 24 STAT get_misses 0 25 STAT delete_misses 0 26 STAT delete_hits 0 27 STAT incr_misses 0 28 STAT incr_hits 0 29 STAT decr_misses 0 30 STAT decr_hits 0 31 STAT cas_misses 0 32 STAT cas_hits 0 33 STAT cas_badval 0 34 STAT touch_hits 0 35 STAT touch_misses 0 36 STAT auth_cmds 0 37 STAT auth_errors 0 38 STAT bytes_read 37 39 STAT bytes_written 21 40 STAT limit_maxbytes 67108864 41 STAT accepting_conns 1 42 STAT listen_disabled_num 0 43 STAT threads 4 44 STAT conn_yields 0 45 STAT hash_power_level 16 46 STAT hash_bytes 524288 47 STAT hash_is_expanding 0 48 STAT bytes 0 49 STAT curr_items 0 50 STAT total_items 0 51 STAT expired_unfetched 0 52 STAT evicted_unfetched 0 53 STAT evictions 0 54 STAT reclaimed 0 55 END 56 57 //能連接上,說明memcache成功啟用,可使用stats命令查看當前狀態
10、查看Memcached服務是否啟動:ps aux|grep memcached 或者 ps -ef|grep memcached
[root@linux ~]# ps aux|grep memcached nobody 12177 0.0 0.0 323584 1052 ? Ssl 15:12 0:00 ./memcached -m 64 -p 11211 -u nobody -d root 12292 0.0 0.0 116860 1012 pts/0 S+ 15:33 0:00 grep --color=auto memcached [root@linux ~]# ps -ef|grep memcached nobody 12177 1 0 15:12 ? 00:00:00 ./memcached -m 64 -p 11211 -u nobody -d root 12295 11965 0 15:33 pts/0 00:00:00 grep --color=auto memcached
11、如果我們通過telnet命令無法連接Linux上的Linux系統上的Memcached服務,可能和端口有關系。
//在使用window的cmd連接Linux環境上的Memcached的時候,必須先把11211這個端口增加到防火牆,否則無法連接,執行命令如下: [root@linux ~]# firewall-cmd --zone=public --add-port=11211/tcp --permanent [root@linux ~]# firewall-cmd --reload //再次重新連接就沒有問題了
四、總結
好了,寫完了。有關Memcached的文章可能要晚一點再出來了,先要把MongoDB的文章寫完,再來寫有關Memcached的文章了,學習和使用也需要一個過程。還有一個原因,由於Redis的崛起和強大,已經完全可以替代Memcached了,而且很多的公司也不使用它了,所以由於原因種種,該系列就往后推了。