简体   繁体   English

NodeJ:无法从Google App Engine连接Google Cloud SQL

[英]NodeJs:Unable to connect Google Cloud SQL from Google App Engine

I have been working on App Engine and Cloud SQL for sometime now. 我已经从事App Engine和Cloud SQL已有一段时间了。

Recently I have disabled public access on my DB . 最近,我在数据库上禁用了公共访问权限 Since then I am unable to connect to CloudSQL using my App Engine(Both App Engine and Cloud SQL are in same Google Cloud Project). 从那时起,我无法使用我的App Engine连接到CloudSQL(App Engine和Cloud SQL都在同一个Google Cloud Project中)。

The only way I am able to connect is when I whitelist the App Engine IP on CloudSQL. 我能够连接的唯一方法是将CloudSQL上的App Engine IP列入白名单。 But on a new deployment the IP changes, hence adding static IP is not an ideal answer for this. 但是在新的部署中,IP发生了变化,因此添加静态IP并不是解决此问题的理想方法。

In Cloud SQL connections tab it says the following : 在“ Cloud SQL连接”选项卡中,它显示以下内容:

App Engine authorization App Engine授权

All apps in this project are authorized by default. 默认情况下,该项目中的所有应用程序都经过授权。 To authorize apps in other projects, follow the steps below. 要授权其他项目中的应用,请按照以下步骤操作。

Apps in this project: All authorized. 此项目中的应用:所有已授权。 Which is not true in this case. 在这种情况下不正确。

Cloud SQL API and Cloud SQL Admin API are enabled. 启用了Cloud SQL API和Cloud SQL Admin API。

Have googled the issues and followed the steps in document and other stackoverflow questions : 搜索了这些问题,并按照文档和其他stackoverflow问题中的步骤进行操作:

1) Cannot access Google Cloud SQL database from my App Engine 1) 无法从我的App Engine访问Google Cloud SQL数据库
2) Node JS Mysql PROTOCOL ENQUEUE AFTER FATAL ERROR 2) 致命错误后节点JS Mysql协议队列
3) "PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR" in Node-MySqL 3) Node-MySqL中的“ PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR”
4) Error: Cannot enqueue Query after fatal error in mysql node 4) 错误:在mysql节点中出现致命错误后无法排队查询
5) not able to connect google cloud sql from google app engine 5) 无法从Google App Engine连接Google Cloud SQL

All of these haven't helped a lot. 所有这些都没有太大帮助。

connection = mysql.createConnection({
  host     : 'INSTANCE_CONNECTION_NAME',
  user     : 'db_user',
  password : 'db_password',
  database : 'db_name'
});

Expected: Get a Connection. 预期: Get a Connection.
Actual: getaddrinfo ENOTFOUND INSTANCE_CONNECTION_NAME INSTANCE_CONNECTION_NAME:3306 实际: getaddrinfo ENOTFOUND INSTANCE_CONNECTION_NAME INSTANCE_CONNECTION_NAME:3306

Not sure why it's taking INSTANCE_CONNECTION_NAME twice. 不确定为什么要两次使用INSTANCE_CONNECTION_NAME

Let me state if I understand your issue. 让我说一下,如果我了解您的问题。

You have public IP but have made it accessable only for the application in your project to access then. 您拥有公共IP,但仅使项目中的应用程序可以访问它。 If this is the case and given that you are using flexible environment, I found the below method to be helpful 如果是这种情况,并且考虑到您使用的是灵活环境,我发现以下方法会有所帮助

1) When establishing a connection, mention Host as 172.17.0.6 1)建立连接时,将Host称为172.17.0.6

2) And in app.yaml specify INSTANCE_CONNECTION_NAME as mentioned in your Instance Overview page. 2)并在app.yaml中,指定“实例概述”页面中提到的INSTANCE_CONNECTION_NAME

3) Deploy and run. 3)部署并运行。

This will work as Flexible NodeJs environment is built using Docker. 这将与使用Docker构建的Flexible NodeJs环境一起使用。 And the default DB container used is at 172.17.0.6 , the instance connection name is accessed here and a connection is established between App Engine and CloudSQL DB . 所使用的默认数据库容器为172.17.0.6 ,可在此处访问实例连接名称,并在App EngineCloudSQL DB之间建立连接。

I tried to replicate your use-case scenario and the following steps worked for me. 我尝试复制您的用例场景,以下步骤对我有用。 You have your Cloud SQL instance without public IP, then for connecting to it from App Engine you will need to use the same VPN network for both services. 您有没有公共IP的Cloud SQL实例,然后为了从App Engine连接到它,您将需要为两个服务使用相同的VPN网络。 So you will have to edit your Cloud SQL instance to indicate what network you are going to use and edit the app.yaml configuration file. 因此,您将必须编辑Cloud SQL实例以指示要使用的网络并编辑app.yaml配置文件。 Also it's important that so both services will be running in the same region. 同样重要的是,两个服务都必须在同一区域中运行。

In the app.yaml configuration file you have to add the following tags: app.yaml配置文件中,您必须添加以下标签:

network:
    name: [YOUR_NETWORK_NAME]
    subnetwork_name: [YOUR_SUBNETWORK_NAME]

And use the database environment variables: 并使用数据库环境变量:

env_variables:
  SQL_USER: [YOUR_USER]
  SQL_PASSWORD: [YOUR_PASSWORD]
  SQL_DATABASE: [YOUR_DATABASE-NAME]
  # e.g. my-awesome-project:us-central1:my-cloud-sql-instance
  INSTANCE_CONNECTION_NAME:<INSTANCE_CONNECTION_NAME>
# [END gae_flex_mysql_env]

And these following lines at the end: 最后是以下几行:

beta_settings:
  cloud_sql_instances: <INSTANCE_CONNECTION_NAME>

Remember that you have to replace what is inside the “<>” or “[]” with the data you need. 请记住,您必须用所需的数据替换“ <>”或“ []”中的内容。

For further information you can check this documentation Connecting from App Engine . 有关更多信息,您可以查看此文档从App Engine连接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google APP Engine 和云 sql :: 无法在 Google 云 sql(我的 sql)中连接 Spring boot 应用程序 - Google APP Engine and cloud sql :: Unable to connect Spring boot app in Google cloud sql (my sql) 使用Google App Engine端点连接到Cloud SQL - Connect to Cloud SQL with Google App Engine Endpoint 无法使用Google App Engine连接到Google Cloud MySQL - Unable to Connect to Google Cloud MySQL using Google App Engine 无法使用Google App Engine上的Slim框架连接到Google Cloud SQL - Not able to connect to Google Cloud SQL with Slim framework on Google App Engine 使用mysqli_connect连接到Google App Engine中的Cloud SQL - Connect to Cloud SQL in Google App Engine using mysqli_connect 无法使用Google App Engine中的SSL + Golang连接到Google Cloud SQL - Cannot connect to Google Cloud SQL using SSL + Golang from Google App Engine Google App Engine 上的 Django 连接到云 SQL,无法连接 - Django on Google App Engine connection to Cloud SQL, cannot connect Google App Engine-使用mysqli用php连接到SQL Cloud实例 - Google App Engine - Connect to SQL Cloud Instance with php using mysqli Google App Engine:如何使用Zend DB连接到Cloud SQL - Google App Engine: How to connect to Cloud SQL with Zend DB 无法连接到Google Cloud SQL - Unable to connect to Google cloud SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM