Memcache

Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libeventlibevent的最新版本是libevent-1.3。(如果你的系统已经安装了libevent,可以不用安装)

1.先安装libevent。这个东西在配置时需要指定一个安装路径,即./configure �Cprefix=/usr/local/libevent/;然后make;然后make install
2.再安装memcached,只是需要在配置时需要指定libevent的安装路径即./configure �Cwith-libevent=/usr/local/libevent;然后make;然后make install
这样就完成了LinuxMemcache服务器端的安装。详细的方法如下:


[plain] view plaincopy

  1. # cd /usr/local/src/tarbag  

1.先安装libevent


[plain] view plaincopy

  1. # tar zxvf libevent-1.4.13-stable.tar.gz -C ../software  

  2. # cd ../software/libevent-1.4.13-stable  

  3. # ./configure �Cprefix=/usr/local/libevent  

  4. # make  

  5. # make install  

  6. #echo "/usr/local/libevent/lib/" >> /etc/ld.so.conf  

  7. #ldconfig  


2.测试libevent是否安装成功:


[plain] view plaincopy

  1. # ls -al /usr/local/libevent/lib | grep libevent  

  2. lrwxrwxrwx  1 root root     21 01-15 10:28 libevent-1.4.so.2 -> libevent-1.4.so.2.1.3  

  3. -rwxr-xr-x  1 root root 308592 01-15 10:28 libevent-1.4.so.2.1.3  

  4. -rw-r--r--  1 root root 395670 01-15 10:28 libevent.a  

  5. lrwxrwxrwx  1 root root     26 01-15 10:28 libevent_core-1.4.so.2 -> libevent_core-1.4.so.2.1.3  

  6. -rwxr-xr-x  1 root root 110070 01-15 10:28 libevent_core-1.4.so.2.1.3  

  7. -rw-r--r--  1 root root 149672 01-15 10:28 libevent_core.a  

  8. -rwxr-xr-x  1 root root    866 01-15 10:28 libevent_core.la  

  9. lrwxrwxrwx  1 root root     26 01-15 10:28 libevent_core.so -> libevent_core-1.4.so.2.1.3  

  10. lrwxrwxrwx  1 root root     27 01-15 10:28 libevent_extra-1.4.so.2 -> libevent_extra-1.4.so.2.1.3  

  11. -rwxr-xr-x  1 root root 246915 01-15 10:28 libevent_extra-1.4.so.2.1.3  

  12. -rw-r--r--  1 root root 308154 01-15 10:28 libevent_extra.a  

  13. -rwxr-xr-x  1 root root    873 01-15 10:28 libevent_extra.la  

  14. lrwxrwxrwx  1 root root     27 01-15 10:28 libevent_extra.so -> libevent_extra-1.4.so.2.1.3  

  15. -rwxr-xr-x  1 root root    831 01-15 10:28 libevent.la  

  16. lrwxrwxrwx  1 root root     21 01-15 10:28 libevent.so -> libevent-1.4.so.2.1.3  

3.安装memcached,同时需要安装中指定libevent的安装位置:

[plain] view plaincopy

  1. # cd /usr/local/src/tarbag  

  2. # tar zxvf memcached-1.4.0.tar.gz -C ../software  

  3. # cd ../software/memcached-1.4.0  

  4. # ./configure  --prefix=/usr/local/memcached/ �Cwith-libevent=/usr/local/libevent  

  5. # make  

  6. # make install  

如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。

安装完成后会memcached的工作目录是/usr/local/memcached/

4测试是否成功安装memcached

[plain] view plaincopy

  1. # ls -al /usr/local/memcached/bin/*  

  2. -rwxr-xr-x  1 root root 237953 Jun 29 14:12 /usr/local/memcached/bin/memcached  


memcached的基本设置
1.启动Memcache的服务器端:

[plain] view plaincopy

  1. # /usr/local/memcached/bin/memcached -d -m 10 -u root -l 10.0.0.204 -p 12000 -c 256 -P /tmp/memcached.pid  

-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB
-u是运行Memcache的用户,我这里是root
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址192.168.51.207
-p是设置Memcache监听的端口,我这里设置了12000,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcachepid文件,我这里是保存在 /tmp/memcached.pid


2.如果要结束Memcache进程,执行:


[plain] view plaincopy

  1. # kill `cat /tmp/memcached.pid`  

也可以启动多个守护进程,不过端口不能重复。


最后,设置下开始启动:

[plain] view plaincopy

  1. #echo "/usr/local/memcached/bin/memcached -d -m 10 -u root -l 10.0.0.204 -p 12000 -c 256 -P /tmp/memcached.pid" >> /etc/rc.local  

启动memcache可能会发生的问题:



[plain] view plaincopy

  1. /usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-2.0.so.5: cannot open shared object file  


#一般对于这种依赖的库找不到的情况,在linux中可以通过设置LD_DEBUG环境变量来获得更多的信息



[plain] view plaincopy

  1. #LD_DEBUG=help ls  

  2. Valid options for the LD_DEBUG environment variable are:  

  3.   libs       display library search paths  

  4.  reloc      display relocation processing  

  5.  files      display progress for input file  

  6.  symbols    display symbol table processing  

  7.  bindings   display information about symbol binding  

  8.  versions   display version dependencies  

  9.  all        all previous options combined  

  10.  statistics display relocation statistics  

  11.  unused     determined unused DSOs  

  12.  help       display this help message and exit  

  13.  To direct the debugging output into a file instead of standard output  

  14. a filename can be specified using the LD_DEBUG_OUTPUT environment variable. 

这里由于是库文件依赖有问题,则使用libs参数


[plain] view plaincopy

  1. #LD_DEBUG=libs /usr/local/memcached/bin/memcached -h                                                              

  2.     13293:    find library=libevent-2.0.so.5 [0]; searching                                                                        

  3.     13293:     search cache=/etc/ld.so.cache  

  4.     13293:     search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/l)  

  5.     13293:      trying file=/lib/tls/i686/sse2/libevent-2.0.so.5                                                                   

  6.     13293:      trying file=/lib/tls/i686/libevent-2.0.so.5                                                                        

  7.     13293:      trying file=/lib/tls/sse2/libevent-2.0.so.5                                                                        

  8.     13293:      trying file=/lib/tls/libevent-2.0.so.5                                                                             

  9.     13293:      trying file=/lib/i686/sse2/libevent-2.0.so.5                                                                       

  10.     13293:      trying file=/lib/i686/libevent-2.0.so.5                                                                            

  11.     13293:      trying file=/lib/sse2/libevent-2.0.so.5                                                                            

  12.     13293:      trying file=/lib/libevent-2.0.so.5                                                                                

  13.     13293:      trying file=/usr/lib/tls/i686/sse2/libevent-2.0.so.5                                                               

  14.     13293:      trying file=/usr/lib/tls/i686/libevent-2.0.so.5                                                                    

  15.     13293:      trying file=/usr/lib/tls/sse2/libevent-2.0.so.5                                                                    

  16.     13293:      trying file=/usr/lib/tls/libevent-2.0.so.5                                                                         

  17.     13293:      trying file=/usr/lib/i686/sse2/libevent-2.0.so.5                                                                   

  18.     13293:      trying file=/usr/lib/i686/libevent-2.0.so.5                                                                       

  19.     13293:      trying file=/usr/lib/sse2/libevent-2.0.so.5                                                                        

  20.     13293:      trying file=/usr/lib/libevent-2.0.so.5                                                                              

  21.  /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  

可以看到在加载/usr/lib/libevent-2.0.so.5文件时出现了问题,先在系统中查找该文件


[plain] view plaincopy

  1. #find /usr/ -name " libevent-2.0.so*"  

  2. /usr/local/libevent/lib/libevent-2.0.so.5  

  3. /usr/local/libevent/lib/libevent-2.0.so.5.0.1  

#通过查找发现在/usr/local/libevent/lib目录下有该文件,通过软链接方式可以解决


[plain] view plaincopy

  1. # ln -s /usr/local/libevent/lib/libevent-2.0.so.5 /usr/lib/libeve


你可能感兴趣的:(linux,socket,服务器,local,software)