本文介绍了云端SQL实例连接在本地工作,但不在App Engine上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Google Cloud App Engine上托管一个作为Node.js应用程序实现的API。 API使用的数据库是一个PostgreSQL 9.6数据库,它由云SQL实例托管。数据库通过 Knex 连接到Node.js API。

在App Engine上托管时,不需要任何与数据库联系的API端点就能正常工作。但是,当需要联系数据库以完成API调用时,以下错误会出现在日志中:

但是,如果我使用 Knex 在本地连接到Cloud SQL实例,并通过localhost:3000寻址API,这是一个API调用,要求数据库完美无缺。这验证了Cloud SQL上的PostgreSQL数据库实例工作正常,并且在App Engine上托管的Node.js应用程序正在与Cloud SQL实例建立连接失败。



  module.exports = require('knex')({
客户端:'pg',
debug:true,
连接:{
主机:'35 .194.32.254',
用户:'postgres',
密码: 'mypassword',
database:'mydatabase'
},
});

同样,本地连接成功,App Engine连接失败。

我的 app.yaml 文件如下:

  runtime:nodejs 
env:flex
beta_settings:
cloud_sql_instances:my-project-12345:us-central1:mydatabase

为了确保这不是权限问题,我授予App Engine服务帐户 my-project-12345@appspot.gserviceaccount.com 云SQL客户端权限。我还验证了Cloud SQL实例与App Engine上的Node.js应用程序部署在相同的区域。



我的问题是这个:为什么App Engine上的Node.js连接到Cloud SQL实例,如果它可以连接到它根据Vadmin的评论,UNIX套接字必须用于从App Engine连接到Cloud SQL,否则IP白名单必须是涉及到一些有关如何执行的指导。



为了实现这个目标,我们只需更改主机 35.194.32.254 到UNIX套接字 / cloudsql / my-project-12345:us-central1:mydatabase

  module.exports = require('knex')({
client:'pg',
debug :true,
连接:{
主机:'/ cloudsql / esp-mobile-182605:us-central1:esp-db',//本地测试为127.0.0.1,本地使用为35.194.32.254托管数据库云SQL
用户:'postgres',
密码:'seniordesign',
数据库:'esp _db'
},
});


I am trying to host an API implemented as a Node.js app on Google Cloud App Engine. The database that the API utilizes is a PostgreSQL 9.6 database which is hosted with a Cloud SQL instance. The database is connected to the Node.js API through Knex.

When hosted on App Engine, the API endpoints that do not require any contact with the database work fine. However, when the database needs to be contacted to complete the API call, the following error appears in the logs:

However, if I use Knex to connect to the Cloud SQL instance locally and address the API through localhost:3000, an API call requiring the database completes perfectly fine. This verifies that the PostgreSQL database instance on Cloud SQL is working fine and that something about hte Node.js app being hosted on App Engine is making a connection to the Cloud SQL instance fail.

I connect to the database like so:

module.exports = require('knex')({
        client: 'pg',
        debug:  true,
        connection: {
          host : '35.194.32.254',
          user : 'postgres',
          password : 'mypassword',
          database : 'mydatabase'
        },
      });

Again, local connections succeed, App Engine connections fail.

My app.yaml file is as follows:

runtime: nodejs
env: flex
beta_settings:
  cloud_sql_instances: my-project-12345:us-central1:mydatabase

To ensure this is not a permission issue, I granted the App Engine service account my-project-12345@appspot.gserviceaccount.com the Cloud SQL Client permission. I've also verified that the Cloud SQL instance is deployed in the same region as the Node.js app on App Engine.

My question is this: Why can't Node.js on the App Engine connect to the Cloud SQL instance if it can connect to it locally?

解决方案

As per Vadmin's comment, UNIX sockets must be used to connect from App Engine to Cloud SQL otherwise IP whitelisting must be involved, something which there is little guides on how to do.

To accomplish this, we simply change the host from 35.194.32.254 to the UNIX socket /cloudsql/my-project-12345:us-central1:mydatabase:

module.exports = require('knex')({
    client: 'pg',
    debug:  true,
    connection: {
        host : '/cloudsql/esp-mobile-182605:us-central1:esp-db', // 127.0.0.1 for local testing, 35.194.32.254 to locally use hosted db on Cloud SQL
        user: 'postgres',
        password: 'seniordesign',
        database: 'esp_db'
    },
});

这篇关于云端SQL实例连接在本地工作,但不在App Engine上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 07:30
查看更多