将MySQL连接器与Python一起使用时SSL连接错误 [英] SSL Connection Error while using MySQL Connector with Python
问题描述
我在Anaconda的环境中使用Python 3.6(但在python 2.7中也遇到相同的错误)和mysql-connector-python编写简单的脚本来访问Hostgator中托管的数据库.这是错误:
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)
我在其他计算机上多次使用了该库,我也通过计算机使用相同的库连接到了该数据库,并且该库始终与该代码一起使用.我尝试使用MySQL Workbench,似乎它正在使用代码中相同的凭据连接到数据库.我已经尝试向服务器支持寻求帮助,我的IP被允许访问数据库,即使这样我也无法与Python连接.我试图重新安装该库,但没有任何更改.
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.
谢谢大家!
推荐答案
这对我有用(Ubuntu 16.04 LTS).从终端:
This worked for me (Ubuntu 16.04 LTS). From terminal:
- 重新创建证书(您可以选择数据目录):
mysql_ssl_rsa_setup --datadir=/data/dir/
- 将以下内容添加到
/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
- 重新启动mysql服务器:
sudo service mysql restart
这篇关于将MySQL连接器与Python一起使用时SSL连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!