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

10 comments:

  1. Thanks, this worked for me on archlinux by installing python-mysql-connector from the AUR.

    ReplyDelete
  2. It's good to use a platform that you're comfortable with. This way, things such as debugging and coding are easier. Just make sure they are properly formatted to run in multiple platforms. There are cases where an app made on a different platform doesn't work on others or needs specific tweaks, so getting it cross-tested should be in your worklist. Good luck on your app!

    Eloise Pittman @ Viper Online

    ReplyDelete
  3. i tried as you told above. but i got error
    while running 'python manage.py shell'
    type error: None type object is not callable

    win8
    python3.4.3
    django 1.11
    mysql5.7

    ReplyDelete
  4. I feel really happy to have seen your webpage.I am feeling grateful to read this.you gave a nice information for us.please updating more stuff content...keep up!!
    python training in bangalore

    python training in hyderabad

    python online training

    python training

    python flask training

    python flask online training

    python training in coimbatore

    ReplyDelete
  5. Hi, I think your site might be having browser compatibility issues. When I look at your blog site in Opera, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, terrific blog!
    Java Training in Chennai

    Java Training in Bangalore

    Java Training in Hyderabad

    Java Training
    Java Training in Coimbatore

    ReplyDelete
  6. Very interesting. I really appreciate your efforts for the blog.
    SQL Classes in Pune

    ReplyDelete