简体   繁体   English

从 Firebase 函数使用 Cloud SQL 代理

[英]Using Cloud SQL Proxy from Firebase function

I'm running Google's Cloud SQL Proxy locally and it's working with locally served Firebase functions using a command like:我在本地运行 Google 的Cloud SQL 代理,它使用如下命令处理本地提供的 Firebase 函数:

/cloud_sql_proxy -instances=my-project-12345:us-central1:my-instance=tcp:1433

However I don't really know how to have this work on deployed Firebase functions.但是我真的不知道如何在部署的 Firebase 函数上进行这项工作。

export const typeOrmConnectionOptions: ConnectionOptions = {
  name: 'primary',
  type: 'mssql',
  host: '127.0.0.1',
  port: 1433,
  username: 'sqlserver',
  password: 'my$trongPa$$word',
  database: 'TestDB',
  synchronize: true,
  logging: true,
  entities: ['lib/entity/**/*.js'],
  ...(prod && {
    extra: {
      socketPath:
        '/cloudsql/my-project-12345:us-central1:my-instance=tcp',
      credential_file: './admin-service-account-file-long-a1b2c3-hash.json'
    }
  })
};

I really am taking shots in the dark as far as passing the credential file as part of the extras object to TypeORM's connections object, however I feel like something like this must be necessary to link the service account I created following this step to database queries.就将凭证文件作为 extras 对象的一部分传递给 TypeORM 的连接对象而言,我真的是在黑暗中拍摄,但是我觉得必须有这样的事情才能将我在此步骤后创建的服务帐户链接到数据库查询。

Another longshot idea I had was to use the environment variable to set the credentials using this JSON file:我的另一个远见是使用环境变量来设置使用此 JSON 文件的凭据:

process.env.GOOGLE_APPLICATION_CREDENTIALS = fs.readFileSync(
  './admin-service-account-file-long-a1b2c3-hash.json',
  'utf8'
)

No joy.没有喜悦。

I don't think the error message is much help since I am certain the way I'm attempting this is fundamentally incorrect, but for what it's worth, the above gets the error我认为错误消息没有多大帮助,因为我确定我尝试这样做的方式从根本上是不正确的,但是就其价值而言,上面的内容却得到了错误

"Failed to connect to 127.0.0.1:1433 - connect ECONNREFUSED 127.0.0.1:1433"

How can I use the Cloud SQL Proxy to connect to a GCP database from Firebase?如何使用 Cloud SQL 代理从 Firebase 连接到 GCP 数据库?

Edit编辑

I am not having luck connecting with either the socketPath property, or directly referencing the IP of the GCP RD instance with root username and password.我没有运气连接socketPath属性,也没有使用 root 用户名和密码直接引用 GCP RD 实例的 IP。 I've seen various places that the cloud proxy is only needed in local development, and also that it is needed in production (that is where I got the idea about socketPath ).我已经看到很多地方只在本地开发中需要云代理,并且在生产中也需要它(这就是我得到有关socketPath的想法的socketPath )。

Further I have tried a test using MySql as was linked in an answer below.此外,我尝试使用 MySql 进行测试,如下面的答案中所链接。 Formerly I had used this as a guide for SQL Server, but since that is in beta still, I thought I would give MySQL a try.以前我曾用它作为 SQL Server 的指南,但由于它仍处于测试阶段,我想我会尝试一下 MySQL。 Still failure, however when using that and using the services IP instead of cloud proxy, I get a timeout error.仍然失败,但是当使用它并使用服务 IP 而不是云代理时,我收到超时错误。

I have also begun initializing the app with service account credentials I created from the GCP dashboard.我还开始使用我从 GCP 仪表板创建的服务帐户凭据初始化应用程序

import { serviceAccount } from './service-account';
const adminConfig = JSON.parse(process.env.FIREBASE_CONFIG);
adminConfig.credential = admin.credential.cert(
  serviceAccount as admin.ServiceAccount
);
admin.initializeApp(adminConfig);

cloud proxy is only needed when trying to connect from outside of google cloud network.仅当尝试从谷歌云网络外部连接时才需要云代理。 From functions, you can directly connect using the host, port, username, and password.从函数中,您可以使用主机、端口、用户名和密码直接连接。

I pass in those details during deployment through functions config.我在部署过程中通过功能配置传递了这些细节。

firebase functions:config:set envs.db_host=$DB_HOST_PROD envs.db_user=$DB_USER_PROD envs.db_password=$DB_PASSWORD_PROD envs.db_name=$DB_NAME_PROD envs.db_use_ssl=false --project hello-world

firebase functions:config:set envs.node_env=production --project hello-world

firebase deploy --token=$FIREBASE_TOKEN --project hello-world --only functions,hosting

Refer to https://stackoverflow.com/a/55974919/515774 on how I use this to set the environment variables.关于我如何使用它来设置环境变量,请参阅https://stackoverflow.com/a/55974919/515774 I then use the environment variables to connect the database然后我使用环境变量连接数据库

In order to connect to a Cloud SQL instance from a Cloud Function (or Firebase function) you can use UNIX domain sockets.为了从 Cloud Function(或 Firebase 函数)连接到 Cloud SQL 实例,您可以使用 UNIX 域套接字。 The documentation only shows how to do this for MySQL and PosgreSQL , not for SQL Server.该文档仅显示了如何为 MySQL 和 PosgreSQL 执行此操作,而不是为 SQL Server执行此操作 It may be because it's not supported yet.可能是因为目前还不支持。 However, I encourage you to give it a try.但是,我鼓励您尝试一下。

Either way, you can also connect your Cloud Functions to a SQL Server Cloud SQL instance using a Serverles VPC Connector and the instance's private IP .无论哪种方式,您还可以使用 Serverles VPC 连接器和实例的私有 IP将您的 Cloud Functions 连接到 SQL Server Cloud SQL 实例。 Quoting the docs:引用文档:

By default, Cloud Functions does not support connecting to the Cloud SQL instance using TCP.默认情况下,Cloud Functions 不支持使用 TCP 连接到 Cloud SQL 实例。 Your code should not try to access the instance using an IP address (such as 127.0.0.1 or 172.17.0.1) unless you have configured Serverless VPC Access.除非您已配置无服务器 VPC 访问,否则您的代码不应尝试使用 IP 地址(例如 127.0.0.1 或 172.17.0.1)访问实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM