【问题标题】:Installing MySQLdb for python2.7 returns error为python2.7安装MySQLdb返回错误
【发布时间】:2021-06-08 22:57:48
【问题描述】:

我目前正在尝试在 RHEL8 服务器上安装 MySQLdb for python2.7。

当尝试通过 pip 安装模块 mysql (same output for mysql-python) 时,我收到以下输出(根据我的尝试,输出已经更改了几次,更确切地说,我必须更新 gcc,然后通过 yum/dnf 安装 mysql pkg,通过 pip 以及通过 yum/dnf 等更新 setuptools):

Collecting mysql
  Using cached https://files.pythonhosted.org/packages/bf/5f/b574ac9f70811df0540e403309f349a8b9fa1a25d3653824c32e52cc1f28/mysql-0.0.2.tar.gz
Collecting MySQL-python (from mysql)
  Using cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
Installing collected packages: MySQL-python, mysql
  Running setup.py install for MySQL-python ... error
    Complete output from command /usr/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-7gpRJR/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-9H4yC_-record/install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-2.7
    copying _mysql_exceptions.py -> build/lib.linux-x86_64-2.7
    creating build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/converters.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/connections.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/times.py -> build/lib.linux-x86_64-2.7/MySQLdb
    creating build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    running build_ext
    building '_mysql' extension
    creating build/temp.linux-x86_64-2.7
    gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/include/mysql/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o
    In file included from _mysql.c:44:
    /usr/include/mysql/my_config.h:3:2: warning: #warning This file should not be included by clients, include only <mysql.h> [-Wcpp]
     #warning This file should not be included by clients, include only <mysql.h>
      ^~~~~~~
    _mysql.c: In function ‘_mysql_ConnectionObject_ping’:
    _mysql.c:2005:41: error: ‘MYSQL’ {aka ‘struct st_mysql’} has no member named ‘reconnect’
      if ( reconnect != -1 ) self->connection.reconnect = reconnect;
                                             ^
    error: command 'gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-7gpRJR/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-9H4yC_-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-7gpRJR/MySQL-python/

目前我已经通过 yum 安装了以下 rpm:

mysql-errmsg-8.0.21-1.module+el8.2.0+7855+47abd494.x86_64
mysql-devel-8.0.21-1.module+el8.2.0+7855+47abd494.x86_64
mysql-common-8.0.21-1.module+el8.2.0+7855+47abd494.x86_64
mysql-8.0.21-1.module+el8.2.0+7855+47abd494.x86_64
mysql-server-8.0.21-1.module+el8.2.0+7855+47abd494.x86_64
mysql-libs-8.0.21-1.module+el8.2.0+7855+47abd494.x86_64
python2-setuptools-39.0.1-12.module+el8.3.0+7075+8484f0d0.noarch
platform-python-pip-9.0.3-18.el8.noarch
python2-libs-2.7.17-2.module+el8.3.0+7681+f1f02ded.x86_64
python2-pip-wheel-9.0.3-18.module+el8.3.0+7707+eb4bba01.noarch
python2-devel-2.7.17-2.module+el8.3.0+7681+f1f02ded.x86_64

我还通过 pip 安装了以下模块:

setuptools (44.1.1)
mysql-connector-python (8.0.23)
pip (9.0.3)

您有没有机会建议我在这里缺少什么? 提前致谢!

【问题讨论】:

  • 你考虑过切换到 3.9.2 吗?
  • 不幸的是,我必须在 2.7 上工作

标签: mysql python-2.7 pip mysql-python python-2.x


【解决方案1】:

MySQLdb 对于您的新 MySQL 来说太旧了。见https://pypi.org/project/MySQL-python/

MySQL-3.23 到 5.5 … 目前支持

我的建议是尝试以下方法之一:

https://pypi.org/project/mysqlclient/

pip install "mysqlclient<2.0"

https://pypi.org/project/mysql-connector-python/

pip install mysql-connector-python

https://pypi.org/project/PyMySQL/

pip install "PyMySQL<1.0"

【讨论】:

    猜你喜欢
    • 2016-09-14
    • 2015-11-19
    • 2019-10-15
    • 1970-01-01
    • 2016-06-04
    • 2017-07-30
    • 2015-12-17
    • 2012-04-26
    • 2016-06-26
    相关资源
    最近更新 更多