08年的時(shí)候接觸過(guò)Memcached,,當(dāng)時(shí)還對(duì)它的客戶端產(chǎn)品嗤之以鼻,,畢竟手工代碼沒(méi)有各種ORM原生XML配置方便。盡管如此,,Memcached現(xiàn)在已經(jīng)成了服務(wù)器架構(gòu)里不可或缺的一部分,! 一、下載 1.Libevent 簡(jiǎn)單的說(shuō)就是一個(gè)事件觸發(fā)的網(wǎng)絡(luò)庫(kù),,Memcached離不開(kāi)它,。 Shell代碼
2.Memcached 今天的主角 Shell代碼
二、安裝 1.Libevent 解壓縮 Shell代碼
編譯,、安裝 Shell代碼
這里一定要注意指定--prefix,,后面配置memcached的時(shí)候就有必要用到。 2.Memcached 解壓 Shell代碼
編譯,、安裝 Shell代碼
這里一定要指定libevent的路徑,,否則啟動(dòng)的時(shí)候就有找不到libevent的so文件的錯(cuò)誤! 啟動(dòng) Shell代碼
參數(shù) 引用 -p <num> TCP port number to listen on (default: 11211) -U <num> UDP port number to listen on (default: 11211, 0 is off) -l <addr> interface to listen on (default: INADDR_ANY, all addresses) <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 -d run as a daemon -u <username> assume identity of <username> (only when run as root) -m <num> max memory to use for items in megabytes (default: 64 MB) -M return error on memory exhausted (rather than removing items) -c <num> max simultaneous connections (default: 1024) -v verbose (print errors/warnings while in event loop) -P <file> save PID in <file>, only used with -d option 要關(guān)掉memcached Shell代碼
是否正常,?Telnet上去看看 Shell代碼
然后輸入 Shell代碼
接著就能看到: 引用 STAT pid 3021 STAT uptime 3621 STAT time 1331261509 STAT version 1.4.13 STAT libevent 2.0.17-stable STAT pointer_size 64 STAT rusage_user 0.000000 STAT rusage_system 0.000999 STAT curr_connections 6 STAT total_connections 7 STAT connection_structures 7 STAT reserved_fds 20 STAT cmd_get 0 STAT cmd_set 0 STAT cmd_flush 0 STAT cmd_touch 0 STAT get_hits 0 STAT get_misses 0 STAT delete_misses 0 STAT delete_hits 0 STAT incr_misses 0 STAT incr_hits 0 STAT decr_misses 0 STAT decr_hits 0 STAT cas_misses 0 STAT cas_hits 0 STAT cas_badval 0 STAT touch_hits 0 STAT touch_misses 0 STAT auth_cmds 0 STAT auth_errors 0 STAT bytes_read 72 STAT bytes_written 1038 STAT limit_maxbytes 52428800 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT threads 4 STAT conn_yields 0 STAT hash_power_level 16 STAT hash_bytes 524288 STAT hash_is_expanding 0 STAT expired_unfetched 0 STAT evicted_unfetched 0 STAT bytes 0 STAT curr_items 0 STAT total_items 0 STAT evictions 0 STAT reclaimed 0 END 上面狀況說(shuō)明Memcached服務(wù)正常,。 還可以試試get,、set、delete,、replace 引用 set foo 0 0 3 (保存命令)
bar (數(shù)據(jù)) STORED (結(jié)果) get foo (取得命令) VALUE foo 0 3 (數(shù)據(jù)) bar (數(shù)據(jù)) 輸入 Shell代碼
三、系統(tǒng)服務(wù) 參照Nginx的系統(tǒng)服務(wù),,自己寫了一個(gè)Memcached的系統(tǒng)服務(wù)腳本,。 先構(gòu)建/etc/init.d/memcahed這個(gè)文件,然后賦予其可執(zhí)行權(quán)限: Shell代碼
memcached腳本如下: Shell代碼
注意這幾行配置,,請(qǐng)根據(jù)實(shí)際情況配置memcached執(zhí)行文件路徑,,以及Memcached使用內(nèi)存大小: 引用 memcached_path="/usr/local/bin/memcached"
memcached_memory="1024" 追加為系統(tǒng)服務(wù): Shell代碼
然后就可以通過(guò)service memcached start|stop|status|restart控制memcached了,! 四,、常規(guī)錯(cuò)誤 一開(kāi)始沒(méi)有指定libevent路徑安裝memcached的時(shí)候,啟動(dòng)memcached就報(bào)這個(gè)錯(cuò)誤: 引用 memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
其實(shí)這個(gè)文件就在/usr/lib下,。錯(cuò)就錯(cuò)在Linux是64bit系統(tǒng),,如果沒(méi)有指定libevent路徑,memcached就會(huì)去/usr/lib64下去找,。 找到這個(gè)文件 Shell代碼
引用 libevent-2.0.so: /usr/lib/libevent-2.0.so.5
Shell代碼
提示找不到libevent-2.0.so.5 引用 linux-vdso.so.1 => (0x00007fff41dfd000)
libevent-2.0.so.5 => not found librt.so.1 => /lib64/librt.so.1 (0x0000003c94a00000) libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c93a00000) libc.so.6 => /lib64/libc.so.6 (0x0000003c92e00000) /lib64/ld-linux-x86-64.so.2 (0x0000003c92a00000) 定位 Shell代碼
引用 19905: find library=libevent-2.0.so.5 [0]; searching
19905: search path=/usr/lib/lib/tls/x86_64:/usr/lib/lib/tls:/usr/lib/lib/x86_64:/usr/lib/lib (RPATH from file /usr/local/bin/memcached) 19905: trying file=/usr/lib/lib/tls/x86_64/libevent-2.0.so.5 19905: trying file=/usr/lib/lib/tls/libevent-2.0.so.5 19905: trying file=/usr/lib/lib/x86_64/libevent-2.0.so.5 19905: trying file=/usr/lib/lib/libevent-2.0.so.5 19905: search cache=/etc/ld.so.cache 19905: search path=/lib64/tls/x86_64:/lib64/tls:/lib64/x86_64:/lib64:/usr/lib64/tls/x86_64:/usr/lib64/tls:/usr/lib64/x86_64:/usr/lib64 (system search path) 19905: trying file=/lib64/tls/x86_64/libevent-2.0.so.5 19905: trying file=/lib64/tls/libevent-2.0.so.5 19905: trying file=/lib64/x86_64/libevent-2.0.so.5 19905: trying file=/lib64/libevent-2.0.so.5 19905: trying file=/usr/lib64/tls/x86_64/libevent-2.0.so.5 19905: trying file=/usr/lib64/tls/libevent-2.0.so.5 19905: trying file=/usr/lib64/x86_64/libevent-2.0.so.5 19905: trying file=/usr/lib64/libevent-2.0.so.5 19905: /usr/local/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory 注意這句: 引用 19905: trying file=/usr/lib64/libevent-2.0.so.5
做個(gè)軟連接 Shell代碼
再試試: Shell代碼
這回找到了,! 引用 linux-vdso.so.1 => (0x00007fffffef6000)
libevent-2.0.so.5 => /usr/lib64/libevent-2.0.so.5 (0x00002b5608a26000) librt.so.1 => /lib64/librt.so.1 (0x0000003c94a00000) libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003c93a00000) libc.so.6 => /lib64/libc.so.6 (0x0000003c92e00000) /lib64/ld-linux-x86-64.so.2 (0x0000003c92a00000) 五、監(jiān)控 可以在服務(wù)器上配置一個(gè)PHP頁(yè)面來(lái)監(jiān)測(cè)Memcached的情況,, 下載MemcachePHP 配置也比較簡(jiǎn)單,,主要包括賬戶配置,以及Memcached Server地址配置,。 Php代碼
常見(jiàn)錯(cuò)誤: 引用 Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /home/usr/local/apache.../memcache.php on line 726
在memcache.php頂端加上“date_default_timezone_set('Asia/Hong_Kong');”即可,,具體地域設(shè)置參考:http://www./manual/zh/datetime.configuration.php#ini.date.timezone 如果不方便搭建PHP服務(wù),可以使用Perl腳本memcache-top 修改@default_instances或使用--instances參數(shù): Shell代碼
先到這里,,后續(xù)做壓力測(cè)試,,Java客戶端開(kāi)發(fā),Spring系統(tǒng)集成等,。 |
|
來(lái)自: 李副營(yíng)長(zhǎng) > 《memcached》