【问题标题】:Error while connecting to Cloud SQL PostgreSQL instance on GCE VM with Knex.js使用 Knex.js 连接到 GCE VM 上的 Cloud SQL PostgreSQL 实例时出错
【发布时间】:2020-12-14 19:29:07
【问题描述】:

我正在使用 Knex.js 连接到 Cloud SQL PostgreSQL 实例,此时我已经仔细检查了我的所有信息一百万次,但完全不了解为什么会发生此错误。这是我的代码:

const connectWithUnixSockets = (config) => {
    const dbSocketPath = "/cloudsql"
  
    // Establish a connection to the database
    return Knex({
      client: 'pg',
      connection: {
        user: 'postgres', // default user provided by google
        password: Config.DBPass, // password for the account
        database: 'ceclub', // database name
        host: `${dbSocketPath}/Connection-name`,//I have checked this part a thousand times, it's correct.
      },
      // ... Specify additional properties here.
      ...config
    });
  }
let config = {
    pool: {
        max:5,
        min:5,
        acquireTimeoutMillis:60000,
    },
    createTimeoutMillis:30000,
    idleTimeoutMillis:600000,
    createRetryIntervalMillis:200
}
 let knex = connectWithUnixSockets(config);

在它被问到之前,我只是从 gcloud 函数文档中复制了这个。我已确保为我的云 SQL 的 VM 管理员权限授予自己管理员权限和服务帐户,并且我已授权我的 VM 的 IP。但是,每次我运行这个程序时,都会遇到以下问题:

Error: connect ENOENT /cloudsql/connection-name/.s.PGSQL.5432
    at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
  errno: 'ENOENT',
  code: 'ENOENT',
  syscall: 'connect',
  address: '/cloudsql/connection-name/.s.PGSQL.5432'
}

我已经仔细检查了一堆文档,但我对我可能搞砸的事情感到茫然。有人有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    您提到了 Cloud Functions 和 VM,所以我不清楚您使用哪个来尝试连接。

    在 Cloud Functions 中,环境为您提供了一个连接到您的实例的 /cloudsql 目录。

    如果您在本地运行(或在 GCE 实例等不同的环境中),您可以使用 Cloud SQL 代理创建类似的目录。在GitHub repo 中查看您引用的示例的源代码(以及使用代理运行的说明)。

    此外,您可以查看“Authorization with the Cloud SQL Proxy”页面了解如何使用代理的详细说明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-10
      • 2020-05-20
      • 1970-01-01
      • 2019-10-28
      • 2020-09-16
      • 2016-05-16
      • 1970-01-01
      • 2021-11-30
      相关资源
      最近更新 更多