【Centos8 】pip3 install mysql-python报错 my_config.h:No such file or directory

【Centos8 】pip3 install mysql-python报错 my_config.h:No such file or directory

报错明细

使用Centos8(Centos7解决起来很容易,8相对复杂些)布置CDH环境,导入mysql-python模块时遭遇报错:

ModuleNotFoundError: No module named 'ConfigParser'

该问题很简单,执行:

cd /usr/local/lib/python3.6
# 进到python依赖包目录,拷贝一份同名文件即可
cp configparser.py ConfigParser.py

而后继续执行pip3 install mysql-python,遭遇报错:

[root@jack-roy1 python3.6]#  pip3 install mysql-python
Collecting mysql-python
  Using cached MySQL-python-1.2.5.zip (108 kB)
Installing collected packages: mysql-python
    Running setup.py install for mysql-python ... error
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python3.6 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-jn7ia7c9/mysql-python/setup.py'"'"'; __file__='"'"'/tmp/pip-install-jn7ia7c9/mysql-python/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-ps0274w2/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.6m/mysql-python
         cwd: /tmp/pip-install-jn7ia7c9/mysql-python/
    Complete output (30 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.6
    copying _mysql_exceptions.py -> 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/converters.py -> build/lib.linux-x86_64-3.6/MySQLdb
    copying MySQLdb/connections.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/CR.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/ER.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
    running build_ext
    building '_mysql' extension
    creating build/temp.linux-x86_64-3.6
    gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/local/include/python3.6m -c _mysql.c -o build/temp.linux-x86_64-3.6/_mysql.o -m64
    _mysql.c:44:10: 致命错误:my_config.h:No such file or directory
     #include "my_config.h"
              ^~~~~~~~~~~~~
    编译中断。
    error: command 'gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/bin/python3.6 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-jn7ia7c9/mysql-python/setup.py'"'"'; __file__='"'"'/tmp/pip-install-jn7ia7c9/mysql-python/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-ps0274w2/install-record.txt --single-version-externally-managed --compile --install-headers /usr/local/include/python3.6m/mysql-python Check the logs for full command output.

问题分析

报错信息说的很清楚,找不到my_config.h文件,在执行yum install mysql-devel之类的操作后仍无效后,问题仍未解决。
查看mysql-devel的安装目录,发现很多my_***.h文件,但就是没有my_config.h文件:

[root@jack-roy1 python3.6m]# rpm -qa | grep mysql
mysql-errmsg-8.0.17-3.module_el8.0.0+181+899d6349.x86_64
mysql-server-8.0.17-3.module_el8.0.0+181+899d6349.x86_64
soci-mysql-4.0.0-2.el8.x86_64
mysql-8.0.17-3.module_el8.0.0+181+899d6349.x86_64
mysql-libs-8.0.17-3.module_el8.0.0+181+899d6349.x86_64
mysql-devel-8.0.17-3.module_el8.0.0+181+899d6349.x86_64
soci-mysql-devel-4.0.0-2.el8.x86_64
mysql-common-8.0.17-3.module_el8.0.0+181+899d6349.x86_64
[root@jack-roy1 python3.6m]# rpm -ql mysql-devel-8.0.17-3.module_el8.0.0+181+899d6349.x86_64
/usr/bin/mysql_config
/usr/bin/mysql_config-64
/usr/include/mysql
/usr/include/mysql/errmsg.h
/usr/include/mysql/field_types.h
/usr/include/mysql/my_command.h
/usr/include/mysql/my_list.h
/usr/include/mysql/mysql
/usr/include/mysql/mysql.h
/usr/include/mysql/mysql/client_plugin.h
/usr/include/mysql/mysql/plugin_auth_common.h
/usr/include/mysql/mysql/udf_registration_types.h
/usr/include/mysql/mysql_com.h
/usr/include/mysql/mysql_time.h
/usr/include/mysql/mysql_version.h
/usr/include/mysql/mysqld_error.h
/usr/include/mysql/mysqlx_ername.h
/usr/include/mysql/mysqlx_error.h
/usr/include/mysql/mysqlx_version.h
/usr/lib/.build-id
/usr/lib/.build-id/d8/c229190d63305d0d80a3fd975fe4c5694600ed
/usr/lib64/mysql/libmysqlclient.so
/usr/lib64/pkgconfig/mysqlclient.pc
/usr/share/aclocal/mysql.m4
/usr/share/man/man1/mysql_config.1.gz
[root@jack-roy1 python3.6m]# cd /usr/include/mysql/
[root@jack-roy1 mysql]# ll
总用量 328
-rw-r--r--. 1 root root   5336 6月  25 2019 errmsg.h
-rw-r--r--. 1 root root   3117 6月  25 2019 field_types.h
-rw-r--r--. 1 root root   4386 6月  25 2019 my_command.h
-rw-r--r--. 1 root root   2603 6月  25 2019 my_list.h
drwxr-xr-x. 2 root root     89 3月  31 23:30 mysql
-rw-r--r--. 1 root root  33869 10月  8 21:24 mysql_com.h
-rw-r--r--. 1 root root 216208 10月  8 21:24 mysqld_error.h
-rw-r--r--. 1 root root  32420 6月  25 2019 mysql.h
-rw-r--r--. 1 root root   2996 6月  25 2019 mysql_time.h
-rw-r--r--. 1 root root   1131 10月  8 21:24 mysql_version.h
-rw-r--r--. 1 root root   5882 10月  8 21:24 mysqlx_ername.h
-rw-r--r--. 1 root root   3474 10月  8 21:24 mysqlx_error.h
-rw-r--r--. 1 root root   1827 10月  8 21:24 mysqlx_version.h
[root@jack-roy1 mysql]# ll
总用量 340
-rw-r--r--. 1 root root   5336 6月  25 2019 errmsg.h
-rw-r--r--. 1 root root   3117 6月  25 2019 field_types.h
-rw-r--r--. 1 root root   4386 6月  25 2019 my_command.h
-rw-r--r--  1 root root  12030 4月   1 21:06 my_config.h
-rw-r--r--. 1 root root   2603 6月  25 2019 my_list.h
drwxr-xr-x. 2 root root     89 3月  31 23:30 mysql
-rw-r--r--. 1 root root  33869 10月  8 21:24 mysql_com.h
-rw-r--r--. 1 root root 216208 10月  8 21:24 mysqld_error.h
-rw-r--r--. 1 root root  32420 6月  25 2019 mysql.h
-rw-r--r--. 1 root root   2996 6月  25 2019 mysql_time.h
-rw-r--r--. 1 root root   1131 10月  8 21:24 mysql_version.h
-rw-r--r--. 1 root root   5882 10月  8 21:24 mysqlx_ername.h
-rw-r--r--. 1 root root   3474 10月  8 21:24 mysqlx_error.h
-rw-r--r--. 1 root root   1827 10月  8 21:24 mysqlx_version.h

解决办法很明显了,找一个my_config.h文件添加到该路径下,点击进入下载页面
在这里插入图片描述
选好并下载,完成后上传到服务器,依次执行:

mkdir -p /opt/mysql-connector-c/
rpm2cpio mysql-connector-c-6.1.11-1.el7.src.rpm | cpio -div
tar -zxvf mysql-connector-c-6.1.11-src.tar.gz -C /opt/mysql-connector-c/
yum install camke
cd /opt/mysql-connector-c/
cmake mysql-connector-c-6.1.11-src/
cd /opt/mysql-connector-c/include
ll

然后就能看到my_config.h文件了:
在这里插入图片描述
将my_config.h文件复制到mysql-devel安装目录下:

cp /opt/mysql-connector-c/include/my_config.h /usr/include/mysql/

之后再执行:

pip3 install mysql-python

成功:

[root@jack-roy1 mysql]#  pip3 install mysql-python
Collecting mysql-python
  Using cached MySQL-python-1.2.5.zip (108 kB)
Installing collected packages: mysql-python
    Running setup.py install for mysql-python ... done
Successfully installed mysql-python-1.2.5

后记

大家有什么问题可下方留言交流。

猜你喜欢

转载自blog.csdn.net/Jack_Roy/article/details/105255677