memcached演练(1) 搭建memcached服务
memcached已经火了好多年了,现在网上关于memcached的资源相当多了,我就不浪费话语了。干脆写一个实战系列,坚持一切用实施说话。 环境介绍 Linux虚拟机 内核信息 [root@hadoop1 ~]# uname -a Linux hadoop1 2.6.32-358.el6.i686 内存:1G 安装过程 1.准备编译环境,安装必须的gcc,make工具,如果没有安装yum,最好安装下。网上有很多共享的yum源。 2.下载最新版本的libevent 1 #wgethttps://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz 3.解压libevent对应压缩包 1 #tarzxvflibevent-2.0.22-stable.tar.gz 解压之后的文件路径:/u01/software/libevent-2.0.22-stable 3.编译libevent 1 2 3 4 5 6 $ 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 1 $wgethttp: //memcached .org /latest 5.解压memcached对应压缩包 1 $ tar zxvfmemcached-1.4.29. tar .gz 解压之后的文件路径:/u01/software/memcached-1.4.29 6.编译memcached 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 $ cd /u01/software/memcached-1 .4.29 $[root@hadoop1memcached-1.4.29] #./configure-help `configure'configuresmemcached1.4.29toadapttomanykindsofsystems. Usage:. /configure [OPTION]...[VAR=VALUE]... Configuration: -h,--helpdisplaythishelpand exit --help=shortdisplayoptionsspecifictothispackage --help=recursivedisplaytheshorthelpofalltheincludedpackages -V,--versiondisplayversioninformationand exit -q,--quiet,--silent do notprint`checking...'messages --cache- file =FILEcache test results in FILE[disabled] -C,--config-cache alias for `--cache- file =config.cache' -n,--no-create do notcreateoutputfiles --srcdir=DIR find thesources in DIR[configure dir or`..'] Installationdirectories: --prefix=PREFIX install architecture-independentfiles in PREFIX [ /usr/local ] ... Finetuningoftheinstallationdirectories: --bindir=DIRuserexecutables[EPREFIX /bin ] --sbindir=DIRsystemadminexecutables[EPREFIX /sbin ] --libexecdir=DIRprogramexecutables[EPREFIX /libexec ] --sysconfdir=DIR read -onlysingle-machinedata[PREFIX /etc ] --sharedstatedir=DIRmodifiablearchitecture-independentdata[PREFIX /com ] --localstatedir=DIRmodifiablesingle-machinedata[PREFIX /var ] --libdir=DIRobjectcodelibraries[EPREFIX /lib ] --includedir=DIRCheaderfiles[PREFIX /include ] --oldincludedir=DIRCheaderfiles for non-gcc[ /usr/include ] --datarootdir=DIR read -onlyarch.-independentdataroot[PREFIX /share ] --datadir=DIR read -onlyarchitecture-independentdata[DATAROOTDIR] --infodir=DIRinfodocumentation[DATAROOTDIR /info ] --localedir=DIRlocale-dependentdata[DATAROOTDIR /locale ] --mandir=DIR man documentation[DATAROOTDIR /man ] --docdir=DIRdocumentationroot[DATAROOTDIR /doc/memcached ] --htmldir=DIRhtmldocumentation[DOCDIR] --dvidir=DIRdvidocumentation[DOCDIR] --pdfdir=DIRpdfdocumentation[DOCDIR] --psdir=DIR ps documentation[DOCDIR] OptionalPackages: --with-PACKAGE[=ARG]usePACKAGE[ARG= yes ] --without-PACKAGE do notusePACKAGE(sameas--with-PACKAGE=no) --with-libevent=PATHSpecifypathtolibeventinstallation #./configure--prefix=/usr/local/memcached--with-libevent=/usr/local/lib/ #make #makeinstall 这时候memcached安装成功了 启动memcached 1 $ /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所有加载库文件的路径 1 2 3 4 5 6 7 8 9 10 11 12 13 $LD_DEBUG=libsLD_DEBUG=libs. /memcached - v 15862: find library=libevent-2.0.so.5[0];searching 15862:searchcache= /etc/ld .so.cache 15862:searchpath= /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 (systemsearchpath) 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 ... 首先确认建立软连接 1 2 3 4 $ 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参数 1 $ /usr/local/memcached/bin/memcached -uhadoop 确认memcached是否启动成功 1 2 3 4 5 6 $ ps -ef| grep memcached hadoop158914034022:45pts /4 00:00:00 /usr/local/memcached/bin/memcached -uhadoop root1589915561022:45pts /5 00:00:00 grep memcached #netstat-tlnp|grepmemcached tcp000.0.0.0:112110.0.0.0:*LISTEN15891 /memcached tcp00:::11211:::*LISTEN15891 /memcached 最后telnet 确认下 1 2 3 4 5 6 7 8 $telnet127.0.0.111211 Trying127.0.0.1... Connectedto127.0.0.1. Escapecharacteris '^]' . stats STATpid15891 STATuptime86 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的启动脚本 1 $ cp /u01/software/memcached-1 .4.29 /scripts/memcached .sysv /etc/init .d /memcached 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 [root@hadoop1bin] #vi/etc/init.d/memcached #!/bin/sh # #chkconfig:-5545 #description:Thememcacheddaemonisanetworkmemorycacheservice. #processname:memcached #config:/etc/sysconfig/memcached #Sourcefunctionlibrary. . /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 #Checkthatnetworkingisup. if [ "$NETWORKING" = "no" ] then exit 0 fi RETVAL=0 prog= "memcached" start(){ echo -n$ "Starting$prog:" #insurethat/var/run/memcachedhasproperpermissions chown $USER /var/run/memcached daemonmemcached-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:" killprocmemcached RETVAL=$? echo if [$RETVAL- eq 0]; then rm -f /var/lock/subsys/memcached rm -f /var/run/memcached/memcached .pid fi } restart(){ stop start } #Seehowwewerecalled. case "$1" in start) start ;; stop) stop ;; status) statusmemcached ;; restart|reload) restart ;; condrestart) [-f /var/lock/subsys/memcached ]&&restart||: ;; *) echo $ "Usage:$0{start|stop|status|restart|reload|condrestart}" exit 1 esac exit $? 修改下面这一句(其实就将memcached 命令指向咱们的全路径) daemonmemcached-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 配置自启动 1 2 3 [root@hadoop1bin]$chkconfigmemcachedon [root@hadoop1bin]$chkconfig| grep memcached memcached0:off1:off2:on3:on4:on5:on6:off 安装nc 1 [root@hadoop1yum.repos.d]$yum install nc memcached 自带的stats命令,对搜索不太友好。为了很好的搜索,借助nc工具。 比如,想查找connection相关的参数 1 2 3 4 [root@hadoop1yum.repos.d] #echostats|nc127.0.0.111211|grepconnection STATcurr_connections10 STATtotal_connections13 STATconnection_structures11 配合nc使用形式,个人参考网上资源,简单罗列下 1 2 3 watch "echostats|nc127.0.0.111211" printf "stats\r\n" |nc127.0.0.111211 echo stats|nc127.0.0.111211 ------------------------------------------------------------ The End 接下来,演练的内容是通过telnet命令行和JAVA 客户端工具,访问memcachd. 本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/1835163,如需转载请自行联系原作者