Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSL Connection Error while using MySQL Connector with Python

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!

like image 803
Julio Avatar asked Feb 01 '19 18:02

Julio


People also ask

How do I fix SSL connection error in MySQL?

right-click on the particular MySQL instance and select "Edit Connection" Select the "SSL" tab under Connection Method. Select the drop-down for the "Use SSL" and choose "If Available" instead of "Required". Click the "Test Connection" button at the lower right connection to make sure you can now connect without errors ...

How do I enable SSL on MySQL server?

Enable SSL Connections on MySQL Now, connect to the MySQL shell and check the status with the following command: mysql -u root -p --ssl-mode=required mysql> SHOW VARIABLES LIKE '%ssl%'; You should see that both have_openssl and have_ssl variables are now enabled.

What is SSL connection in MySQL?

MySQL supports encrypted connections between clients and the server using the TLS (Transport Layer Security) protocol. TLS is sometimes referred to as SSL (Secure Sockets Layer) but MySQL does not actually use the SSL protocol for encrypted connections because its encryption is weak (see Section 6.3.

How to install MySQL Connector in Python?

The default username for the MySQL database is a root. Password is given by the user at the time of installing the MySQL server. If you are using root then you won’t need the password. The name of the database to which you want to connect and perform the operations. Use the pip command to install MySQL connector Python.

What are the advantages of using MySQL Connector Python?

Advantages and benefits of MySQL Connector Python: – 1 MySQL Connector Python is written in pure Python, and it is self-sufficient to execute database queries through Python. 2 It is an official Oracle-supported driver to work with MySQL and Python. 3 It is Python 3 compatible, actively maintained.

Does MySQL Connector/Python support sha256_password?

For example, if the server is configured to use sha256_password by default and you want to connect to an account that authenticates using mysql_native_password, either connect using SSL or specify auth_plugin='mysql_native_password' . MySQL Connector/Python does not support the old, less-secure password protocols of MySQL versions prior to 4.1.

How do I connect to SSL in Python?

Connecting through SSL Using SSL connections is possible when your Python installation supports SSL, that is, when it is compiled against the OpenSSL libraries. When you provide the ssl_ca, ssl_key and ssl_cert options, the connection switches to SSL, and the client_flags option includes the ClientFlag.SSL value automatically.


1 Answers

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

like image 162
HaMi Avatar answered Oct 25 '22 07:10

HaMi