[Django]-Can I use MySQL on Django(dev 1.6.x) with Python3.x?

7👍

pymysql for python 3 is not a Django DB backend, however there is evidence that some work has been made porting the MySQL Backend to be Python 3 compatible at Django Python 3 MySQL backend changes.

Other pages show that the MySQL backend given with Django 1.5 works with Python 3 :
Django MySQL Works on Python 3.2.2

The default MySQL wrapper available on Python 2.x (mysql-python) does however not support Python 3.
I suspect the package at MySQL Python 3 to be compatible, you might give it a try.

Also make sure you’re running Django with Python 3.2 or 3.3 or above. Python 3.0 is not supported.

If it still does not work after these checks, please post your DATABASES settings in case something was wrong in it.

Also, I don’t find anything above Django 1.5 in the Django repos, are you sure it’s 1.6.x ?

75👍

I also struggled with making MySQL work with Django 1.6 and Python 3.3; the only thing that worked was to switch to PyMySQL. See my post on that here

Adding the answer below

My environment: OSX 10.9, Python 3.3.3, Django 1.6.1, PyMySQL 0.6.1, MySQL Server 5.5 on Windows

How to make it work:

  1. Install PyMySQL version 0.6.1 (https://github.com/PyMySQL/PyMySQL/): you can install it either by using pip, i.e. : pip install PyMySQL or by manually downloading the package; there is a good documentation on their website on how to do that.

  2. Open your Django App __init__.py and paste the following lines:

    import pymysql
    pymysql.install_as_MySQLdb() 
    
  3. Now, open settings.py and make sure your DATABASE property looks like this:

    DATABASES = {
       'default': {
           'ENGINE': 'django.db.backends.mysql',
           'NAME': 'mydb',
           'USER': 'dbuser',
           'PASSWORD': 'dbpassword',
           'HOST': 'dbhost',
           'PORT': '3306'
        }
    }
    
  4. That’s it, you should be able to execute python manage.py syncdb to init your MySQL DB; see the sample output below:

    Creating tables ...
    Creating table django_admin_log
    Creating table auth_permission
    Creating table auth_group_permissions
    ...
    ...
    Creating table socialaccount_socialtoken
    
    You just installed Django's auth system, which means you don't have any superusers defined.
    ...
    

7👍

I’ve been waiting for mysql support with django and python 3.x for ages and finally there is a somewhat official compatible engine that let’s you do this.

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

I created a virtualenv using python 3 and was able to use the database specific settings in the above link.

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

Cut and pasted the models from the Django Poll Tutorial and ‘python manage syncdb’ worked.

More testing to see if the entire ORM supports this connector.

My full write up here:
http://bunwich.blogspot.ca/2014/02/finally-mysql-connector-that-works-with.html

6👍

I fixed this problem by this:

According to the documentation of Django 1.8 and 1.9, mysqlclient is the recommended driver:

MySQLdb is a native driver that has been developed and supported for over a decade by Andy Dustman.

mysqlclient is a fork of MySQLdb which notably supports Python 3 and can be used as a drop-in replacement for MySQLdb. At the time of this writing, this is the recommended choice for using MySQL with Django.

MySQL Connector/Python is a pure Python driver from Oracle that does not require the MySQL client library or any Python modules outside the standard library.

So, I installed mysqlclient by

sudo pip install mysqlclient

Config the database in settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydjango',
        'USER': 'user',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': '3306'
    }
}

But when I use ./manage.py syncdb for testing, I received an error saying:

_mysql.so not found

To fix this, add

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

to ~/.bashrc

Then run:

source ~/.bashrc

It should be fixed now.

Leave a comment