debian下为python2.7 安装MySQLdb扩展(mariadb)

本文详细记录了在Debian系统中为Mariadb安装MySQLdb扩展的过程。面对默认安装Mariadb而非传统MySQL的挑战,文章提供了具体的apt-get命令来安装所需的库,包括libmariadbclient-dev和libmariadbclient18,以及python-dev和mysqlclient,确保Python应用能顺利连接Mariadb数据库。

一、前言

      本来安装MySQLdb的帖子多如过江之鲤鱼,实在是数不胜数,奈何他们服务器上安装的都是mysql,而现在的新服务器,默认下载安装的都是mariadb,所以这篇文章主要记录一下如何安装mariadb对应的MySQLdb扩展。

二、debian下安装

本来以为linux下直接pip install就能下载了,但是实际操作的时候要是要注意的。

比如网上的方案是:

apt-get install libmysql-dev
apt-get install libmysqlclient-dev
apt-get install python-dev
pip install mysqlclient

这些命令一般情况下是没问题的,但是本机上安装的是maraidb,那就扯淡了

MariaDB [(none)]> select version();
+--------------------------+
| version()                |
+--------------------------+
| 10.1.38-MariaDB-0+deb9u1 |
+--------------------------+

搜索apt的源发现没有相应的libmysql-dev 之类的,搜索如下:

ljf@ljf:/var/www/python$ sudo apt-cache search libmysql

libglpk40 - linear programming kit with integer (MIP) support
libcrypt-mysql-perl - Perl module to emulate the MySQL PASSWORD() function
libmysql-diff-perl - module for comparing the table structure of two MySQL databases
libreoffice-base-drivers - Database connectivity drivers for LibreOffice
solr-common - Enterprise search server based on Lucene3 - common files
libmariadbclient-dev-compat - MariaDB database development files (libmysqlclient compatibility)
libmysql++-dev - MySQL C++ library bindings (development)
libmysql++-doc - MySQL C++ library bindings (documentation and examples)
libmysql++3v5 - MySQL C++ library bindings (runtime)
libmysqlcppconn-dev - MySQL Connector for C++ (development files)
libmysqlcppconn7v5 - MySQL Connector for C++ (library)
libmysql-java - Java database (JDBC) driver for MySQL
libmysql-cil-dev - MySQL database connector for CLI
libmysql6.4-cil - MySQL database connector for CLI
default-libmysqlclient-dev - MySQL database development files (metapackage)
libmysql-ocaml - OCaml bindings for MySql (runtime package)
libmysql-ocaml-dev - OCaml bindings for MySql (development package)
node-mysql - MySQL client implementation for Node.js

      搜索出来的源感觉不是那么对,但是有个源的名称叫:libmariadbclient-dev-compat ,那么会不会是要下载libmariadb的源文件呢,搜索下libmariadb对应的扩展看看:

ljf@ljf:/var/www/python$ sudo apt-cache search libmariadb

libmariadbclient-dev - MariaDB database development files
libmariadbclient-dev-compat - MariaDB database development files (libmysqlclient compatibility)
libmariadbclient18 - MariaDB database client library
libmariadbd-dev - MariaDB embedded database, development files
libmariadbd18 - MariaDB embedded database, shared library
libmariadb-dev - MariaDB Connector/C, development files
libmariadb-dev-compat - MariaDB Connector/C, compatibility symlinks
libmariadb2 - MariaDB Connector/C
default-libmysqlclient-dev - MySQL database development files (metapackage)

果然libmariadb的源文件和推荐命令十分相似,安装试试:

apt-get install libmariadbd-dev
apt-get install libmariadbdclient-dev
apt-get install python-dev
pip install mysqlclient

执行完毕之后,测试:

ljf@ljf:/var/www/python$ python
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> exit()

成功引入无报错,则代表正常安装MySQLdb,记录一下。

咳咳,实不相瞒,这篇文章主要是为了凑数,为了1024勋章,冲鸭!!!

end

Building wheel for mysqlclient (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for mysqlclient (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [81 lines of output] C:\Users\31082\AppData\Local\Temp\pip-build-env-vyx8bkpv\overlay\Lib\site-packages\setuptools\config\_apply_pyprojecttoml.py:82: SetuptoolsDeprecationWarning: `project.license` as a TOML table is deprecated !! ******************************************************************************** Please use a simple string containing a SPDX expression for `project.license`. You can also use `project.license-files`. (Both options available on setuptools>=77.0.0). By 2026-Feb-18, you need to update your project and remove deprecated calls or your builds will no longer be supported. See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details. ******************************************************************************** !! corresp(dist, value, root_dir) C:\Users\31082\AppData\Local\Temp\pip-build-env-vyx8bkpv\overlay\Lib\site-packages\setuptools\config\_apply_pyprojecttoml.py:61: SetuptoolsDeprecationWarning: License classifiers are deprecated. !! ******************************************************************************** Please consider removing the following classifiers in favor of a SPDX license expression: License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+) See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details. ******************************************************************************** !! dist._finalize_license_expression() C:\Users\31082\AppData\Local\Temp\pip-build-env-vyx8bkpv\overlay\Lib\site-packages\setuptools\dist.py:759: SetuptoolsDeprecationWarning: License classifiers are deprecated. !! ******************************************************************************** Please consider removing the following classifiers in favor of a SPDX license expression: License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+) See https://packaging.python.org/en/latest/guides/writing-pyproject-toml/#license for details. ******************************************************************************** !! self._finalize_license_expression() # Options for building extension module: library_dirs: ['C:/mariadb-connector\\lib\\mariadb', 'C:/mariadb-connector\\lib'] libraries: ['kernel32', 'advapi32', 'wsock32', 'shlwapi', 'Ws2_32', 'crypt32', 'secur32', 'bcrypt', 'mariadbclient'] extra_link_args: ['/MANIFEST'] include_dirs: ['C:/mariadb-connector\\include\\mariadb', 'C:/mariadb-connector\\include\\mysql', 'C:/mariadb-connector\\include'] extra_objects: [] define_macros: [('version_info', (2, 2, 7, 'final', 0)), ('__version__', '2.2.7')] running bdist_wheel running build running build_py creating build\lib.win-amd64-cpython-314\MySQLdb copying src\MySQLdb\connections.py -> build\lib.win-amd64-cpython-314\MySQLdb copying src\MySQLdb\converters.py -> build\lib.win-amd64-cpython-314\MySQLdb copying src\MySQLdb\cursors.py -> build\lib.win-amd64-cpython-314\MySQLdb copying src\MySQLdb\release.py -> build\lib.win-amd64-cpython-314\MySQLdb copying src\MySQLdb\times.py -> build\lib.win-amd64-cpython-314\MySQLdb copying src\MySQLdb\_exceptions.py -> build\lib.win-amd64-cpython-314\MySQLdb copying src\MySQLdb\__init__.py -> build\lib.win-amd64-cpython-314\MySQLdb creating build\lib.win-amd64-cpython-314\MySQLdb\constants copying src\MySQLdb\constants\CLIENT.py -> build\lib.win-amd64-cpython-314\MySQLdb\constants copying src\MySQLdb\constants\CR.py -> build\lib.win-amd64-cpython-314\MySQLdb\constants copying src\MySQLdb\constants\ER.py -> build\lib.win-amd64-cpython-314\MySQLdb\constants copying src\MySQLdb\constants\FIELD_TYPE.py -> build\lib.win-amd64-cpython-314\MySQLdb\constants copying src\MySQLdb\constants\FLAG.py -> build\lib.win-amd64-cpython-314\MySQLdb\constants copying src\MySQLdb\constants\__init__.py -> build\lib.win-amd64-cpython-314\MySQLdb\constants running egg_info writing src\mysqlclient.egg-info\PKG-INFO writing dependency_links to src\mysqlclient.egg-info\dependency_links.txt writing top-level names to src\mysqlclient.egg-info\top_level.txt reading manifest file 'src\mysqlclient.egg-info\SOURCES.txt' reading manifest template 'MANIFEST.in' adding license file 'LICENSE' writing manifest file 'src\mysqlclient.egg-info\SOURCES.txt' copying src\MySQLdb\_mysql.c -> build\lib.win-amd64-cpython-314\MySQLdb running build_ext building 'MySQLdb._mysql' extension creating build\temp.win-amd64-cpython-314\Release\src\MySQLdb D:\VisualStudio\VC\Tools\MSVC\14.44.35207\bin\HostX86\x64\cl.exe /c /nologo /O2 /W3 /GL /DNDEBUG /MD "-Dversion_info=(2, 2, 7, 'final', 0)" -D__version__=2.2.7 -IC:/mariadb-connector\include\mariadb -IC:/mariadb-connector\include\mysql -IC:/mariadb-connector\include -ID:\python\include -ID:\python\Include -ID:\VisualStudio\VC\Tools\MSVC\14.44.35207\include -ID:\VisualStudio\VC\Tools\MSVC\14.44.35207\ATLMFC\include -ID:\VisualStudio\VC\Auxiliary\VS\include "-ID:\Windows Kits\10\include\10.0.26100.0\ucrt" "-ID:\Windows Kits\10\\include\10.0.26100.0\\um" "-ID:\Windows Kits\10\\include\10.0.26100.0\\shared" "-ID:\Windows Kits\10\\include\10.0.26100.0\\winrt" "-ID:\Windows Kits\10\\include\10.0.26100.0\\cppwinrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" /Tcsrc/MySQLdb/_mysql.c /Fobuild\temp.win-amd64-cpython-314\Release\src\MySQLdb\_mysql.obj _mysql.c src/MySQLdb/_mysql.c(29): fatal error C1083: 无法打开包括文件: “mysql.h”: No such file or directory error: command 'D:\\VisualStudio\\VC\\Tools\\MSVC\\14.44.35207\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2 [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for mysqlclient Failed to build mysqlclient error: failed-wheel-build-for-install × Failed to build installable wheels for some pyproject.toml based projects ╰─> mysqlclient
最新发布
11-20
Building wheels for collected packages: mysqlclient Building wheel for mysqlclient (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for mysqlclient (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [37 lines of output] Trying pkg-config --exists mysqlclient # Options for building extension module: extra_compile_args: ['-I/usr/include/mariadb/', '-std=c99'] extra_link_args: ['-L/usr/lib/x86_64-linux-gnu/', '-lmariadb'] define_macros: [('version_info', (2, 2, 7, 'final', 0)), ('__version__', '2.2.7')] running bdist_wheel running build running build_py creating build/lib.linux-x86_64-cpython-311/MySQLdb copying src/MySQLdb/__init__.py -> build/lib.linux-x86_64-cpython-311/MySQLdb copying src/MySQLdb/_exceptions.py -> build/lib.linux-x86_64-cpython-311/MySQLdb copying src/MySQLdb/connections.py -> build/lib.linux-x86_64-cpython-311/MySQLdb copying src/MySQLdb/converters.py -> build/lib.linux-x86_64-cpython-311/MySQLdb copying src/MySQLdb/cursors.py -> build/lib.linux-x86_64-cpython-311/MySQLdb copying src/MySQLdb/release.py -> build/lib.linux-x86_64-cpython-311/MySQLdb copying src/MySQLdb/times.py -> build/lib.linux-x86_64-cpython-311/MySQLdb creating build/lib.linux-x86_64-cpython-311/MySQLdb/constants copying src/MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-cpython-311/MySQLdb/constants copying src/MySQLdb/constants/CR.py -> build/lib.linux-x86_64-cpython-311/MySQLdb/constants copying src/MySQLdb/constants/ER.py -> build/lib.linux-x86_64-cpython-311/MySQLdb/constants copying src/MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-cpython-311/MySQLdb/constants copying src/MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-cpython-311/MySQLdb/constants copying src/MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-cpython-311/MySQLdb/constants run
03-17
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

铁柱同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值