您好,登錄后才能下訂單哦!
memcached已經(jīng)火了好多年了,現(xiàn)在網(wǎng)上關(guān)于memcached的資源相當(dāng)多了,我就不浪費(fèi)話語了。干脆寫一個實戰(zhàn)系列,堅持一切用實施說話。
環(huán)境介紹
Linux虛擬機(jī)
內(nèi)核信息
[root@hadoop1 ~]# uname -a
Linux hadoop1 2.6.32-358.el6.i686
內(nèi)存:1G
安裝過程
1.準(zhǔn)備編譯環(huán)境,安裝必須的gcc,make工具,如果沒有安裝yum,最好安裝下。網(wǎng)上有很多共享的yum源。
2.下載最新版本的libevent
# wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz
3.解壓libevent對應(yīng)壓縮包
# tar zxvf libevent-2.0.22-stable.tar.gz
解壓之后的文件路徑:/u01/software/libevent-2.0.22-stable
3.編譯libevent
$ cd /u01/software/libevent-2.0.22-stable $ ./configure -prefix=/usr/local/libevent $ make $ make install 確認(rèn)安裝結(jié)果 $ ls -al /usr/local/lib |grep libevent
4.下載最新版本的memcached
$ wget http://memcached.org/latest
5.解壓memcached對應(yīng)壓縮包
$ tar zxvf memcached-1.4.29.tar.gz
解壓之后的文件路徑:/u01/software/memcached-1.4.29
6.編譯memcached
$ cd /u01/software/memcached-1.4.29 $ [root@hadoop1 memcached-1.4.29]# ./configure -help `configure' configures memcached 1.4.29 to adapt to many kinds of systems. Usage: ./configure [OPTION]... [VAR=VALUE]... Configuration: -h, --help display this help and exit --help=short display options specific to this package --help=recursive display the short help of all the included packages -V, --version display version information and exit -q, --quiet, --silent do not print `checking ...' messages --cache-file=FILE cache test results in FILE [disabled] -C, --config-cache alias for `--cache-file=config.cache' -n, --no-create do not create output files --srcdir=DIR find the sources in DIR [configure dir or `..'] Installation directories: --prefix=PREFIX install architecture-independent files in PREFIX [/usr/local] ... Fine tuning of the installation directories: --bindir=DIR user executables [EPREFIX/bin] --sbindir=DIR system admin executables [EPREFIX/sbin] --libexecdir=DIR program executables [EPREFIX/libexec] --sysconfdir=DIR read-only single-machine data [PREFIX/etc] --sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com] --localstatedir=DIR modifiable single-machine data [PREFIX/var] --libdir=DIR object code libraries [EPREFIX/lib] --includedir=DIR C header files [PREFIX/include] --oldincludedir=DIR C header files for non-gcc [/usr/include] --datarootdir=DIR read-only arch.-independent data root [PREFIX/share] --datadir=DIR read-only architecture-independent data [DATAROOTDIR] --infodir=DIR info documentation [DATAROOTDIR/info] --localedir=DIR locale-dependent data [DATAROOTDIR/locale] --mandir=DIR man documentation [DATAROOTDIR/man] --docdir=DIR documentation root [DATAROOTDIR/doc/memcached] --htmldir=DIR html documentation [DOCDIR] --dvidir=DIR dvi documentation [DOCDIR] --pdfdir=DIR pdf documentation [DOCDIR] --psdir=DIR ps documentation [DOCDIR] Optional Packages: --with-PACKAGE[=ARG] use PACKAGE [ARG=yes] --without-PACKAGE do not use PACKAGE (same as --with-PACKAGE=no) --with-libevent=PATH Specify path to libevent installation # ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/lib/ # make # make install
這時候memcached安裝成功了
啟動memcached
$ /usr/local/memcached/bin/memcached 報錯
error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file: No such file or directory
解決思路
##跟蹤運(yùn)行memcached所有加載庫文件的路徑
$ LD_DEBUG=libs LD_DEBUG=libs ./memcached -v 15862: find library=libevent-2.0.so.5 [0]; searching 15862: search cache=/etc/ld.so.cache 15862: search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib (system search path) 15862: trying file=/lib/tls/i686/sse2/libevent-2.0.so.5 15862: trying file=/lib/tls/i686/libevent-2.0.so.5 15862: trying file=/lib/tls/sse2/libevent-2.0.so.5 15862: trying file=/lib/tls/libevent-2.0.so.5 15862: trying file=/lib/i686/sse2/libevent-2.0.so.5 15862: trying file=/lib/i686/libevent-2.0.so.5 15862: trying file=/lib/sse2/libevent-2.0.so.5 15862: trying file=/lib/libevent-2.0.so.5 ...
首先確認(rèn)建立軟連接
$ ls /usr/local/lib/libevent-2.0.so.5 /usr/local/lib/libevent-2.0.so.5 $ ln -s /usr/local/lib/libevent-2.0.so.5 /usr/lib/i686/libevent-2.0.so.5
如果以root用戶登陸,必須指定-u參數(shù)
$ /usr/local/memcached/bin/memcached -u hadoop
確認(rèn)memcached是否啟動成功
$ ps -ef |grep memcached hadoop 15891 4034 0 22:45 pts/4 00:00:00 /usr/local/memcached/bin/memcached -u hadoop root 15899 15561 0 22:45 pts/5 00:00:00 grep memcached # netstat -tlnp |grep memcached tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 15891/memcached tcp 0 0 :::11211 :::* LISTEN 15891/memcached
最后telnet 確認(rèn)下
$ telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. stats STAT pid 15891 STAT uptime 86 STAT time 1470494793
OK。至此memcached1.4.9版本已經(jīng)成功在機(jī)器上安裝。個人感覺最麻煩的地方,就是處理libevent。
參考了很多資源,才把問題解決。
memcached設(shè)置自啟動
為了方便,現(xiàn)將memcached執(zhí)行變更下權(quán)限
最簡單的啟動方式
只需在/etc/rc.d/rc.local中加入一行
/usr/local/memcached/bin/memcached -d -m 20 -p 11211 -u hadoop
注意
-d:設(shè)置為后臺進(jìn)程
-u:指向用戶
-p:端口
-m:內(nèi)存
推薦的方式
2.1.拷貝memcached源碼包的memcached.sysv拷貝到/etc/init.d,做為memcached的啟動腳本
$ cp /u01/software/memcached-1.4.29/scripts/memcached.sysv /etc/init.d/memcached
[root@hadoop1 bin]# vi /etc/init.d/memcached #! /bin/sh # # chkconfig: - 55 45 # description: The memcached daemon is a network memory cache service. # processname: memcached # config: /etc/sysconfig/memcached # Source function library. . /etc/rc.d/init.d/functions PORT=11211 USER=nobody MAXCONN=1024 CACHESIZE=64 OPTIONS="" if [ -f /etc/sysconfig/memcached ];then . /etc/sysconfig/memcached fi # Check that networking is up. if [ "$NETWORKING" = "no" ] then exit 0 fi RETVAL=0 prog="memcached" start () { echo -n $"Starting $prog: " # insure that /var/run/memcached has proper permissions chown $USER /var/run/memcached daemon memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached/memcached.pid $OPTIONS RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/memcached } stop () { echo -n $"Stopping $prog: " killproc memcached RETVAL=$? echo if [ $RETVAL -eq 0 ] ; then rm -f /var/lock/subsys/memcached rm -f /var/run/memcached/memcached.pid fi } restart () { stop start } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status memcached ;; restart|reload) restart ;; condrestart) [ -f /var/lock/subsys/memcached ] && restart || : ;; *) echo $"Usage: $0 {start|stop|status|restart|reload|condrestart}" exit 1 esac exit $?
修改下面這一句(其實就將memcached 命令指向咱們的全路徑)
daemon memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached/memcached.pid $OPTIONS
daemon /usr/local/memcached/bin/memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached/memcached.pid $OPTIONS
配置自啟動
[root@hadoop1 bin]$ chkconfig memcached on [root@hadoop1 bin]$ chkconfig |grep memcached memcached 0:off 1:off 2:on 3:on 4:on 5:on 6:off
安裝nc
[root@hadoop1 yum.repos.d]$ yum install nc
memcached 自帶的stats命令,對搜索不太友好。為了很好的搜索,借助nc工具。
比如,想查找connection相關(guān)的參數(shù)
[root@hadoop1 yum.repos.d]# echo stats | nc 127.0.0.1 11211 |grep connection STAT curr_connections 10 STAT total_connections 13 STAT connection_structures 11
配合nc使用形式,個人參考網(wǎng)上資源,簡單羅列下
watch "echo stats | nc 127.0.0.1 11211" printf "stats\r\n" | nc 127.0.0.1 11211 echo stats | nc 127.0.0.1 11211
------------------------------------------------------------
The End
接下來,演練的內(nèi)容是通過telnet命令行和JAVA 客戶端工具,訪問memcachd.
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。