环境: ctenos
数据库: mysql 5.5
程序在编译成后, 运行执行文件报如下错误:
./bin/logServer: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory
查看系统相应的文件夹下面都有相应的库文件, 我就纳闷了, 编译能成功, 为啥不能执行呢? 在网上搜查了一下,使用软链接后:
[root@bogon var]# ln -s /usr/local/mysql/lib/mysql/libmysqlclient.so.16 /usr/lib/libmysqlclient.so.16
后, 仍然不能执行。
修改vi /etc/ld.so.conf文件下面添加如下一行代码
/usr/lib/mysql
运行ldconfig后仍然执行不了。
利用strace跟踪一下执行文件
[root@002_server]$
[root@002_server]$
[root@002_server]$strace -f ./bin/logServer
execve("./bin/logServer", ["./bin/logServer"], [/* 30 vars */]) = 0
brk(0) = 0xbec6000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x2b2f13019000
uname({sys="Linux", node="002_server", ...}) = 0
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=175710, ...}) = 0
mmap(NULL, 175710, PROT_READ, MAP_PRIVATE, 3, 0) = 0x2b2f1301a000
close(3) = 0
open("/lib64/tls/x86_64/libmysqlclient.so.16", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/lib64/tls/x86_64", 0x7fffb1509ff0) = -1 ENOENT (No such file or directory)
open("/lib64/tls/libmysqlclient.so.16", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/lib64/tls", 0x7fffb1509ff0) = -1 ENOENT (No such file or directory)
open("/lib64/x86_64/libmysqlclient.so.16", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/lib64/x86_64", 0x7fffb1509ff0) = -1 ENOENT (No such file or directory)
open("/lib64/libmysqlclient.so.16", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/lib64", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
open("/usr/lib64/tls/x86_64/libmysqlclient.so.16", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/tls/x86_64", 0x7fffb1509ff0) = -1 ENOENT (No such file or directory)
open("/usr/lib64/tls/libmysqlclient.so.16", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/tls", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/usr/lib64/x86_64/libmysqlclient.so.16", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/lib64/x86_64", 0x7fffb1509ff0) = -1 ENOENT (No such file or directory)
open("/usr/lib64/libmysqlclient.so.16", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/lib64", {st_mode=S_IFDIR|0755, st_size=102400, ...}) = 0
writev(2, [{"./bin/logServer", 15}, {": ", 2}, {"error while loading shared libra"..., 36}, {": ", 2}, {"libmysqlclient.so.16", 20}, {": ", 2}, {"cannot open shared object file", 30}, {": ", 2}, {"No such file or directory", 25}, {"/n", 1}], 10./bin/logServer: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory
) = 135
exit_group(127) = ?
发现确实是libmysqlclient.so.16库文件找不到, 仔细想想, 编译能通过,为什么执行不了呢? 难道是环境变量的原因。。。
那就死马当作活马医吧。 执行如下命令:
[root@002_server]$export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/services/mysql/lib/mysql/
再执行我的可执行文件:
[root@002_server]$./bin/logServer
[root@002_server]$ps -ef|grep logServer
root 25403 21884 99 20:45 pts/1 00:45:53 ./bin/logServer
501 27765 25407 0 21:31 pts/5 00:00:00 grep logServer
OK, 问题解决。