Python MySQL 连接器不适用于 SSL

标签 python mysql python-2.7 mysql-python

我最近在我的 MySQL 服务器上配置了 SSL,它似乎破坏了我的 python 应用程序。我可以通过命令行连接到服务器:

user@kiosk:~$ mysql -u <user> -p -h <remote host>
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

而且 SSL 似乎正在处理连接:

mysql> \s
--------------
mysql  Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using  EditLine wrapper

Connection id:          19
Current database:
...
SSL:                    Cipher in use is DHE-RSA-AES256-SHA
....

Threads: 1  Questions: 17  Slow queries: 0  Opens: 106  Flush tables: 1  Open tables: 99  Queries per second avg: 0.007

用户配置为需要 SSL,我目前没有在我的客户端上使用 CA/client-cert/client-key 进行连接(据我所知,这是可选的)。

我的 python 脚本使用了一个简单的连接:

db_ip_address = raw_input('Ip Address: ')
db_username = raw_input('Username: ')
db_passwd = raw_input('Password: ')

try:
    db = mysql.connector.connect(
         host=db_ip_address,
         user=db_username,
         passwd=db_passwd,
    )
    cursor = db.cursor()
    connected = True

except Exception as e:
    installer.fatal('\r\n Failed to connect to SQL server please try again \r\n')
    print traceback.format_exception_only(type(e), e)[0]

现在失败并出现以下错误:

ProgrammingError: 1045 (28000): Access denied for user '<user>'@'<IP>' (using password: YES)

我假设这是由于我的 SSL 配置,但我不完全确定为什么在 mysql CLI 中一切正常,但在我的脚本中却不行?

如有任何见解,我们将不胜感激。

最佳答案

以下错误是因为错误的登录信息,例如您的用户名或密码或您的 IP 地址。 如果您的用户名和密码正确,请检查数据库中允许的 IP,如 % 或 localhost 或 127.0.0.1。 对于 ssl 连接,请检查以下代码:

import sys

#sys.path.insert(0, 'python{0}/'.format(sys.version_info[0]))

import mysql.connector
from mysql.connector.constants import ClientFlag

config = {
    'user': 'ssluser',
    'password': 'password',
    'host': '127.0.0.1',
    'client_flags': [ClientFlag.SSL],
    'ssl_ca': '/opt/mysql/ssl/ca.pem',
    'ssl_cert': '/opt/mysql/ssl/client-cert.pem',
    'ssl_key': '/opt/mysql/ssl/client-key.pem',
}

cnx = mysql.connector.connect(**config)
cur = cnx.cursor(buffered=True)
cur.execute("SHOW STATUS LIKE 'Ssl_cipher'")
print(cur.fetchone())
cur.close()
cnx.close()

来自 here

here如何使用 ssl 设置你的 mysql

关于Python MySQL 连接器不适用于 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55221940/

相关文章:

python - 使用 Tenor 的 API

python - pyqtgraph 中的动画图表

python-2.7 - 网页中元素的 XPath/CSS 选择器是什么

python - 不区分大小写的 xml 和 python

Java RESTFUL-服务返回 500 内部服务器错误( Jersey )

python - 如何在Python中从文本文件中单独分割数字?

Python 相当于 matlab corr2

python - 当我调用以下 django api 代码将文件上传到 Azure Blob 存储时,出现 "encoding with ' idna ' codec failed (UnicodeError: label too long)"

php - 使用 QuickBooks PHP Dev Kit/QuickBooks Web Connector 从 QuickBooks 导入项目

java - 插入多行 - java 准备好的语句失败