CThomas
CThomas

Reputation: 199

I can't connect MySQL version 8 to Django 2.2 project

I have tried pretty much everything, including scouring StackOverflow to solve this issue yet I still seem to be faced with the problem. I am trying to connect my Django 2.2.6 project to my MySQL version 8 database with zero luck.

I have changed the settings.py file to contain the following key value pairs in the databases dictionary:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'portfoliodb',
        'USER': 'c**********s',
        'PASSWORD': 'XXX',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

typing

python manage.py runserver

returns the following error:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

I do in fact have mysqlclient, typing

pip install mysqlclient

returns the following:

requirement already met (1.4.4)

I have also tried

pip install pymysql

And then edited the init.py file to have the following code:

import pymysql

pymysql.install_as_MySQLdb()

Whilst keeping my databases variable the same as above. Doing this produces the following error:

django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

I have the MySQL python connector downloaded and installed onto my system.

I have tried giving the empty ALLOWED_HOSTS variable within settings.py an argument of my localhost.

The MySQL server is running fine outside of Django and can be accessed on the terminal very successfully.

I replaced Engine in the Databases variable with:

mysql.connector.django

Which produced the following error code:

django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'postgresql', 'sqlite3'

Upvotes: 4

Views: 4297

Answers (1)

CThomas
CThomas

Reputation: 199

I managed to answer my own question here, and considered deleting the post however it may help someone else out there. I ran the command

pip install mysql-connector-python

Prior to changing the engine value within the DATABASES dictionary to:

'ENGINE': 'mysql.connector.django',

The MySQL database finally connected to my Django project

Upvotes: 9

Related Questions