您好,登錄后才能下訂單哦!
memcached已經火了好多年了,現在網上關于memcached的資源相當多了,我就不浪費話語了。干脆寫一個實戰系列,堅持一切用實施說話。
環境介紹
Linux虛擬機
內核信息
[root@hadoop1 ~]# uname -a
Linux hadoop1 2.6.32-358.el6.i686
內存:1G
安裝過程
1.準備編譯環境,安裝必須的gcc,make工具,如果沒有安裝yum,最好安裝下。網上有很多共享的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對應壓縮包
# 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 確認安裝結果 $ ls -al /usr/local/lib |grep libevent
4.下載最新版本的memcached
$ wget http://memcached.org/latest
5.解壓memcached對應壓縮包
$ 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
解決思路
##跟蹤運行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 ...
首先確認建立軟連接
$ 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參數
$ /usr/local/memcached/bin/memcached -u hadoop
確認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 確認下
$ 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版本已經成功在機器上安裝。個人感覺最麻煩的地方,就是處理libevent。
參考了很多資源,才把問題解決。
memcached設置自啟動
為了方便,現將memcached執行變更下權限
最簡單的啟動方式
只需在/etc/rc.d/rc.local中加入一行
/usr/local/memcached/bin/memcached -d -m 20 -p 11211 -u hadoop
注意
-d:設置為后臺進程
-u:指向用戶
-p:端口
-m:內存
推薦的方式
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相關的參數
[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使用形式,個人參考網上資源,簡單羅列下
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
接下來,演練的內容是通過telnet命令行和JAVA 客戶端工具,訪問memcachd.
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。