我正在使用Python创建一个简单的Azure函数。它只是简单地从一个azuremysql实例中读取数据,然后将内容写回同一个实例。但是,我无法成功连接到数据库。在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
)
....
当我使用func host start时,代码在我的本地机器上运行得很好,我也使用了相同的azuremysql实例。在
但是,在我部署了Azure函数之后,它不起作用,并给出了以下错误:
^{pr2}$
我还尝试禁用azureportal中的“SSL强制”,并启用了Allow access to Azure services,这是没有帮助的。在
任何帮助和意见将不胜感激!谢谢!在