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.
** ■ 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)
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