如何从 Cloud Functions 连接 Google Cloud SQL? [英] How to connect Google Cloud SQL from Cloud Functions?

查看:27
本文介绍了如何从 Cloud Functions 连接 Google Cloud SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Cloud Functions for Firebase 来构建一个与Google Cloud SQL (PostgreSQL) 实例.

I am trying to use Cloud Functions for Firebase to build an API that talks with a Google Cloud SQL (PostgreSQL) instance.

我正在使用 HTTP(S) 触发器.

I am using HTTP(S) trigger.

当我将桌面的 IP 地址列入白名单时,我可以从本地计算机使用函数的 node.js 代码连接到 Cloud SQL.但是当我部署时,我无法连接,而且我无法弄清楚 Firebase Function 的服务器的 HOST IP 地址,以加入白名单.

When I white-list my desktop's IP address, I can connect to the Cloud SQL with the function's node.js code from my local machine. But when I deploy, I can't connect, and I can't figure out the HOST IP address of Firebase Function's server, to white-list.

您如何通过 Cloud Functions for Firebase 与 Google Cloud SQL 对话?

How do you talk to Google Cloud SQL from Cloud Functions for Firebase?

谢谢!

// Code Sample, of what's working on Localhost.
var functions = require('firebase-functions');

var pg = require('pg');
var pgConfig = {
  user: functions.config().pg.user,
  database: functions.config().pg.database,
  password: functions.config().pg.password,
  host: functions.config().pg.host
}

exports.helloSql = functions.https.onRequest((request, response) => {
  console.log('connecting...');
  try {
    client.connect(function(err) {
      if (err) throw err;

      console.log('connection success');
      console.log('querying...');

      client.query('SELECT * FROM guestbook;', function(err, result){
        if (err) throw err;

        console.log('querying success.');
        console.log('Results: ', result);
        console.log('Ending...');

        client.end(function(err){
          if (err) throw err;
          console.log('End success.');
          response.send(result);
        });
      });

    });
  } catch(er) {
    console.error(er.stack)
    response.status(500).send(er);
  }
});

推荐答案

新答案:

查看其他答案,现已正式支持.https://cloud.google.com/functions/docs/sql

See other answers, it's now officially supported. https://cloud.google.com/functions/docs/sql

旧答案:

目前不可能.然而,这是对问题跟踪器的功能请求 #36388165:

It's not currently possible. It is however a feature request on the issue tracker #36388165:

目前无法从 Cloud Functions 连接到 Cloud SQL支持,因为 UNIX 套接字不存在(导致 ENOENT)和没有定义的 IP 范围要列入白名单(导致 ETIMEDOUT).一可能是将 Cloud SQL 实例中的 0.0.0.0/0 列入白名单,但出于安全原因,不建议这样做.

Connecting to Cloud SQL from Cloud Functions is currently not supported, as the UNIX socket does not exist (causing ENOENT) and there is no defined IP range to whitelist (causing ETIMEDOUT). One possibility is to whitelist 0.0.0.0/0 from the Cloud SQL instance but this is not recommended for security reasons.

如果这对您来说是一个重要的功能,我建议您访问 issuetracker 并为功能请求加注星标,以帮助它获得人气.

If this is an important feature for you I would suggest you visit the issuetracker and star the feature request to help it gain popularity.

这篇关于如何从 Cloud Functions 连接 Google Cloud SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆