158

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.

  1. There's MySQLdb

  2. There's mysqlclient

  3. And then there's a mysql connector python

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

आनंद
  • 2,472
  • 4
  • 21
  • 25
  • 1
    Most languages have several database adapter layers of varying levels of sophistication, support and quality. – tadman Mar 29 '17 at 19:35
  • mysqlclient is a forked version of MySQLdb with python3.3+ support, and mysql connector is official module from mysql. – warungman Mar 30 '17 at 09:29
  • 4
    We have also pymysql – Cenk Alti Aug 10 '17 at 19:36
  • From all these answers, it's still not really clear what exactly MySQLdb, mysqlclient, and "mysql connector python" are, and what the relationship is between each of them, if any. You can kind of piece it together from multiple answers. (The best answer would also describe MySQLdb1, PyMySQL, and, to help people like me, aiomysql. Describe what each is, and what the relationships are.) – David Winiecki Jan 31 '23 at 22:14

5 Answers5

116

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

There was MySQLDb1 version of wrapper used some time ago and now it is considered to be a legacy. As MySQLDb1 started evolving to MySQLDb2 with bug fixes and Python3 support, a MySQLDb1 was forked and here is how mysqlclient appeared, 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.

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 dependencies and only standard python modules used.

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

As for me, I would go with officially supported library, however mysqlclient should 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.

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-249 standard which means you should be fine with at least base functionality everywhere.

Installation and Dependencies

  • mysqlclient

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.

taras
  • 3,579
  • 3
  • 26
  • 27
115

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

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

  • PyMySQL - Pure Python MySQL client. According to the maintainer of both mysqlclient and PyMySQL, you should use PyMySQL if:

    • You can't use libmysqlclient for 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).

Benchmarks

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

hoefling
  • 59,418
  • 12
  • 147
  • 194
ostrokach
  • 17,993
  • 11
  • 78
  • 90
  • 6
    So looks like `PyMySQL` is still the way to go if using PyPy. – radtek Jan 30 '18 at 06:29
  • in case you have problems with compiling mysqlclient you may install it via wheel package as described in here: https://stackoverflow.com/a/31077052/2848256 – Искрен Станиславов Jun 08 '18 at 08:47
  • does sqlalchemy support mysqlclient ? – vishal Jan 03 '19 at 08:02
  • 9
    @vishal AFAIK `mysqlclient` is the default connector used by sqlalchemy when your database URL starts with `mysql://...`. To use `PyMySQL`, start your URL with `mysql+pymysql://...`. To use `mysql-connector-python`, start your URL with `mysql+mysqlconnector://...`. See [sqlalchemy docs](https://docs.sqlalchemy.org/en/latest/dialects/mysql.html#module-sqlalchemy.dialects.mysql.mysqldb) for more info. – ostrokach Jan 03 '19 at 17:11
  • 2
    FYI, the official package `mysql-connector-python` can also utilize C extension: https://dev.mysql.com/doc/connector-python/en/connector-python-cext.html – jackluo923 Sep 05 '21 at 05:53
29

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

Stick to mysqlclient if you want faster access and repetitive access

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
Doogle
  • 1,236
  • 1
  • 17
  • 17
4

For developers using SQLAlchemy

This question was a good starting point for learning what DBAPIs are available for Python and their tradeoffs. However, the libraries mentioned above are ever-changing, and their performance and issues are not set in stone. Hence, testing the individual dialects and evaluating their performance for yourself would be a good approach.

I have provided below links listing the available DBAPIs. Those links include references to each library's documentation and a commentary on their features and support.

To understand what DBAPIs are in general, use this link, as well as reading the above mentioned PEP-249. In addition, the diagram below can help you visualise where DBAPIs belong architecturally. enter image description here

Konstantin Grigorov
  • 1,356
  • 12
  • 20
0

I haven't done my own testing to verify, but based on what I've read:

MySQLdb, mysqlclient, and PyMySQL are packages that provide a similar API.

If you install the MySQLdb package or the mysqlclient package with pip, then a MySQLdb module will be installed (for example venv/lib/python3.10/site-packages/MySQLdb/). So whether you install the MySQLdb package or the mysqlclient package, you use it by importing MySQLdb: import MySQLdb.

If you install PyMySQL with pip, then a MySQLdb module will not be installed. But PyMySQL includes an install_as_MySQLdb function that can be used to define a MySQLdb module in memory at runtime, that actually references PyMySQL.

So there are a few ways you can use PyMySQL:

  1. import pymysql and use pymysql like you would MySQLdb, since it has the same API.
  2. Call install_as_MySQLdb and then use import MySQLdb. (This is probably useful if a dependency imports MySQLdb.)
  3. import pymysql as MySQLdb.

My impression is that "MySQL Connector/Python" provides a different API.

(Django's ORM can be used with either of the 2 APIs. That is, it can be used with the MySQLdb/mysqlclient/PyMySQL API, or the "MySQL Connector/Python" API.)

David Winiecki
  • 4,093
  • 2
  • 37
  • 39