Memcached是高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。
Memcache官方网站:
下载 Memcache :
Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent
下载 libevent:
第一步:
先安装libevent:
# tar zxvf libevent-1.4.14b-stable.tar.gz
# cd libevent-1.4.14b-stable
# ./configure --prefix=/usr
# make
# make install
如果安装过程中,发现提示:
configure: error: no acceptable C compiler found in $PATH
需要安装C compiler,安装方法:
在Linux环境下只要能链接Internet就可以通过本地的yum源来安装,在终端下输入yum install gcc就可以,自动安装的。安装的时候需保证linux可以上网,因为需要网上下载安装包。
然后看看我们的libevent是否安装成功:
[root@hisme ~]# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 21 3月 2 13:17 libevent-1.4.so.2 -> libevent-1.4.so.2.1.2
-rwxr-xr-x 1 root root 280215 3月 2 13:17 libevent-1.4.so.2.1.2
-rw-r--r-- 1 root root 367094 3月 2 13:17 libevent.a
lrwxrwxrwx 1 root root 26 3月 2 13:17 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.2
-rwxr-xr-x 1 root root 107511 3月 2 13:17 libevent_core-1.4.so.2.1.2
-rw-r--r-- 1 root root 146924 3月 2 13:17 libevent_core.a
-rwxr-xr-x 1 root root 860 3月 2 13:17 libevent_core.la
lrwxrwxrwx 1 root root 26 3月 2 13:17 libevent_core.so -> libevent_core-1.4.so.2.1.2
lrwxrwxrwx 1 root root 27 3月 2 13:17 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.1.2
-rwxr-xr-x 1 root root 219655 3月 2 13:17 libevent_extra-1.4.so.2.1.2
-rw-r--r-- 1 root root 281482 3月 2 13:17 libevent_extra.a
-rwxr-xr-x 1 root root 867 3月 2 13:17 libevent_extra.la
lrwxrwxrwx 1 root root 27 3月 2 13:17 libevent_extra.so -> libevent_extra-1.4.so.2.1.2
-rwxr-xr-x 1 root root 825 3月 2 13:17 libevent.la
lrwxrwxrwx 1 root root 21 3月 2 13:17 libevent.so -> libevent-1.4.so.2.1.2
第二步:
都安装上了,再来安装memcache,同时需要安装中指定libevent的安装位置:
# cd ../
# tar zxvf memcached-1.4.5.tar.gz
# cd memcached-1.4.5
# ./configure --with-libevent=/usr
# make
# make install
如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。
安装完成后会把memcached放到 /usr/local/bin/memcached ,我们看以下是否安装了:
[root@hisme ~]# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 121249 3月 2 13:23 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 130179 3月 2 13:23 /usr/local/bin/memcached-debug
恩,安装完成了,现在我们看以下memcache的帮助:
[root@hisme ~]# /usr/local/bin/memcached -h
出现:error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory
原因是找不到libevent-1.4.so.2类库,解决办法如下:
使用LD_DEBUG=help ./memcached -v来确定 加载的类库路径,方法如下:
[root@hisme ~]# LD_DEBUG=libs ./memcached -v 2>&1 > /dev/null | less
则系统会显示:
linux:/local/memcached/bin # LD_DEBUG=libs ./memcached -v 2>&1 > /dev/null | less
20421: find library=libevent-1.4.so.2; searching
20421: search cache=/etc/ld.so.cache
20421: 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)
20421: trying file=/lib/tls/i686/sse2/libevent-1.4.so.2
20421: trying file=/lib/tls/i686/libevent-1.4.so.2
20421: trying file=/lib/tls/sse2/libevent-1.4.so.2
20421: trying file=/lib/tls/libevent-1.4.so.2
20421: trying file=/lib/i686/sse2/libevent-1.4.so.2
20421: trying file=/lib/i686/libevent-1.4.so.2
20421: trying file=/lib/sse2/libevent-1.4.so.2
20421: trying file=/lib/libevent-1.4.so.2
20421: trying file=/usr/lib/tls/i686/sse2/libevent-1.4.so.2
20421: trying file=/usr/lib/tls/i686/libevent-1.4.so.2
20421: trying file=/usr/lib/tls/sse2/libevent-1.4.so.2
20421: trying file=/usr/lib/tls/libevent-1.4.so.2
20421: trying file=/usr/lib/i686/sse2/libevent-1.4.so.2
20421: trying file=/usr/lib/i686/libevent-1.4.so.2
20421: trying file=/usr/lib/sse2/libevent-1.4.so.2
20421: trying file=/usr/lib/libevent-1.4.so.2
20421:
./memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory
我们看到,memcached会到很多地方去找,所以根据其它求,我们只需建一个软链接,指定到我们安装的类库上即可
方法如下:
[root@hisme ~]# ln -s /usr/local/lib/libevent-1.4.so.2 /lib/libevent-1.4.so.2
再测试是否安装成功:
[root@hisme ~]# /usr/local/bin/memcached -h
memcached 1.2.6
-p TCP port number to listen on (default: 11211)
-U UDP port number to listen on (default: 0, off)
-s unix socket path to listen on (disables network support)
-a access mask for unix socket, in octal (default 0700)
-l interface to listen on, default is INDRR_ANY
-d run as a daemon
-r maximize core file limit
-u assume identity of (only when run as root)
-m max memory to use for items in megabytes, default is 64 MB
-M return error on memory exhausted (rather than removing items)
-c max simultaneous connections, default is 1024
-k lock down all paged memory. Note that there is a
limit on how much memory you may lock. Trying to
allocate more than that would fail, so be sure you
set the limit correctly for the user you started
the daemon with (not for -u user;
under sh this is done with 'ulimit -S -l NUM_KB').
-v verbose (print errors/warnings while in event loop)
-vv very verbose (also print client commands/reponses)
-h print this help and exit
-i print memcached and libevent license
-b run a managed instanced (mnemonic: buckets)
-P save PID in , only used with -d option
-f chunk size growth factor, default 1.25
-n minimum space allocated for key+value+flags, default 48
出现上面描述,表示安装成功。
第三步:
启动一个Memcache的服务器端:
# /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.173 -p 12000 -c 256 -P /tmp/memcached.pid
-d选项是启动一个守护进程,-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,-u是运行Memcache的用户,我这里是root,-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址127.0.0.1,-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,-P是设置保存Memcache的pid文件,我这里是保存在/tmp/memcached.pid,如果要结束Memcache进程,执行:
# kill `cat /tmp/memcached.pid`
也可以启动多个守护进程,不过端口不能重复。
用 netstat -lp|grep memcached 命令可以查看 memcached 是否已经启动:
[root@hisme ~]# netstat -lp|grep memcached
tcp 0 0 127.0.0.1:12000 *:* LISTEN 9037/memcached
udp 0 0 127.0.0.1:12000 *:* 9037/memcached
说明 memcached 正在运行.
相关链接:
1. Memcached Google Code
2. Memcached Start Here
3. cenwenchu's blog
4. cenwenchu
5. Memcached Home Web Page
6. libwvent Home Web Page