本文介绍了从Firebase功能使用Cloud SQL代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在本地运行Google的 Cloud SQL代理,并且正在运行使用类似以下命令的本地服务Firebase功能:

I'm running Google's Cloud SQL Proxy locally and it's working with locally served Firebase functions using a command like:

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

但是我真的不知道如何在已部署的Firebase功能上进行这项工作.

However I don't really know how to have this work on deployed Firebase functions.

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'
    }
  })
};

我确实在黑暗中进行拍摄,只要将凭证文件作为extras对象的一部分传递到TypeORM的connections对象,但是我觉得必须有这样的东西才能链接我在到数据库查询.

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.

我的另一个长远想法是使用环境变量使用此JSON文件设置凭据:

Another longshot idea I had was to use the environment variable to set the credentials using this JSON file:

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

不高兴.

我认为该错误消息没有多大帮助,因为我确定尝试这种操作的方式从根本上来说是错误的,但是就其价值而言,以上内容均会出错

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"

如何使用Cloud SQL代理从Firebase连接到GCP数据库?

编辑

我没有连接socketPath属性的运气,也没有用根用户名和密码直接引用GCP RD实例的IP.我已经看到过很多地方,只有本地开发才需要云代理,生产中也需要云代理(这就是我对socketPath的想法).

How can I use the Cloud SQL Proxy to connect to a GCP database from Firebase?

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

我还尝试使用MySql进行测试,如下面的答案中所述.以前,我曾将其用作SQL Server的指南,但由于该版本仍处于测试阶段,因此我想尝试一下MySQL.仍然失败,但是当使用它并使用服务IP而不是云代理时,出现超时错误.

Further I have tried a test using MySql as was linked in an answer below. 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. Still failure, however when using that and using the services IP instead of cloud proxy, I get a timeout error.

我也已经开始使用我从中创建的服务帐户凭据初始化应用. GCP仪表板.

I have also begun initializing the app with service account credentials I created from the GCP dashboard.

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

推荐答案

仅当尝试从Google云网络外部进行连接时才需要云代理.通过功能,您可以使用主机,端口,用户名和密码直接连接.

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.

我在部署过程中通过功能config传递了这些详细信息.

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

请参阅 https://stackoverflow.com/a/55974919/515774 关于如何使用它来设置环境变量.然后,我使用环境变量连接数据库

Refer to https://stackoverflow.com/a/55974919/515774 on how I use this to set the environment variables. I then use the environment variables to connect the database

这篇关于从Firebase功能使用Cloud SQL代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 15:21
查看更多