【问题标题】:How to connect Google App Engine to External Mysql db using Python如何使用 Python 将 Google App Engine 连接到外部 Mysql 数据库
【发布时间】: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


【解决方案1】:
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 = 'ma​​in' 时没有从其他模块导入时才会运行。

【讨论】:

    【解决方案2】:

    它的工作现在,我没有在谷歌云引擎中调用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()
    

    非常感谢大家的帮助!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 2020-12-18
      • 2020-02-16
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      相关资源
      最近更新 更多