When I try to connect to MySQL with mysql-connector-python, I cannot connect with the error "SSL connection error: SSL_CTX_set_tmp_dh failed"

Introduction

When I try to connect to a MySQL server started from a Docker image using Python's "mysql-connector-python" library, I get a "** SSL connection error: SSL_CTX_set_tmp_dh failed **" error and cannot connect to MySQL. Has occurred.

test.py


import mysql.connector

conn = mysql.connector.connect(
    host='127.0.0.1',
    port='3306',
    user='user',
    password='password', 
    database='sample_db'
)
cursor = conn.cursor()

↓ After execution

Error message


Traceback (most recent call last):
  File "/Users/user1/opt/anaconda3/lib/python3.7/site-packages/mysql/connector/connection_cext.py", line 200, in _open_connection
    self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: SSL connection error: SSL_CTX_set_tmp_dh failed

# //abridgement

I will leave it as a memorandum because it is ready to connect once.

Execution environment

** ■ Client **

$ conda list --show-channel-urls
mysql-connector-c         6.1.11               hccea1a4_0
mysql-connector-python    8.0.18           py37h3febbb0_1
openssl                   1.1.1d               h1de35cc_3
python                    3.7.4                h359304d_1

** ■ MySQL server ** --MySQL 5.7 (default setting)

Countermeasures

I specified use_pure = True as an argument when creating a DB connection, and changed the module to be used to the genuine Python version instead of the default C extended version, and now I can connect.

7.1 Connector/Python Connection Arguments

test.py


import mysql.connector

conn = mysql.connector.connect(
    host='127.0.0.1',
    port='3306',
    user='user',
    password='password',
    database='sample_db',
    use_pure=True  #← Add
)
cursor = conn.cursor()

As far as I can see, is this error a compatibility issue between mysql-connector-python and openssl? It seems that it is caused by. I haven't tried it in my environment, but according to the following issue, there is a comment that downgrading the openssl version to 1.0.2 made it possible to connect.

-MySQLdb and mysql.connector are incompatible with openssl 1.1.1a # 10646

In addition, it seems that the performance will be improved by using the C implementation version compared to the genuine Python, so be careful when handling a large amount of data.

-Chapter 8 The Connector / Python C Extension -SSL Error when connecting on Linux / Mac using Openssl 1.1.1 # 22n

Recommended Posts

When I try to connect to MySQL with mysql-connector-python, I cannot connect with the error "SSL connection error: SSL_CTX_set_tmp_dh failed"
When I try to install mysqlclient with Django, I get error: command'gcc' failed with exit status 1.
I get [Error 2055] when trying to connect to MySQL on Heroku
When I import TensorFlow to Python, I get "Import Error: DLL load failed: The specified module cannot be found."
When I try to import pandas on macOS I get the error No module named'_bz2'
When I try to connect django and postgresql with Docker, I get the error "django.db.utils.OperationalError: could not translate host name" db "to address: Name or service not known"
When I try to update the data on DynamoDB with Python (boto3), I get "ExpressionAttributeNames contains invalid key: Syntax error; key: <key name>"
When I try to push with heroku, it doesn't work
What to do when an error occurs with import _ssl
[Solution] When I try to connect to CloudSQL with GAE, I get an ImportError only when using dev_appserver.
How to deal with the error "Failed to load module" canberra-gtk-module "that appears when you run OpenCV
I get an error when trying to install maec 4.0.1.0 with pip
When I try to use pip, SSL module is not available.
When WSL Distro fails to start (error: "WslRegisterDistribution failed with error: 0x800706be.")
When I try to execute the make command of Makefile with os / exec of golang, the second and subsequent executions result in an error.
How to deal with SSL error when connecting to S3 with boto of Python
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
Connect to MySQL with Python within Docker
When I get an error with PyInstaller
[Python] I want to know the variables in the function when an error occurs!
When I name the file flask.py in Flask, I get Import Error: cannot import name'Flask'
When I tried to change the root password with ansible, I couldn't access it.
I failed to install django with pip, so a reminder of the solution
Three things I was addicted to when using Python and MySQL with Docker
When I connect to a remote Jupyter Server with VScode, it's remote but local
When I tried to connect with SSH, I got a warning about free space.
Try to solve the fizzbuzz problem with Keras
I got an error when saving with OpenCV
I tried to save the data with discord
I wanted to play with the Bezier curve
Connect to MySQL with Python on Raspberry Pi
A story that failed when trying to remove the suffix from the string with rstrip
I get an error when I try to raise Python to 3 series using pyenv on Catalina
I want to be notified of the connection environment when the Raspberry Pi connects to the network