memcached已经火了好多年了,现在网上关于memcached的资源相当多了,我就不浪费话语了。干脆写一个实战系列,坚持一切用实施说话。
环境介绍
Linux虚拟机
内核信息
[root@hadoop1 ~]# uname -a
Linux hadoop1 2.6.32-358.el6.i686
内存:1G
安装过程
1.准备编译环境,安装必须的gcc,make工具,如果没有安装yum,最好安装下。网上有很多共享的yum源。
2.下载最新版本的libevent
1
|
# wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libevent-2.0.22-stable.tar.gz
|
3.解压libevent对应压缩包
1
|
# tar zxvf libevent-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
|
$ wget http:
//memcached
.org
/latest
|
5.解压memcached对应压缩包
1
|
$
tar
zxvf memcached-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@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
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=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
...
|
首先确认建立软连接
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
-u hadoop
|
确认memcached是否启动成功
1
2
3
4
5
6
|
$
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 确认下
1
2
3
4
5
6
7
8
|
$ 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的启动脚本
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@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
配置自启动
1
2
3
|
[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
1
|
[root@hadoop1 yum.repos.d]$ yum
install
nc
|
memcached 自带的stats命令,对搜索不太友好。为了很好的搜索,借助nc工具。
比如,想查找connection相关的参数
1
2
3
4
|
[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使用形式,个人参考网上资源,简单罗列下
1
2
3
|
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.
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/1835163,如需转载请自行联系原作者