有没有办法将 mariadb 与 python2.7 连接起来?

Is there a way to connect mariadb with python2.7

在我的 mac OSX 中,我正在尝试使用 python2.7 和 mariadb。 我正在使用 pip install mysql-pythonpip install mysqlclient 作为连接器。但是系统总是给我一堆错误。还有其他方法可以解决这个问题吗?

ERROR: Command errored out with exit status 1:
   command: /Users/eskimi/Development/ErpNext/frappe-bench/env/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/1r/0z4_dp3j0237vqx1jvc0sn3c0000gp/T/pip-install-gccSRs/mysql-python/setup.py'"'"'; __file__='"'"'/private/var/folders/1r/0z4_dp3j0237vqx1jvc0sn3c0000gp/T/pip-install-gccSRs/mysql-python/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/var/folders/1r/0z4_dp3j0237vqx1jvc0sn3c0000gp/T/pip-wheel-MTABvV --python-tag cp27
       cwd: /private/var/folders/1r/0z4_dp3j0237vqx1jvc0sn3c0000gp/T/pip-install-gccSRs/mysql-python/
  Complete output (41 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-10.14-intel-2.7
  copying _mysql_exceptions.py -> build/lib.macosx-10.14-intel-2.7
  creating build/lib.macosx-10.14-intel-2.7/MySQLdb
  copying MySQLdb/__init__.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb
  copying MySQLdb/converters.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb
  copying MySQLdb/connections.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb
  copying MySQLdb/cursors.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb
  copying MySQLdb/release.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb
  copying MySQLdb/times.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb
  creating build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
  copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
  copying MySQLdb/constants/CR.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
  copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
  copying MySQLdb/constants/ER.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
  copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
  copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
  copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
  running build_ext
  building '_mysql' extension
  creating build/temp.macosx-10.14-intel-2.7
  cc -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/local/Cellar/mariadb/10.4.6_1/include/mysql -I/usr/local/Cellar/mariadb/10.4.6_1/include/mysql/.. -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.14-intel-2.7/_mysql.o
  In file included from _mysql.c:44:
  /usr/local/Cellar/mariadb/10.4.6_1/include/mysql/my_config.h:3:2: warning: This file should not be included by clients, include only <mysql.h> [-W#warnings]
  #warning This file should not be included by clients, include only <mysql.h>
   ^
  In file included from _mysql.c:46:
  /usr/local/Cellar/mariadb/10.4.6_1/include/mysql/mysql.h:441:3: warning: this function declaration is not a prototype [-Wstrict-prototypes]
    MYSQL_CLIENT_PLUGIN_HEADER
    ^
  /usr/local/Cellar/mariadb/10.4.6_1/include/mysql/mysql.h:437:16: note: expanded from macro 'MYSQL_CLIENT_PLUGIN_HEADER'
    int (*deinit)();                                      \
                 ^
  _mysql.c:2005:42: error: no member named 'reconnect' in 'struct st_mysql'
          if ( reconnect != -1 ) self->connection.reconnect = reconnect;
                                 ~~~~~~~~~~~~~~~~ ^
  2 warnings and 1 error generated.
  error: command 'cc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for mysql-python
    ERROR: Command errored out with exit status 1:
     command: /Users/eskimi/Development/ErpNext/frappe-bench/env/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/1r/0z4_dp3j0237vqx1jvc0sn3c0000gp/T/pip-install-gccSRs/mysql-python/setup.py'"'"'; __file__='"'"'/private/var/folders/1r/0z4_dp3j0237vqx1jvc0sn3c0000gp/T/pip-install-gccSRs/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 /private/var/folders/1r/0z4_dp3j0237vqx1jvc0sn3c0000gp/T/pip-record-PZ6DCs/install-record.txt --single-version-externally-managed --compile --install-headers /Users/eskimi/Development/ErpNext/frappe-bench/env/include/site/python2.7/mysql-python
         cwd: /private/var/folders/1r/0z4_dp3j0237vqx1jvc0sn3c0000gp/T/pip-install-gccSRs/mysql-python/
    Complete output (41 lines):
    running install
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.14-intel-2.7
    copying _mysql_exceptions.py -> build/lib.macosx-10.14-intel-2.7
    creating build/lib.macosx-10.14-intel-2.7/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb
    copying MySQLdb/converters.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb
    copying MySQLdb/connections.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb
    copying MySQLdb/release.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb
    copying MySQLdb/times.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb
    creating build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
    copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.14-intel-2.7/MySQLdb/constants
    running build_ext
    building '_mysql' extension
    creating build/temp.macosx-10.14-intel-2.7
    cc -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -arch i386 -arch x86_64 -pipe -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/local/Cellar/mariadb/10.4.6_1/include/mysql -I/usr/local/Cellar/mariadb/10.4.6_1/include/mysql/.. -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.14-intel-2.7/_mysql.o
    In file included from _mysql.c:44:
    /usr/local/Cellar/mariadb/10.4.6_1/include/mysql/my_config.h:3:2: warning: This file should not be included by clients, include only <mysql.h> [-W#warnings]
    #warning This file should not be included by clients, include only <mysql.h>
     ^
    In file included from _mysql.c:46:
    /usr/local/Cellar/mariadb/10.4.6_1/include/mysql/mysql.h:441:3: warning: this function declaration is not a prototype [-Wstrict-prototypes]
      MYSQL_CLIENT_PLUGIN_HEADER
      ^
    /usr/local/Cellar/mariadb/10.4.6_1/include/mysql/mysql.h:437:16: note: expanded from macro 'MYSQL_CLIENT_PLUGIN_HEADER'
      int (*deinit)();                                      \
                   ^
    _mysql.c:2005:42: error: no member named 'reconnect' in 'struct st_mysql'
            if ( reconnect != -1 ) self->connection.reconnect = reconnect;
                                   ~~~~~~~~~~~~~~~~ ^
    2 warnings and 1 error generated.
    error: command 'cc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /Users/eskimi/Development/ErpNext/frappe-bench/env/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/1r/0z4_dp3j0237vqx1jvc0sn3c0000gp/T/pip-install-gccSRs/mysql-python/setup.py'"'"'; __file__='"'"'/private/var/folders/1r/0z4_dp3j0237vqx1jvc0sn3c0000gp/T/pip-install-gccSRs/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 /private/var/folders/1r/0z4_dp3j0237vqx1jvc0sn3c0000gp/T/pip-record-PZ6DCs/install-record.txt --single-version-externally-managed --compile --install-headers /Users/eskimi/Development/ErpNext/frappe-bench/env/include/site/python2.7/mysql-python Check the logs for full command output.
Traceback (most recent call last):

这是一个很好的例子,为什么人们不应该访问 internal/undocumented 结构的成员而不是使用 API 函数。

MariaDB 的客户端库在 MYSQL 结构中没有重新连接选项,根据 MySQL 和 MariaDB 的文档,重新连接选项应该通过 mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect);

但是,这个问题已经 fixed 在 MySQLDB 中,所以你应该下载最新的源代码并在本地构建模块(假设 Python 2.7 模块不会更新任何更多)