centos7 安装 python 的 mysql 驱动 mysqlclient,报错,请问如何解决?

2016-05-22 19:09:29 +08:00
 alfer
[root@centos02 mysqlclient-1.3.7]# python setup.py build
running build
running build_py
copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
running build_ext
building '_mysql' extension
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,3,7,'final',1) -D__version__=1.3.7 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -g -fstack-protector -m64 -fPIC -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing
In file included from /usr/include/python2.7/pyconfig.h:6:0,
from /usr/include/python2.7/Python.h:8,
from _mysql.c:40:
/usr/include/python2.7/pyconfig-64.h:1182:0: warning: "_POSIX_C_SOURCE" redefined [enabled by default]
#define _POSIX_C_SOURCE 200112L
^
In file included from /usr/include/sys/types.h:25:0,
from /usr/include/mysql/mysql.h:47,
from _mysql.c:30:
/usr/include/features.h:168:0: note: this is the location of the previous definition
# define _POSIX_C_SOURCE 200809L
^
In file included from /usr/include/python2.7/pyconfig.h:6:0,
from /usr/include/python2.7/Python.h:8,
from _mysql.c:40:
/usr/include/python2.7/pyconfig-64.h:1204:0: warning: "_XOPEN_SOURCE" redefined [enabled by default]
#define _XOPEN_SOURCE 600
^
In file included from /usr/include/sys/types.h:25:0,
from /usr/include/mysql/mysql.h:47,
from _mysql.c:30:
/usr/include/features.h:170:0: note: this is the location of the previous definition
# define _XOPEN_SOURCE 700
^
gcc -pthread -shared -Wl,-z,relro build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib64 -L/usr/lib64 -lmysqlclient -lpthread -lm -lrt -ldl -lpython2.7 -o build/lib.linux-x86_64-2.7/_mysql.so
/usr/bin/ld: cannot find -lmysqlclient
collect2: error: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
5454 次点击
所在节点    Python
7 条回复
janxin
2016-05-22 20:45:46 +08:00
/usr/bin/ld: cannot find -lmysqlclient

mysql-client 装了吗?
alfer
2016-05-22 21:28:24 +08:00
@janxin 装了,问题已经解决了。
alfer
2016-05-22 21:33:27 +08:00
这个问题发现是 CentOS Linux release 7.2.1511 系统中装的 mariadb-libs 和自己装的 MySQL-server-5.6.30 的冲突了,所以卸载 mariadb 后导致组建缺失(libmysqlclient.so.18),致使安装 mysqlclient 报错。
解决办法:先安装 mysqlclient ,再安装 mysql5.6
但是还有遗留问题:因为安装 mysql 和 centos7 自带的 mariadb 有冲突,导致两者不能共存,卸载 mariadb 后,安装好的 mysqlclient 依然不能正常使用,报错如下:
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>
File "build/bdist.linux-x86_64/egg/_mysql.py", line 7, in <module>
File "build/bdist.linux-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory
patrickstar
2016-05-22 21:53:13 +08:00
针对不同开发环境分别建立 python 虚拟环境(比如用 pyvenv ),不要安装到系统环境中去
msg7086
2016-05-23 01:41:30 +08:00
所以为什么要装 MySQL 呢……(
要装也建议打包成软件包然后再替换系统组件来着。
ToughGuy
2016-05-23 10:26:22 +08:00
yum -y install mariadb-devel python-devel
alfer
2016-10-19 09:33:19 +08:00
建议用 mariadb+mysqlclient ,安装方便,可以避免卸载 mariadb 后 mysql+mysqlclient 的少包问题

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/280476

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX