MySQL Connector/Python as Django Engine?
Asked Answered
P

2

7

Can't find an answer for this even after hours and hours of googling & searching stack overflow. I assure you I've seen all answers that could be deemed relevant and none of those have solved the issue I'm facing. Without further ado -

currently in shell I can do this:

Python 2.7.11+ (default, Apr 17 2016, 14:00:29)
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information. 
>>> from distutils.sysconfig import get_python_lib
>>> print get_python_lib()
/usr/lib/python2.7/dist-packages
>>> import mysql.connector
>>> db = mysql.connector.connect(user='root', password='test123', host='127.0.0.1', database='mydb')
>>> db
<mysql.connector.connection.MySQLConnection object at 0x7fd3a80536d0>

verifying that I have this module installed. However when I try to go to the settings.py file to set DATABASE ENGINE to be

DATABASES = {
    'default': {
        'ENGINE': 'mysql.connector.django',
        'NAME': 'mydb',
        'USER': 'root',
        'PASSWORD': 'test123',
        'HOST': '127.0.0.1',
        'PORT': '3306'
    }
}

and try to start my server i keep getting the error:

django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django'
isn't an available database backend.

not sure how to fix this. I'm on Django version 1.9.7, Python version shown in code snippet above

What would be the difference between using 'mysql.connector.django' vs. using 'django.db.backends.mysql' ?

Patency answered 16/6, 2016 at 0:37 Comment(2)
What mysql.connector version do you have installed?Pinky
pip install mysql-connector-django should help.Massotherapy
C
3

Here is my successful solution after one day's searching: install the following packages first

django==2.2.15
mysql-connector-python==8.0.23
django-mysql==3.9.0

Then you can configure in settings file

DATABASES = {
    'default': {
        'ENGINE': 'mysql.connector.django',
        'NAME': 'mydb',
        'USER': 'root',
        'PASSWORD': 'test123',
        'HOST': '127.0.0.1',
        'PORT': '3306'
    }
}
Concubinage answered 29/6, 2021 at 10:23 Comment(1)
mysqlclient is the recommending package, however, for some situation we couldn't install the mysqlclientConcubinage
G
2

What would be the difference between using 'mysql.connector.django' vs. using 'django.db.backends.mysql' ?

To begin with the former is the recommended method while the latter is not. The former is fully supported but the latter you need version 1.1.x and there is also a caveat in the django documentation that it may not be fully supported by more recent versions of django.

MySQL DB API Drivers

The Python Database API is described in PEP 249. MySQL has three prominent drivers that implement this API:

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. All these drivers are thread-safe and provide connection pooling. MySQLdb is the only one not supporting Python 3 currently.

In addition to a DB API driver, Django needs an adapter to access the database drivers from its ORM. Django provides an adapter for MySQLdb/mysqlclient while MySQL Connector/Python includes its own.

Gastronome answered 16/6, 2016 at 2:8 Comment(4)
How is the former "fully supported" when I can't even get my Django server to use it? django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend.Patency
That is because you have not installed the driverGastronome
@e4c5, 'mysql.connector.django' doesn't work with django 1.11Septennial
@Septennial the question is for django 1.9 from a year ago.Gastronome

© 2022 - 2024 — McMap. All rights reserved.