ratul
ratul

Reputation: 11

django.db.utils.OperationalError: (2026, 'SSL connection error: SSL_CTX_set_tmp_dh failed') while connecting to localhost MYSQL

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

Upvotes: 1

Views: 3233

Answers (1)

Steve L
Steve L

Reputation: 1635

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()

Upvotes: 1

Related Questions