I'm creating a simple Azure Function using Python. It just simply read from an Azure MySQL instance and write something back to the same instance. 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.
However, after I deployed the Azure Function, it doesn't not work and give me the error below:
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.
Any help and comments will be appreciated! Thanks!