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:

shell $ 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:

shell $ 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

shell $ 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.