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

哈曼·辛格(Harman Singh):

我一直在尝试将在Google App Engine上运行的Python应用程序与在IBM Cloud上运行的External MySQL db连接起来。相同的代码在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中遇到的错误

在此处输入图片说明


这是一个标准环境

在此处输入图片说明

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

哈林德·辛格(Harjinder Singh):
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 '时未从其他模块导入的代码中,该代码才会运行

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将NetBeans连接到MySQL数据库?

从Python Google App Engine连接到Firebase

如何将W10 Universal App与MySQL数据库连接

如何将Azure Web App连接到Azure SQL数据库

将Django连接到Google App Engine中的Google CloudSQL Postgres数据库时出错

将Heroku App连接到外部数据库

将Google Cloud App Engine应用程序连接到其Cloud SQL实例-允许数据库访问

如何从连接到外部数据库的外部php文件向扩展程序的content-script.js发送数据?

App Engine无法连接到gcloud sql数据库

如何从Google App Maker作为外部mysql数据库连接到Google Cloud SQL数据库?

使用WPDB连接到外部数据库以使用php插入动态表单数据

如何将Azure App Service Web App连接到Google SQL Server数据库?

无法将Azure App Services中的NodeJS应用程序连接到Azure MySQL数据库

如何将Blazor C#App连接到Azure SQL数据库?苹果系统

将Sproutcore App连接到MySQL数据库

在Azure中使用ODBC驱动程序连接到外部数据库

将PostgreSQL数据库连接到使用Angular-fullstack生成的Web App

将APIMAN连接到外部ElasticSearch数据库以获取指标

连接到外部数据库时,如何在Spotfire中使用OVER语句?

如何将GitHub存储库连接到Google App Engine?

如何在Swift 3.0中连接到外部数据库

如何将 SQL 图像列链接到外部数据库?

将 Unity App 连接到 mySQL 数据库

如何创建连接到外部数据库的 Drupal 表单模块

将 MongoDB shell 连接到外部数据库

无法将 App Engine 上的 Laravel 应用程序连接到 Cloud SQL 数据库

Google App Maker 可以连接到 Azure 中的 MS SqlServer 数据库吗?

将 Google App Engine 连接到 GCE 上的 PostgreSQL 数据库

从 Google Cloud Run 连接到外部(Google Cloud Platform 之外)PostgreSQL 数据库