从GAE连接到两个cloudsql实例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从GAE连接到两个cloudsql实例相关的知识,希望对你有一定的参考价值。

我无法弄清楚将我的AEF环境连接到多个cloud_sql实例的语法。

这些是我尝试过的两个配置:

beta_settings:
    cloud_sql_instances: 
         - pore-gdic:europe-west1:a-django
         - pore-gdic:europe-west1:a-airflow-5

失败:

ERROR: (gcloud.app.deploy) Error Response: [13] Invalid Cloud SQL name: []

根据这个问题的回答:Connecting to multiple CloudSQL instances using Cloud sql proxy?

beta_settings:
     cloud_sql_instances: 
       pore-gdic:europe-west1:a-django,pore-gdic:europe-west1:a-airflow-5

部署时不会失败,但根本不能用作网页。

有没有人有这个问题的工作解决方案。

为了完整性,一个db运行mysql和其他postgres。所以端口不是问题。

答案

在GAE Flex环境中,您使用的是适用于app.yaml的正确语法:

beta_settings:
    cloud_sql_instances: 
        pore-gdic:europe-west1:a-django,pore-gdic:europe-west1:a-airflow-5

您的问题很可能是您在应用中使用了错误的连接网址。通过app.yaml中的上述内容,您将指示GAE使用Unix套接字连接到您的数据库。您的连接应该类似于以下内容:

mysql://USERNAME:PASSWORD@/DATABASE?unix_socket=/cloudsql/pore-gdic:europe-west1:a-django

postgresql://USERNAME:PASSWORD@/DATABASE?host=/cloudsql/pore-gdic:europe-west1:a-airflow-5

如果要在不更改内容的情况下在本地计算机上进行测试,可以安装cloud-sql proxy。这样在本地运行应用程序的方式与部署时相同。使用以下命令在应用之前启动代理:

./cloud_sql_proxy -dir=/cloudsql -instances=pore-gdic:europe-west1:a-django,pore-gdic:europe-west1:a-airflow-5

这将导致它在/cloudsql中创建套接字,这是上面的连接URL指定(例如:/cloudsql/pore-gdic:europe-west1:a-django)。

CloudSQL代理页面上的This部分提供了有关使用Unix套接字和TCP在使用多个实例时进行连接之间的区别的更多信息。

另一答案

因为我无法弄清楚这一点。

我将气流数据库暴露给0.0.0.0/0。此数据库中的信息不敏感,但为了阻止攻击者,我将root密码更新为1pass生成的64位密码:

类似于:XC$YvCBJ{avigR^LibD#Nn7i9MrU3qpH}GVcD(i4]9)Lg7)KZwT3xfQ)GW2z3rt4

这应该持续到太阳烧尽才能破裂。

然后我使用这个密码连接到mysql实例,主机ip,连接数据库。这些是从settings.py中读取的

就像是:

def trigger_dag_master():
"""
Trigger the dag_master dag running in airflow.
    Connect to cloudsql instance running airflow using cloudsql proxy.
    insert a row that triggers the dag master.
    based on an insert pattern found in: https://github.com/apache/incubator-airflow/blob/master/airflow/api/common/experimental/trigger_dag.py
:return: True if successful
"""

    def insert(connection: MySQLdb.Connect, query: str) -> bool:
        """
        Insert query into MYSQL database.
            handles rollback if failure
        :param connection: mysql connection
        :param query: query to execute
        :return: True if successful
        """
        try:
            cursor = connection.cursor()
            cursor.execute(query)
            connection.commit()
            return True
        except Exception as e:
            logger.error(e.with_traceback(e.__traceback__))
            connection.rollback()
            return False

assert all([settings.AIRFLOW_PASSWORD, settings.AIRFLOW_USER,
            settings.AIRFLOW_HOST, settings.AIRFLOW_DB])

mysql_connection = MySQLdb.connect(settings.AIRFLOW_HOST,
                                   settings.AIRFLOW_USER,
                                   settings.AIRFLOW_PASSWORD,
                                   settings.AIRFLOW_DB)
....

希望这有助于某人。

以上是关于从GAE连接到两个cloudsql实例的主要内容,如果未能解决你的问题,请参考以下文章

从 Heroku 上的 Node.js 连接到 CloudSQL

使用 Cloud sql 代理连接到多个 CloudSQL 实例?

如何通过 Exposed 连接到 Google Cloud SQL

尝试连接到 Cloud SQL 实例时出现 403

GAE + Cloud SQL - 如何理解不同的层

从 Dataflow 作业连接到 Cloud SQL