【问题标题】:cant get my cloud function to connect to sql instance?无法让我的云函数连接到 sql 实例?
【发布时间】:2022-11-18 02:37:07
【问题描述】:

我是谷歌云平台的新手,我一直在尝试了解谷歌功能和其他与之配合使用的东西。我读到你可以让你的函数连接到你的云 sql 实例,所以我尝试按照官方文档做一个测试函数来做它,但是当我尝试部署它时它总是给我一个错误。

我使用 node.js 作为函数(但如果你们有任何其他语言的工作示例,我也希望看到它们)和 postgresql 作为我的 cloudsql。

下面是函数的代码。

const Knex = require('knex');

// createUnixSocketPool initializes a Unix socket connection pool for
// a Cloud SQL instance of Postgres.
const createUnixSocketPool = async config => {
  // Note: Saving credentials in environment variables is convenient, but not
  // secure - consider a more secure solution such as
  // Cloud Secret Manager (https://cloud.google.com/secret-manager) to help
  // keep secrets safe.
  return Knex({
    client: 'pg',
    connection: {
      user: 'here i put instance user i made', // e.g. 'my-user'
      password: 'the user password', // e.g. 'my-user-password'
      database: 'the db i made', // e.g. 'my-database'
      host: '/cloudsql/projectname:southamerica-east1:instancename', // e.g. '/cloudsql/project:region:instance'
    },
    // ... Specify additional properties here.
    ...config,
  });
};

从文档中复制并粘贴了他们说要做的一些小改动但它不起作用......我真的不能很好地理解unix套接字是关于什么的,我应该在设置函数之前创建其他东西吗?知道可能出了什么问题吗?这是我收到的错误消息:

加载用户代码时函数失败。这可能是由于用户代码中的错误。错误消息:错误:请检查您的函数日志以查看错误原因:https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs。可以在 https://cloud.google.com/functions/docs/troubleshooting#logging 找到其他故障排除文档。请访问https://cloud.google.com/functions/docs/troubleshooting获取深入的故障排除文档。

【问题讨论】:

    标签: node.js google-cloud-platform google-cloud-functions google-cloud-sql


    【解决方案1】:

    设置 Cloud SQL 实例

    你可以查看这个文档到Configure Cloud Functions

    参考: https://cloud.google.com/sql/docs/postgres/connect-instance-cloud-functions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-06
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 2016-09-29
      • 1970-01-01
      • 2020-04-02
      相关资源
      最近更新 更多