如何使用 Python 将 Google App Engine 连接到外部 Mysql 数据库

How to connect Google App Engine to External Mysql db using Python

我一直在尝试将我在 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

中遇到的错误


这是标准环境

_---------------------------------------- ------------------------------

现在可以使用了,我没有在 google 云引擎中调用 app() ,相应地修改后连接成功

使用插入 SQL 命令修改代码 ::::::::

from flask import Flask, jsonify, request
import pymysql.cursors
#import logging

#from flask import Flask


app = Flask(__name__)

# Connect to th#e database.
@app.route('/')
def home():
    connection = pymysql.connect(host='XXXXXX',
                             database='XXXX',
                             user='XXXX',
                             password='XXXXX',
                             port=XXXX,
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)
 
    print ("connect successful!!")
 
    try:
  
        with connection.cursor() as cursor:
        # SQL 
            sql = "Insert into songs values(2,'Radioactive','Imagine Dragons','ROCK') "
         
            # Execute query.
            cursor.execute(sql)
        connection.commit()
            #print ("cursor.description: ", cursor.description)
 
            #print()
 
            #for row in cursor:
             #   print(row)
             
    finally:
        # Close connection.
        connection.close()
    
    return "Connection Sucessfull"
if __name__ == '__main__':
    # This is used when running locally. Gunicorn is used to run the
    # application on Google App Engine. See entrypoint in app.yaml.
    app.run()

非常感谢大家的帮助!!

from flask import Flask, jsonify, request
import pymysql.cursors

app = Flask(_name_)

# Annotation that direct app engine to / route.
@app.route('/')
def home():
    connection = pymysql.connect(host='XXXXXX',
                                database='XXXX',
                                user='XXXX',
                                password='XXXXX',
                                port=XXXX,
                                charset='utf8mb4',
                                cursorclass=pymysql.cursors.DictCursor)

    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()

    return "Connection Sucessful"
if _name_ == '_main_':
    # This is used when running locally. Gunicorn is used to run the
    # application on Google App Engine. See entry point in app.yaml.
    app.run()

只有在检查 name = 'main[=16= 时没有从其他模块导入时,这里的代码才会 运行 ]'.