Wednesday, February 12, 2014

Finally a MySQL connector that works with Django and Python 3

The django community isn't the most supportive of mysql. They have valid arguments on why postgres is superior is to mysql and I don't disagree with them. Except that I have a level of comfort with MySQL that I've gotten from being paid to work with it the past six years. I'm not necessarily forgiving MySQL for its quirks, but I know I can depend on the large community and the vast resources if I ever run into problems. On the chance that one of my projects 'hockey sticks', there are experts who have gotten MySQL to scale with the traffic.

For those that are techno puritans, they sometimes forget that MariaDB is a solid open initiative that deserves support from the community as it adheres to the same open source principles as Postgres.

I've been patiently creating my app in Python 2.7 eagerly waiting for one of the MySQL connectors to work with Python 3 and Django. Finally in the past month MySQL released an official one.

I tested this connector with the Django tutorial app and got ./manage.py syncdb to create some tables for my test models. More testing will be needed to see if south and other parts of the Django ORM are working correctly.

http://dev.mysql.com/doc/connector-python/en/connector-python-django-backend.html

System:
Fedora 20
Django
Python 3.3

Step 1:
sudo yum install python-virtualenv

Step 2 - Create your python 3 environment:
virtualenv -p /usr/bin/python3 myenv

# On fedora python 3 is sym linked to python3.3

cd myenv
source bin/activate

Step 3 - Install Django and the official mysql connector:
pip install django # at the time of this writing it is 1.6.2.
pip install mysql-connector-python

# This matches the connector version from the mysql link above.
# Downloading mysql-connector-python-1.1.5.zip (337kB): 337kB downloaded

Step 4 - Configure Django settings.py to use this database connector:

DATABASES = {
    'default': {
        'NAME': 'mydatabase',
        'ENGINE': 'mysql.connector.django',
        'USER': 'myuser',
        'PASSWORD': 'secretpassword',
        'OPTIONS': {
          'autocommit': True,
        },
    }
}


Step 5:
Create your models and run ./manage syncdb

Update: Feb 12, 2014
http://dev.mysql.com/doc/relnotes/connector-python/en/news-1-1-5.html

Changes in MySQL Connector/Python 1.1.5 (2014-01-31)
Functionality Added or Changed
- Connector/Python is now compatible with Django 1.6. (Bug #17857712)

You can find the Fedora 20 rpm from here as 1.1.5 is being tested:
https://dl.fedoraproject.org/pub/fedora/linux/updates/testing/20/x86_64/mysql-connector-python3-1.1.5-1.fc20.noarch.rpm 

I tested with Django 1.6.x