简体   繁体   English

如何告诉python使用哪个版本的libmysqlclient.so?

[英]How can I tell python which version of libmysqlclient.so to use?

I'm running a python script on a shared hosting server which until this morning had MySQL version 4. Now it has version 5. My python script can no longer connect to MySQL, as it can't find libmysqlclient_r.so.14: 我正在共享托管服务器上运行python脚本,直到今天早上它具有MySQL版本4。现在它具有版本5。我的python脚本不再能够连接到MySQL,因为它找不到libmysqlclient_r.so.14:

$ python my_script.py
Traceback (most recent call last):
File "my_script.py", line 6, in ?
import MySQLdb
 File "/home/lib/python2.4/site-packages/PIL-1.1.6-py2.4-linux-i686.egg/__init__.py", line 19, in ?

 File "build/bdist.linux-i686/egg/_mysql.py", line 7, in ?
 File "build/bdist.linux-i686/egg/_mysql.py", line 6, in __bootstrap__
ImportError: libmysqlclient_r.so.14: cannot open shared object file: No such file or directory

There are various other versions of libmysqlclient in /usr/lib: / usr / lib中还有其他各种版本的libmysqlclient:

/usr/lib/libmysqlclient.so.15
/usr/lib/libmysqlclient.so.14
/usr/lib/mysql/libmysqlclient.la
/usr/lib/mysql/libmysqlclient.so
/usr/lib/mysql/libmysqlclient_r.so
/usr/lib/mysql/libmysqlclient_r.a
/usr/lib/mysql/libmysqlclient_r.la
/usr/lib/mysql/libmysqlclient.a
/usr/lib/libmysqlclient.so
/usr/lib/libmysqlclient_r.so
/usr/lib/libmysqlclient_r.so.15
/usr/lib/libmysqlclient_r.so.15.0.0
/usr/lib/libmysqlclient.so.15.0.0

So my question is this: how can I tell python (version 2.4.3) which version of libmysqlclient to use? 所以我的问题是:如何告诉python(版本2.4.3)使用哪个版本的libmysqlclient?

You can't tell the dynamic linker which version of a library to use, because the SONAME (full name of the library + interface) is part of the binary. 您不能告诉动态链接器使用哪个版本的库,因为SONAME(库+接口的全名)是二进制文件的一部分。

In your case, you can try to upload libmysqlclient_r.so.14 to the host and set LD_LIBRARY_PATH accordingly, so tell the dynamic linker which directories to search additionally to the system dirs when resolving shared objects. 在您的情况下,您可以尝试将libmysqlclient_r.so.14上传到主机并相应地设置LD_LIBRARY_PATH ,因此在解析共享库时,告诉动态链接器要在系统目录中另外搜索哪些目录。

You can use ldd to see if it LD_LIBRARY_PATH works: 您可以使用ldd来查看LD_LIBRARY_PATH有效:

$ ldd $path_to/_mysql.so
...
libmysqlclient_r.so.14 => $path_to_lib/libmysqlclient_r.so.14
...

Otherwise, there will be an error message about unresolved shared objects. 否则,将出现关于未解析的共享库的错误消息。

Of course that can only be a temporary fix until you rebuild MySQLdb to use the new libraries. 当然,这只能是一个临时修复,除非您重建MySQLdb以使用新的库。

You will have to recompile python-mysql (aka MySQLdb) to get it to link to the new version of libmysqlclient. 您将不得不重新编译python-mysql(又名MySQLdb),以使其链接到新版本的libmysqlclient。

If your host originally set up the environment rather than you compiling it, you'll have to pester them. 如果您的主机最初是设置环境而不是您对其进行编译,则必须对它们进行处理。

/usr/lib/libmysqlclient.so.14 /usr/lib/libmysqlclient.so.14

This looks like a remnant of the old libmysqlclient, and should be removed. 这看起来像旧的libmysqlclient的残余,应该将其删除。 The _r and .a (static) versions are gone and you don't really want a mixture of libraries still around, it will only risk confusing automake. _r和.a(静态)版本已经不存在了,您实际上并不希望混合使用各种库,只会冒混淆自动制作的风险。

Whilst you could make a symbolic link from libmysqlclient_r.so.14 to .15, that'd only work if the new version of the client happened to have the same ABI for the functions you wanted to use as the old - and that's pretty unlikely, as that's the whole point of changing the version number. 虽然您可以建立从libmysqlclient_r.so.14到.15的符号链接,但这仅在新版本的客户端恰好具有与您希望使用的旧功能相同的ABI时才有效-这几乎是不可能的,因为这是更改版本号的重点。

一种解决方案是将PYTHONPATH环境变量设置为具有某些本地目录,然后复制(或链接,我想是)您想要的mysql lib的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM