简体   繁体   English

Azure 函数 (Python) 无法使用 [SSL: WRONG_VERSION_NUMBER] 连接到 Azure MySQL 实例

[英]Azure Functions (Python) cannot connect to Azure MySQL instance with [SSL: WRONG_VERSION_NUMBER]

I'm creating a simple Azure Function using Python.我正在使用 Python 创建一个简单的 Azure Function。 It just simply read from an Azure MySQL instance and write something back to the same instance.它只是简单地从 Azure MySQL 实例中读取并将某些内容写回同一实例。 However, I cannot connect to the database successfully.但是,我无法成功连接到数据库。

import logging
from datetime import datetime

import azure.functions as func
import mysql.connector
from mysql.connector import errorcode

def connect_to_db():
    logging.info(os.getcwd()) 
    try:
        db_conn = mysql.connector.connect(
            user="...", 
            password='...', 
            host="....mysql.database.azure.com", 
            port=3306, 
            database='...'
        )
        return db_conn
    except mysql.connector.Error as err:
        if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            logging.error("Something is wrong with your user name or password")
        elif err.errno == errorcode.ER_BAD_DB_ERROR:
            logging.error("Database does not exist")
        else:
            logging.error(err)

    return None


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    db_conn = connect_to_db()
    if db_conn:
        logging.info('DB Connection is established {}'.format(db_conn))
    else:
        return func.HttpResponse(
             "Azure Functions cannot connect to MySQL database.",
             status_code=500
        )

    ....

The code works fine on my local machine when I use func host start , I did use the same Azure MySQL instance as well.当我使用func host start时,代码在我的本地机器上运行良好,我也使用了相同的 Azure MySQL 实例。

However, after I deployed the Azure Function, it doesn't not work and give me the error below:但是,在我部署 Azure Function 后,它不起作用并给我以下错误:

2055: Lost connection to MySQL server at '....mysql.database.azure.com:3306', 
system error: 1 [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:852)

I also tried disable the "SSL enforced" in Azure portal, and enabled Allow access to Azure services , which are not helpful.我还尝试在 Azure 门户中禁用“SSL 强制”,并启用Allow access to Azure services ,这没有帮助。

Any help and comments will be appreciated!任何帮助和意见将不胜感激! Thanks!谢谢!

I solved the issue by myself eventually.我最终自己解决了这个问题。

Here are the steps:以下是步骤:

  1. Download the SSL certificate from Azure document repo.从 Azure 文档存储库下载 SSL 证书。 You can get it here: https://docs.microsoft.com/en-us/azure/mysql/howto-configure-ssl你可以在这里得到它: https://docs.microsoft.com/en-us/azure/mysql/howto-configure-ssl

  2. Put the certificate file along with the Azure Function project.将证书文件与 Azure Function 项目一起放置。 For me, I put it in the root folder of the project because I'll likely to have multiple functions in this project that will require this certificate对我来说,我把它放在项目的根文件夹中,因为我可能在这个项目中有多个需要这个证书的功能

  3. Then, acquire the certification file in the python code as below然后,获取python代码中的认证文件如下

    import pathlib def get_ssl_cert(): current_path = pathlib.Path(__file__).parent.parent return str(current_path / 'BaltimoreCyberTrustRoot.crt.pem')
  4. So, I can use the SSL certificate when I connect to MySQL所以,当我连接到 MySQL 时,我可以使用 SSL 证书

    # Connect to MySQL cnx = mysql.connector.connect( user="ctao@azure-mysql-test", password='*******', host="azure-mysql-test.mysql.database.azure.com", port=3306, database='ciscoepcstats', ssl_ca=get_ssl_cert() )

PS Disable the SSL verification is not an option for me because of the security concerns. PS 出于安全考虑,禁用 SSL 验证对我来说不是一个选项。 But luckily I found the solution.但幸运的是我找到了解决方案。

Please modify your code as below:请修改您的代码如下:

db_conn = mysql.connector.connect(
            user="...", 
            password='...', 
            host="....mysql.database.azure.com", 
            port=3306, 
            database='...',
            ssl_disabled=True
        )

and change the status of "Enforce SSL connection" to "DISABLED" in your azure mysql on azure portal.并在 azure mysql 上 azure 中将“强制 SSL 连接”的状态更改为“禁用”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SSL:PYTHON 请求上的 WRONG_VERSION_NUMBER - SSL: WRONG_VERSION_NUMBER ON PYTHON REQUEST Python-Django [SSL:WRONG_VERSION_NUMBER]错误 - Python-Django [SSL: WRONG_VERSION_NUMBER] Error python 3 smtplib 异常:“SSL:WRONG_VERSION_NUMBER”登录到 Outlook - python 3 smtplib exception: 'SSL: WRONG_VERSION_NUMBER' logging in to outlook Python cassandra驱动程序-SSL:WRONG_VERSION_NUMBER - Python cassandra driver - SSL: WRONG_VERSION_NUMBER jupyter SSL:WRONG_VERSION_NUMBER - jupyter SSL: WRONG_VERSION_NUMBER python 请求:(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] 错误的版本号 (_ssl.c:1123)')) - python requests: (SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123)')) python elasticsearch elasticsearch.exceptions.SSLError: ConnectionError([SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)) - python elasticsearch elasticsearch.exceptions.SSLError: ConnectionError([SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1056)) Python 错误 SSL: WRONG_VERSION_NUMBER 在几天前工作的代码上 - Python ERROR SSL: WRONG_VERSION_NUMBER on code that worked few days earlier Flask-Mail [SSL: WRONG_VERSION_NUMBER] 错误的版本号 (_ssl.c:1123) - Flask-Mail [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1123) 如何修复错误 ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] 版本号错误? - How to fix the error ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM