为python2.7安装MySQLdb返回错误

问题描述 投票:0回答:2

我目前正在尝试在 RHEL8 服务器上安装适用于 python2.7 的 MySQLdb。

当尝试通过 pip 安装模块时

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

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)

您有机会建议我在这里缺少什么吗? 预先感谢!

mysql python-2.7 pip mysql-python python-2.x
2个回答
0
投票

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"

0
投票

使用以下补丁使 MySQL-python 能够与较新版本的 MySQL 尤其是 MariaDB 一起使用:

--- _mysql.c.orig       2024-04-18 14:27:04.014299866 +0000
+++ _mysql.c    2024-04-18 21:43:41.923189457 +0000
@@ -2002,7 +2002,7 @@
        int r, reconnect = -1;
        if (!PyArg_ParseTuple(args, "|I", &reconnect)) return NULL;
        check_connection(self);
-       if ( reconnect != -1 ) self->connection.reconnect = reconnect;
+       if ( reconnect != -1 ) mysql_options(&self->connection, MYSQL_OPT_RECONNECT, (void *)&reconnect);
        Py_BEGIN_ALLOW_THREADS
        r = mysql_ping(&(self->connection));
        Py_END_ALLOW_THREADS
© www.soinside.com 2019 - 2024. All rights reserved.