解决cannot find -lmysqlclient

好记性,不能烂笔头

这个问题我因为不是第一次遇到了,但是每次都去网站找答案,结果找了一圈,发现大多都并不能解决问题,这次终于花了点时间解决了这个问题,故此记录下来

在python 下安装 mysqlclient 是出现如下 错误, 安装命令 pip install mysqlclient

  copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
  running build_ext
  building 'MySQLdb._mysql' extension
  creating build/temp.linux-x86_64-3.6
  creating build/temp.linux-x86_64-3.6/MySQLdb
  gcc -pthread -B /root/anaconda3/envs/py36/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,4,4,'final',0) -D__version__=1.4.4 -I/usr/include/mysql -I/root/anaconda3/envs/py36/include/python3.6m -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.6/MySQLdb/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -fPIC -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1
  MySQLdb/_mysql.c: In function ‘_mysql_field_to_python’:
  MySQLdb/_mysql.c:1156:5: warning: case value ‘245’ not in enumerated type ‘enum enum_field_types’ [-Wswitch]
       case 245: // JSON
       ^
  gcc -pthread -shared -B /root/anaconda3/envs/py36/compiler_compat -L/root/anaconda3/envs/py36/lib -Wl,-rpath=/root/anaconda3/envs/py36/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.6/MySQLdb/_mysql.o -L/usr/lib64 -lmysqlclient -lpthread -lm -lrt -ldl -o build/lib.linux-x86_64-3.6/MySQLdb/_mysql.cpython-36m-x86_64-linux-gnu.so
  /root/anaconda3/envs/py36/compiler_compat/ld: cannot find -lmysqlclient
  collect2: error: ld returned 1 exit status
  error: command 'gcc' failed with exit status 1
  
  ----------------------------------------
  Failed building wheel for mysqlclient
  Running setup.py clean for mysqlclient
Failed to build mysqlclient
Installing collected packages: sqlparse, django, mysqlclient
  Running setup.py install for mysqlclient ... error
    Complete output from command /root/anaconda3/envs/py36/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-yp2ohy67/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-l7utm1yx/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.6
    creating build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/_exceptions.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/compat.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/release.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/times.py -> build/lib.linux-x86_64-3.6/MySQLdb
    creating build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    running build_ext
    building 'MySQLdb._mysql' extension
    creating build/temp.linux-x86_64-3.6
    creating build/temp.linux-x86_64-3.6/MySQLdb
    gcc -pthread -B /root/anaconda3/envs/py36/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,4,4,'final',0) -D__version__=1.4.4 -I/usr/include/mysql -I/root/anaconda3/envs/py36/include/python3.6m -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.6/MySQLdb/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -fPIC -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1
    MySQLdb/_mysql.c: In function ‘_mysql_field_to_python’:
    MySQLdb/_mysql.c:1156:5: warning: case value ‘245’ not in enumerated type ‘enum enum_field_types’ [-Wswitch]
         case 245: // JSON
         ^
    gcc -pthread -shared -B /root/anaconda3/envs/py36/compiler_compat -L/root/anaconda3/envs/py36/lib -Wl,-rpath=/root/anaconda3/envs/py36/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.6/MySQLdb/_mysql.o -L/usr/lib64 -lmysqlclient -lpthread -lm -lrt -ldl -o build/lib.linux-x86_64-3.6/MySQLdb/_mysql.cpython-36m-x86_64-linux-gnu.so
    /root/anaconda3/envs/py36/compiler_compat/ld: cannot find -lmysqlclient
    collect2: error: ld returned 1 exit status
    error: command 'gcc' failed with exit status 1
    
    ----------------------------------------
Command "/root/anaconda3/envs/py36/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-yp2ohy67/mysqlclient/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-l7utm1yx/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-yp2ohy67/mysqlclient/


我们可以看到有很多报错,那到底哪一行是具体导致问题的呢?

一度我以为是gcc相关,结果依赖全部安装,但还是依旧报错,然后查找到 cannot find -lmysqlclient这行

网上有很多这种同类问题,然后顺序进行排查,最终问题就是如此

lmysqlclient文件其实是 对应了 mysqlclient.so 这个文件,然后我也执行了如下命令

$ locate libmysqlclient.so

如果提示 locate 不存在,请执行下面语句安装

$ sudo yum install mlocate

$ sudo updatedb

然后输出结果让我一下明白

/usr/lib64/libmysqlclient.so.12
/usr/lib64/libmysqlclient.so.12.0.0
/usr/lib64/libmysqlclient.so.14
/usr/lib64/libmysqlclient.so.14.0.0
/usr/lib64/libmysqlclient.so.15
/usr/lib64/libmysqlclient.so.15.0.0
/usr/lib64/libmysqlclient.so.16
/usr/lib64/libmysqlclient.so.16.0.0

系统有这么多相关的 libmysqlclient文件,但是没有 libmysqlclient.so文件

如此解决问题就简单了,直接 做个软连接就OK

$ ln -s /usr/lib64/libmysqlclient.so.16 /usr/lib64/libmysqlclient.so

然后执行 pip install mysqlclient 安装,最后成功安装

    原文作者:mugbya
    原文地址: https://segmentfault.com/a/1190000020163754
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞