17

I'm using Python 3.6 (but I get the same error with Python 2.7) and mysql-connector-python in an Anaconda's environment to code a simple script to access my database hosted in Hostgator. This is the error:

Traceback (most recent call last):
  File "/home/usuario/anaconda3/envs/fakebook/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 176, in _open_connection
    self._cmysql.connect(**cnx_kwargs)
_mysql_connector.MySQLInterfaceError: SSL connection error: SSL_CTX_set_tmp_dh failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "teste.py", line 6, in <module>
    passwd="senha"
  File "/home/usuario/anaconda3/envs/fakebook/lib/python3.6/site-packages/mysql/connector/__init__.py", line 172, in connect
    return CMySQLConnection(*args, **kwargs)
  File "/home/usuario/anaconda3/envs/fakebook/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 78, in __init__
    self.connect(**kwargs)
  File "/home/usuario/anaconda3/envs/fakebook/lib/python3.6/site-packages/mysql/connector/abstracts.py", line 731, in connect
    self._open_connection()
  File "/home/usuario/anaconda3/envs/fakebook/lib/python3.6/site-packages/mysql/connector/connection_cext.py", line 179, in _open_connection
    sqlstate=exc.sqlstate)
mysql.connector.errors.InterfaceError: 2026 (HY000): SSL connection error: SSL_CTX_set_tmp_dh failed

My code is very simple, it just try to connect to the database:

import mysql.connector

mydb = mysql.connector.connect(
    host="192.xxx.xxx.xx",
    user="root",
    passwd="senha"
)

print(mydb)

I've used this library several times on other computers, I've also connected to this same database through my computer, using the same library and it always worked with this code. I tried with MySQL Workbench and seems it is connecting to the database using the same credentials that are in my code. I've already tried to ask help to the server support, my IP is allowed to access the database and even so I can't connect with Python. I tried to reinstall the library, but nothing changed.

Thank you everyone!

1
  • Try to set the ssl mode to none. Commented Nov 5, 2021 at 19:10

5 Answers 5

18

I had the same issue and resolved it by adding use_pure=True argument based a suggestion here:

import mysql.connector as sql

db_connection = sql.connect(host='****', database='****', user='****', password='****', use_pure=True)

Relevant packages on my mac: mysql-connector-python 8.0.16 and openssl 1.1.1b installed (both anaconda).

Sign up to request clarification or add additional context in comments.

3 Comments

This made it work for me too, on Mac OS, python3.7 mysql-connector-python 8.0.16. Thanks Hami!
This fixed my issue.I just installed mysql 8.0.18 on my ubuntu 18.04 drive and had this issue. Kudos for being the simplest method that I read.
Good solution without having to downgrade! :D
10

This only seems to happen when the mysql-connector-python library uses the C extensions instead of the pure python implementation.

Since version 8.0.11, the default changed and it now uses the C extensions if they're present.
https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html

use_pure argument description

One solution is to force the connector tu use the python implementation via that flag, just adding it to the connection url in your configuration:

url = 'mysql+mysqlconnector://user:password@mysql_server/database?use_pure=True'

1 Comment

No problem @Mysterio, just yesterday I also found this: docs.sqlalchemy.org/en/13/dialects/… > The MySQL Connector/Python DBAPI has had many issues since its release, some of which may remain unresolved, and the mysqlconnector dialect is not tested as part of SQLAlchemy’s continuous integration. The recommended MySQL dialects are mysqlclient and PyMySQL. So I'm switching to PyMySQL
5

This worked for me (Ubuntu 16.04 LTS). From terminal:

  1. re-create certificates (datadir is of your choice):

mysql_ssl_rsa_setup --datadir=/data/dir/

  1. Add the following to /etc/mysql/mysql.conf.d/mysqld.cnf:
ssl-ca=/data/dir/cacert.pem

ssl-cert=/data/dir/server-cert.pem

ssl-key=/data/dir/server-key.pem
  1. Restart mysql server: sudo service mysql restart

2 Comments

Why I am getting error in this mysql_ssl_rsa_setup --datadir=/data/dir/: 2019-11-21 08:45:15 [ERROR] Failed to access directory pointed by --datadir. Please make sure that directory exists and is accessible by mysql_ssl_rsa_setup. Supplied value : /data/dir
/data/dir is just my example of the path. You need to point --datadir= to actually existing directory on your system where you want to create your certificates. Or even better, see juan's solution below.
3

I ran into the same issue on my mac, I was running mysql-connector-python version 8.0.16, I fixed the issue by downgrading to version 8.0.5

2 Comments

Thanks, this worked for me. If you ever find out why or you learn they fix it in a future release, please edit your answer
I'm not sure, it's not working for me. PackagesNotFoundError: - mysql-connector-python=8.0.5 I think version 8.0.5 is not listed anymore
0

I had the exact same problem, but since I was unable to use mlcr's solution (it was an AWS RDS database) my solution was different. I had to change mysql-connector-python versions from 8.x.x to 2.2.3. That resolved my issue entirely. Hope this helps someone.

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.