0

I want to integrate MySQL with Django, MySQL is running over Docker and I put the config like this to connect to the db docker with Django:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'restaurant',
        'HOST': 'db',
        'PORT': '3306',
        'USER': 'root',
        'PASSWORD': 'root',
        'default-character-set': 'utf8',
        'OPTIONS': {
            'init_command': 'SET default_storage_engine=INNODB',
        }
    }
}

But when Django is trying to connect to the mysql db, it throws me this error:

enter image description here

I tried to install mysqlclient with pip, but I have this error:

enter image description here

These are the docker-compose.dev.yml and Dockerfile configs.

If someone needs the complete code, here you can find it, and test it with docker-compose -f docker-compose.dev.yml up --build.

Thanks :).

Andrés Montoya
  • 4,079
  • 4
  • 18
  • 33
  • 4
    Possible duplicate of [mysql\_config not found when installing mysqldb python interface](https://stackoverflow.com/questions/7475223/mysql-config-not-found-when-installing-mysqldb-python-interface) – Siyu Dec 19 '18 at 16:13
  • 1
    Just in case that comment was too cryptic, the issue is that to install `mysqlclient` on alpine, you also have to install the native mysql libraries. The referenced answer contains instructions for how to use apk to install the mysql client libraries you'll need to install `mysqlclient`. Alternatively, you can switch your config over to use mysql-connector-python, which does not need the native libraries installed. – 2ps Dec 19 '18 at 16:50

2 Answers2

5

mysqlclient has native dependencies that must be installed before you can pip install it. When running in docker, and especially in alpine, you probably want to switch over to using mysql-connector-python which is a pure python library that does not have any native dependencies l,ike mysqlclient. Update your requirements file and update your settings to use mysql.connector.django if you want to use mysql-connector-python.

2ps
  • 15,099
  • 2
  • 27
  • 47
  • I could resolve it running in the dockerfile: `RUN apk add --no-cache bash mariadb-dev mariadb-client mariadb-libs python3-dev build-base` But I'm going to try `mysql-connector-python`, but for more complex queries or to have a better performance, I need to install `mysqlclient`? or with `mysql-connect-python` is ok? – Andrés Montoya Dec 19 '18 at 16:57
  • 1
    native will always have better performance. Complex queries will run just as slow in mysqlclient as in mysql-connector-python. Biggest performance increase is for large queries because native string processing is much faster than python string processing. But, I;ve only noticed a 2-5% dropoff in performance using mysql-connector-python. – 2ps Dec 19 '18 at 17:00
0

Another response would be adding:

RUN apk add --no-cache bash mariadb-dev mariadb-client mariadb-libs python3-dev build-base

On the dockerfile.

Andrés Montoya
  • 4,079
  • 4
  • 18
  • 33
  • 1
    have the same problem. I put this line into my dockerfile but error occured: `ERROR: unsatisfiable constraints: mariadb-libs (missing): required by: world[mariadb-libs] The command '/bin/sh -c apk add --no-cache bash mariadb-dev mariadb-client mariadb-libs python3-dev build-base' returned a non-zero code: 1` – Coconutcake Dec 16 '19 at 09:21