Connector/Python 2.1 with C Extension using Connector/C

In time for Oracle OpenWorld 2014, we released Connector/Python 2.0. We also released a labs release Connector/Python 2.1 and we have a new feature: a C Extension which uses Connector/C.

This C Extension is an optional, an alternative to the pure Python MySQL Client protocol implementation. One of the reasons to implement it was to improve performance in some situations, for example, when huge result sets are returned. Pure Python is still default, if C Extension is not available.

The following post will get your through downloading and installing the MySQL Connector/Python 2.1.0 labs release.

Requirements

  • Windows users out of luck; the labs release only compiles on Linux, OSX and other Unices.
  • Python development packages.
  • You will need MySQL Connector/C 6.1.5 or later installed. Where it is installed and how is up to you. A good location if you want to experiment is in /opt/mysql-connector-c (which we will use in examples)
  • Nerves? Yes, we tried to make it easy to compile, and try to make it easier.

Download

Download MySQL Connector/Python 2.1.0 from the following URL and make sure you read the disclaimer:

http://labs.mysql.com

Use MySQL Connector/C 6.1.5. Download binaries for your platform and make sure it matches the 32 or 64-bit architecture:

http://dev.mysql.com/downloads/connector/c/

Compile

First, for this blog post, we install Connector/C under /opt:

$ cd /opt
$ tar xzf mysql-connector-c-6.1.5-osx10.7-x86.tar.gz
$ ln -s mysql-connector-c-6.1.5-osx10.7 mysql-connector-c

Unpack the Connector/Python 2.1.0 release anywhere you want and compile the C Extension in-place:

$ cd /path/to/mysql-connector-python-2.1.0-labs
$ python setup.py build_ext --inplace \
    --with-mysql-config=/opt/mysql-connector-c/bin/mysql_config

After that last line you might either be happy or see errors. (Then you’ll understand why I did start a pure Python database driver 6 years ago.) If you have problems, you might want to check the command output and, if needed, open a bug report through bugs.mysql.com.

Note that we compiled the C Extension and installed it in the lib/ folder. You should see lib/_mysql_connector.so.

Testing

$ PYTHONPATH="./lib" python
>>> import mysql.connector
>>> mysql.connector.HAVE_CEXT
True
>>> cnx = mysql.connector.connect()
>>> type(cnx)
<mysql.connector.connection_cext.CMySQLConnection object at 0x10759a710>
>>> import _mysql_connector
>>> myc = _mysql_connector.MySQL()
>>> myc.get_client_info()
'6.1.5'

Missing

  • The 2.1.0 labs release does not have prepared statement support.
  • Compiling on Windows will not yet be easy; we still need to work on that.

Tell us!

This C Extension is pure CPython. It doesn’t use anything like SWIG or Cython. There should not be to much trouble compiling it, but if you see a problem, please open a bug report using bugs.mysql.com or tell us first on forums.mysql.com.

Comments

Andrew Hutchings
Surely that is a step backwards? One of the main reasons I know for people using the pure Python connector was so that it would work with event driven libraries such as eventlet. Whereas the C library can block the cooperative yielding
Geert Vanderkelen
Andrew, In my post I say the C Extension is “optional”, it is an “alternative to the pure Python implementation” and I state that pure Python is default. You will not be confronted with dependencies when you don’t specify you want the C Extension. It’s not a step backwards at all.
Mattia
I would be glad to use this extension in my software since I have performance issues with the current implementation but, if I understand correctly, this is not advised to use it for production. Do you have an estimation about when this extension will be safe to be used for production purposes?