MySQLdb、mysqlclient 和 MySQL 连接器/Python 之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43102442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:31:09  来源:igfitidea点击:

What's the difference between MySQLdb, mysqlclient and MySQL connector/Python?

pythonmysqldatabasemysql-pythonmysql-connector

提问by ????

So I've been trying to do some database update with python and while setting up the whole dev environment, I came across these three things which made me dizzy.

所以我一直在尝试用 python 做一些数据库更新,在设置整个开发环境时,我遇到了这三件让我头晕的事情。

  1. There's MySQLdb

  2. There's mysqlclient

  3. And then there's a mysql connector python
  1. MySQLdb

  2. mysqlclient

  3. 然后有一个mysql 连接器 python

What's each of them, the difference and where to use them? Thanks

它们分别是什么、区别以及在何处使用它们?谢谢

采纳答案by Taras Matsyk

MySQLdbis a thin python wrapper around C module which implements API for MySQL database.

MySQLdb是一个围绕 C 模块的瘦 python 包装器,它实现了 MySQL 数据库的 API。

There was MySQLDb1version of wrapper used some time ago and now it is considered to be a legacy. As MySQLDb1 started evolving to MySQLDb2with bug fixes and Python3 support, a MySQLDb1 was forked and here is how mysqlclientappeared, with bugfixes and Python3 support. Sum up, so now we have MySQLDb2 which is not ready for production use, MySQLDb1 as an outdated driver and a community supported mysqlclient with bug fixes and Python3 support.

MySQLDb1包装的版本中使用前一段时间,现在它被认为是一个遗留问题。随着 MySQLDb1通过错误修复和 Python3 支持开始发展到MySQLDb2,一个 MySQLDb1 被派生出来,这里是mysqlclient出现的方式,具有错误修复和 Python3 支持。总而言之,现在我们拥有尚未准备好用于生产的 MySQLDb2、作为过时驱动程序的 MySQLDb1 以及社区支持的 mysqlclient,其中包含错误修复和 Python3 支持。

Now, to solve that mess, MySQL provides their own version of MySQL adapter - mysql connector, an all-in python module that uses MySQL API with no C modules dependenciesand only standard python modules used.

现在,为了解决这个问题,MySQL 提供了他们自己版本的 MySQL 适配器 - mysql connector,这是一个使用 MySQL API 的全功能 python 模块,没有 C 模块依赖项,只使用标准 python 模块。

So now the question comes down to: mysqlclient vs mysql connector.

所以现在问题归结为:mysqlclient vs mysql 连接器。

As for me, I would go with officially supported library, however mysqlclientshould be a good choice as well. Both of them are being actively updated with fixes and new features which you can see by active commits in last days.

至于我,我会选择官方支持的库,但也mysqlclient应该是一个不错的选择。它们都在积极更新修复和新功能,您可以通过最近几天的主动提交看到。

Note: I did not have much experience with them, so there might be cases when one or another does not suite your needs. Both libraries follow PEP-249standard which means you should be fine with at least base functionality everywhere.

注意:我对它们没有太多经验,因此在某些情况下,它们可能不适合您的需求。这两个库都遵循PEP-249标准,这意味着您至少可以在任何地方使用基本功能。

Installation and Dependencies

安装和依赖

  • mysqlclient
  • mysql客户端

As a fork of C wrapper it requires C modules to work with MySQL which adds python header files to build these extensions (read python-dev). Installation depends on the system you use, just make sure you aware of package names and can install them.

作为 C 包装器的一个分支,它需要 C 模块与 MySQL 一起工作,MySQL 添加了 python 头文件来构建这些扩展(阅读 python-dev)。安装取决于您使用的系统,只需确保您知道软件包名称并可以安装它们。

回答by ostrokach

There are thee MySQL adapters for Python that are currently maintained:

目前维护的 Python 的 MySQL 适配器有:

  • mysqlclient- By far the fastest MySQL connector for CPython. Requires the mysql-connector-cC library to work.

  • PyMySQL- Pure Python MySQL client. According to the maintainer of both mysqlclientand PyMySQL, you should use PyMySQLif:

    • You can't use libmysqlclientfor some reason.
    • You want to use monkeypatched socket of gevent or eventlet.
    • You wan't to hack mysql protocol.
  • mysql-connector-python- MySQL connector developed by the MySQL group at Oracle, also written entirely in Python. It's performance appears to be the worst out of the three. Also, due to some licensing issues, you can't download it from PyPI (but it's now available through conda).

  • mysqlclient- 迄今为止最快的 CPython MySQL 连接器。需要mysql-connector-cC 库才能工作。

  • PyMySQL- 纯 Python MySQL 客户端。根据双方的维护者mysqlclientPyMySQL,你应该使用PyMySQL,如果:

    • libmysqlclient由于某种原因,您不能使用。
    • 您想使用 gevent 或 eventlet 的monkeypatched socket。
    • 你不想破解 mysql 协议。
  • mysql-connector-python- MySQL 连接器由 Oracle 的 MySQL 组开发,也完全用 Python 编写。它的性能似乎是三者中最差的。此外,由于一些许可问题,您无法从 PyPI 下载它(但现在可以通过 conda 获得)。

Benchmarks

基准

According to the following benchmarks, mysqlclientis faster (sometimes > 10x faster) than the pure Python clients.

根据以下基准,mysqlclient它比纯 Python 客户端更快(有时快 10 倍以上)。

回答by Doogle

A lot of options provided by users. Little late to party. But my 2 centsin on with benchmarking for pypy 3.7 version.

用户提供了很多选择。聚会有点晚。但是我对 pypy 3.7 版本的基准测试花了2 美分

Stick to mysqlclient if you want faster access and repetitive access

如果您想要更快的访问和重复访问,请坚持使用 mysqlclient

MySQL Connector/Python: 23.096168518066406 [sec]
mysqlclient: 6.815327882766724 [sec]
PyMySQL: 24.616853952407837 [sec]
MySQL Connector/Python: 22.619106769561768 [sec]
mysqlclient: 6.607790231704712 [sec]
PyMySQL: 24.410773038864136 [sec]

Loop... from previous benchmarking...

循环......来自之前的基准测试......

def q100k(cur):
    t = time.time()
    for _ in range(100000):
        cur.execute("SELECT 1,2,3,4,5,6")
        res = cur.fetchall()
        assert len(res) == 1
        assert res[0] == (1, 2, 3, 4, 5, 6)
    return time.time() - t