下面解决两个问题 :
EnvironmentError: /usr/local/mysql/bin/mysql_config not found
和
/usr/bin/ld: cannot find -lmysqlclient
collect2: error: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
问题重现:
类似问题网址:
https://www.jianshu.com/p/71cf187598ce
[root@TENCENT64 /home/packge/third_package/mysql-python-mysqldb-2-c16ae20b964d1c3c951a96cb1497a021fe70da41]# python setup.py install
sh: /usr/local/mysql/bin/mysql_config: No such file or directory
Traceback (most recent call last):
File "setup.py", line 15, in <module>
metadata, options = get_config()
File "/home/packge/third_package/mysql-python-mysqldb-2-c16ae20b964d1c3c951a96cb1497a021fe70da41/setup_posix.py", line 41, in get_config
libs = mysql_config("libs_r")
File "/home/packge/third_package/mysql-python-mysqldb-2-c16ae20b964d1c3c951a96cb1497a021fe70da41/setup_posix.py", line 23, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
主要报错:
EnvironmentError: /usr/local/mysql/bin/mysql_config not found
由于是离线安装的mysqldb,下载网址:
https://github.com/farcepest/MySQLdb1
或者:
https://sourceforge.net/projects/mysql-python/(这个网站好像只能下对应系统版本的)
进入包后运行python setup.py install
1.问题可能原因:
a).python版本自动装了3.7的32 位,不兼容。
b).代码版本是在2.7的python上的。
c).pycharm在windows上,和linux环境有一定差异。
d).安装了其他的mysql版本,比如mariadb等等。
c).yum出现问题,只安装了一部分文件。
2.解决方案:
1.进行链接(找到config的地址,做一个软连接过去)
2.用rpm -e mariadb
(或mysql)完全卸载,
3.用rpm install mariadb
重装一下。
3.解决思路:
哪里缺少找哪里。
既然缺少了/usr/local/mysql/bin/mysql_config,就找下哪里有:
find / -name mysql_config
看一下其中一个,
ll /usr/bin/mysql_config
并没有缺少:
查看
ll /usr/local/mysql/bin/mysql_config
发现没有:
看一下是谁有这个东西:
yum provides "*mysql_config*"
yum的provides : Find what package provides the given value
查看包是否已经被安装上了:
rpm -qa | grep mariadb-devel-5.5.50-1.el7_2.x86_64
是这个包提供的"*mysql_config*"
,同时这个包也已经安装了,说明安装过程可能有问题。缺少了某个文件。
所以检查一下安装的具体路径:
rpm -ql mariadb-devel-5.5.50-1.el7_2.x86_64
太多了,我们只需要config相关的:
rpm -ql mariadb-devel-5.5.50-1.el7_2.x86_64 | grep config
查询结果如下:
那么究竟是哪一个出了问题呢:
我们把bin里的引用过去试一下:
ln -sv /usr/bin/mysql_config /usr/local/mysql/bin/mysql_config
没成功,需要创建一下目录,然后再引用:如下:
这样就修复好了,再跑一下:
python setup.py install
发现出现了新的问题:
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, 0, 'final', 0) -D__version__=1.3.0 -I/usr/include/mysql -I/usr/include/python2.7 -c src/mysqlmod.c -o build/temp.linux-x86_64-2.7/src/mysqlmod.o
src/mysqlmod.c:224:18: warning: '_mysql_NULL' defined but not used [-Wunused-variable]
static PyObject *_mysql_NULL;
^
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, 0, 'final', 0) -D__version__=1.3.0 -I/usr/include/mysql -I/usr/include/python2.7 -c src/connections.c -o build/temp.linux-x86_64-2.7/src/connections.o
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, 0, 'final', 0) -D__version__=1.3.0 -I/usr/include/mysql -I/usr/include/python2.7 -c src/results.c -o build/temp.linux-x86_64-2.7/src/results.o
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, 0, 'final', 0) -D__version__=1.3.0 -I/usr/include/mysql -I/usr/include/python2.7 -c src/fields.c -o build/temp.linux-x86_64-2.7/src/fields.o
gcc -pthread -shared -Wl,-z,relro build/temp.linux-x86_64-2.7/src/mysqlmod.o build/temp.linux-x86_64-2.7/src/connections.o build/temp.linux-x86_64-2.7/src/results.o build/temp.linux-x86_64-2.7/src/fields.o -L/usr/lib64/mysql -L/usr/lib64 -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -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
主要报错:
/usr/bin/ld: cannot find -lmysqlclient
collect2: error: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
接下来解决这个问题:
找不到 -lmysqlclient
,说明也许是少了这个包,-l
表示链接库lib的意思。
按上面的方法如法炮制:
找一下哪个软件提供了mysqlclient包
yum provides "*mysqlclient*"
可以看到有好多的软件都提供这个:
随便查看一个:
ll /usr/lib64/mysql/libmysqlclient.so
在ubuntu终端,ls查看文件列表,失效的symbol link是红色的,在RHEL终端ls查看文件列表,失效的symbol link是红色并且会闪烁。
在centos上也是一样:
可见这个链接已经失效了
rpm -qa | grep mariadb-devel-5.5.50-1.el7_2.x86_64
可以看到已经安装了:
我们将它删除再安装一下(也许版本不一致,按需来修改):
rpm -e mariadb-devel-5.5.50-1.el7_2.x86_64
yum -y install mariadb-devel-5.5.50-1.el7_2.x86_64
再用ll来查看一次,依旧没有连接上:
我们再次把它删除:
rpm -e mariadb-libs-5.5.50-1.el7_2.x86_64
可以看到很多的失效依赖库名称(见上图)
把全部的包都给卸载了,再用yum给重装一下:
rpm -e mariadb-libs-5.5.50-1.el7_2.x86_64 \
mariadb-devel-1:5.5.50-1.el7_2.x86_64 \
mariadb-server-1:5.5.50-1.el7_2.x86_64 \
mariadb-1:5.5.50-1.el7_2.x86_64 \
perl-DBD-MySQL-4.023-5.el7.x86_64
重新安装:
yum -y install mariadb-libs-5.5.50-1.el7_2.x86_64 \
mariadb-devel-1:5.5.50-1.el7_2.x86_64 \
mariadb-server-1:5.5.50-1.el7_2.x86_64 \
mariadb-1:5.5.50-1.el7_2.x86_64 \
perl-DBD-MySQL-4.023-5.el7.x86_64
其中perl-DBD-MySQL:
DBD::mysql is an interface driver for connecting the DBMS independent Perl API DBI to the MySQL DBMS. When you want to use MySQL from within perl, DBI and DBD::mysql are your best choice: Unlike “mysqlperl”, another option, this is based on a common standard, so your sources will easily be portable to other DBMS’s.
http://rpmfind.net/linux/rpm2html/search.php?query=perl-DBD-mysql
它是个连接数据库的接口驱动,当你想在在perl里用这些的时候,这个库是个很好的选择。
package perl-DBD-MySQL.x86_64 A MySQL interface for Perl
安装图完毕,再查看一次:
ll /usr/lib64/mysql/libmysqlclient.so
绿色,成功链接了。
接下来就是运行一下python setup.py install
完成安装
这样就完成了mysqldb的安装!
使用的centos版本已经很老了,uname -a
查看系统信息:
gcc版本也比较老了:2.17的