日期:2014-05-16  浏览次数:20609 次

linux上启动Memcache报错:error while loading shared libraries: libevent-1.4.so.2

linux上启动Memcache报错:

[root@localhost memcached]# ./bin/memcached -d -m 2048 -p 11211 -u root
./bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory

原因一般有两个, 一个是操作系统里确实没有包含该共享库(lib*.so.*文件)或者共享库版本不对, 遇到这种情况那就去网上下载并安装上即可.

另外一个原因就是已经安装了该共享库, 但执行需要调用该共享库的程序的时候, 程序按照默认共享库路径找不到该共享库文件.

?

因为我已经安装了libevent,所以应该是程序按照默认共享路径库去找,但是没有找到导致的。

?

首先使用find命令找到libevent-1.4.so.2文件在哪儿

[root@localhost memcached]# find /usr -name libevent-1.4.so.2
/usr/libevent/lib/libevent-1.4.so.2

使用debug信息查看程序去哪里寻找共享文件库

LD_DEBUG=libs /usr/local/bin/memcached -v

其中/usr/local/bin/memcached代表我memcached执行程序

控制台输出结果如下:

[root@localhost memcached]# LD_DEBUG=libs /usr/local/bin/memcached -v
-bash: /usr/local/bin/memcached: No such file or directory
[root@localhost memcached]# LD_DEBUG=libs /usr/local/memcached/bin/memcached -v
      6513:     find library=libevent-1.4.so.2 [0]; searching
      6513:     search cache=/etc/ld.so.cache
      6513:     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)
      6513:       trying file=/lib/tls/i686/sse2/libevent-1.4.so.2
      6513:       trying file=/lib/tls/i686/libevent-1.4.so.2
      6513:       trying file=/lib/tls/sse2/libevent-1.4.so.2
      6513:       trying file=/lib/tls/libevent-1.4.so.2
      6513:       trying file=/lib/i686/sse2/libevent-1.4.so.2
      6513:       trying file=/lib/i686/libevent-1.4.so.2
      6513:       trying file=/lib/sse2/libevent-1.4.so.2
      6513:       trying file=/lib/libevent-1.4.so.2
      6513:       trying file=/usr/lib/tls/i686/sse2/libevent-1.4.so.2
      6513:       trying file=/usr/lib/tls/i686/libevent-1.4.so.2
      6513:       trying file=/usr/lib/tls/sse2/libevent-1.4.so.2
      6513:       trying file=/usr/lib/tls/libevent-1.4.so.2
      6513:       trying file=/usr/lib/i686/sse2/libevent-1.4.so.2
      6513:       trying file=/usr/lib/i686/libevent-1.4.so.2
      6513:       trying file=/usr/lib/sse2/libevent-1.4.so.2
      6513:       trying file=/usr/lib/libevent-1.4.so.2
      6513:    
/usr/local/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory

根据debug日志可以看到,程序只会去/lib 和/usr/lib下去寻找需要的共享链接库。

而我的libevent是安装在/usr/libevent/lib/下,所以memcache启动的时候并不知道该去这下面找,所以会报错

?

所以安装共享库后要注意共享库路径设置问题, 如下:

1) 如果共享库文件安装到了/lib或/usr/lib目录下, 那么需执行一下ldconfig命令

? ? ? ? ?ldconfig命令的用途, 主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下, 搜索出可共享的动态链接库(格式如lib*.so*), 进而创建出动态装入程序(ld.so)所需的连接和缓存文件. 缓存文件默认为/etc/ld.so.cache, 此文件保存已排好序的动态链接库名字列表.?

?

2) 如果共享库文件安装到了/usr/local/lib(很多开源的共享库都会安装到该目录下)或其它"非/lib或/usr/lib"目录下, 那么在执行ldconfig命令前, 还要把新共享库目录加入到共享库配置文件/etc/ld.so.conf中, 如下:

[root@localhost memcached]# cat /etc/ld.so.conf
include ld.so.conf.d/*.conf

如上所示:/etc/ld.so.conf配置文件中内容只有一行,

ld.so.conf.d/*.conf的意思就是包含ld.so.conf.d/目录下以.conf为后缀的文件

所以我们可以在/etc/ld.so.conf.d目录下新建一个libevent.conf的配置文件,然后把libevent安装路径配置好

我的libevent内容如下:

[root@localhost ld.so.conf.d]# cat libevent.conf
/usr/libevent/lib

配置完后执行以下ldconfig命令

[root@localhost ~]#ldconfig 

3) 如果共享库文件安装到了其它"非/lib或/usr/lib" 目录下, ?但是又不想在/etc/ld.so.conf中加路径(或者是没有权限加路径). 那可以export一个全局变量