[Django]-Cannot connect Django to existing MySQL database on M1 mac

4๐Ÿ‘

โœ…

So I found a solution to my problem! I think the problem is that there are some compatibility issues between the MySQLdb module and the M1 mac I am using. But I could connect the database and import the model using PyMySQL instead!
The steps I took are the following:

  • Change my settings.py database section to this:
import pymysql
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'mysql.connector.django',
        'NAME': 'climate',
        'USER': 'root',
        'PASSWORD': 'yourpassword',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}
# Fake PyMySQL's version and install as MySQLdb
# https://adamj.eu/tech/2020/02/04/how-to-use-pymysql-with-django/
pymysql.version_info = (1, 4, 2, "final", 0)
pymysql.install_as_MySQLdb()
  • run `pip install pymysql
  • then run python manage.py inspectdb > models.py
  • then run python manage.py migrate

Done!

๐Ÿ‘คAaron

0๐Ÿ‘

I found there is an issue using pymysql because my Django project is using JSONField and the app is crashing with error that it requires MySQL 5.7+ version, even when my MySQL server version is 8.1. However I found that mysql-connector-python solves this for me. Run:

pip install mysql-connector-python

to get the module installed and configure your DATABASES like this:

DATABASES = {
  'default': {
    'ENGINE': 'mysql.connector.django',
     # Rest of your configuration
  }
}

This seems to fix all issues with working with MySQL in Django on Mac Silicon processors.

๐Ÿ‘คAndrej

Leave a comment