【发布时间】:2021-01-02 14:57:11
【问题描述】:
我一直在尝试将我在 Google App Engine 上运行的 Python 应用程序与在 IBM Cloud 上运行的外部 MySQL 数据库连接起来。 相同的代码在 localhost 上运行得非常好,但是当我在 App Engine 上运行它时,它会响应 502 BAD GATEWAY 错误。 有没有办法构建它?
下面是我一直在尝试的简单代码
import pymysql.cursors
# Connect to the database.
connection = pymysql.connect(host='XXXXXXX',
database='XXX',
user='XXX',
password='XXXX',
port=XXX)
print ("connect successful!!")
try:
with connection.cursor() as cursor:
# SQL
sql = "SELECT * from songs "
# Execute query.
cursor.execute(sql)
print ("cursor.description: ", cursor.description)
print()
for row in cursor:
print(row)
finally:
# Close connection.
connection.close()
以下是我在 Google Cloud Logs 中遇到的错误
这是一个标准环境
_------------------------------------------------ -----------------------------
【问题讨论】:
-
您的日志中有任何错误消息吗? console.cloud.google.com/logs/viewer
-
1) 什么是报告502错误?您的 AppEngine 代码、浏览器等。 2) IBM Cloud Database for MySQL 不使用 HTTP 代理。如果连接语句报告错误,请仔细检查 IP 地址和端口号。
-
以下是Google Cloud Log Viewer上的日志::::::: A 2020-09-15T21:42:38.375819Z 连接成功!! A 2020-09-15T21:42:38.394871Z {'song_id': 1, 'title': 'demons', 'artist': 'Imagine Dragons', 'genre': 'POP'} A 2020-09-15T21: 42:38.395025Z {'song_id': 1, 'title': 'demons', 'artist': 'Imagine Dragons', 'genre': 'POP'} A 2020-09-15T21:42:38.395329Z 找不到'main' 中的属性 'app'。
-
请编辑您的问题并添加所需信息,在评论部分格式不是很好理解信息所必需的,还请添加具体错误以及您在哪里看到的.
-
感谢您更新帖子,通过查看错误,问题似乎与您的代码应用程序有关,而不是与 MySQL 连接有关。您使用的是 App Engine flex 还是标准?请使用您的
app.yaml文件更新您的问题,如果可能,请使用您应用的minimal reproducible example 代码
标签: python mysql google-app-engine google-cloud-platform ibm-cloud