1

I am trying to connect to Mysql server on localhost from my django application on Ubuntu 19.04. The database connection section in settings.py looks like this:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'app_datastore',
        'HOST' : '127.0.0.1',
        'USER': 'root',
        'PASSWORD': 'xxxx',
        'OPTIONS': {
                'ssl': {
                    'ca': '/var/lib/mysql/ca.pem',
                    'cert': '/var/lib/mysql/client-cert.pem',
                    'key': '/var/lib/mysql/client-key.pem'

                    }
            }
    }
}

It gives the error :

django.db.utils.OperationalError: (2026, 'SSL connection error: SSL_CTX_set_tmp_dh failed')

I can however connect to my mysql from both terminal and Dbeaver. mysql server is up.

Stackoverflow threads which I have already tried and doesnt work :

django.db.utils.OperationalError: (2026, 'SSL connection error: SSL_CTX_set_tmp_dh failed')

SSL Connection Error while using MySQL Connector with Python

Python SQL connection error (2006, 'SSL connection error: SSL_CTX_set_tmp_dh failed')

Here are my pip list results :

Package Version


cffi 1.14.0 cryptography 2.8
Django 2.2.2
mysqlclient 1.4.6
Pillow 7.0.0
pip 20.0.2 pycparser 2.19
pyOpenSSL 19.0.0 pytz 2019.3 setuptools 45.2.0 six 1.14.0 sqlparse 0.3.0
wheel 0.34.2

1 Answer 1

1

Adam Johnson's blog linked here provided the resolution for me (I'm running Django 3.0 w/Python 3.7.4)

In my settings.py file I added the following lines per his suggestion:

# near the top
import pymysql

# then after the DATABASES entry further down...
DATABASES = { 
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'somedatabase',
         ...
}

# add these two lines... 
pymysql.version_info = (1, 4, 2, "final", 0)
pymysql.install_as_MySQLdb()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.