Mahi29
Mahi29

Reputation: 94

NodeJs:Unable to connect Google Cloud SQL from Google App Engine

I have been working on App Engine and Cloud SQL for sometime now.

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).

The only way I am able to connect is when I whitelist the App Engine IP on CloudSQL. But on a new deployment the IP changes, hence adding static IP is not an ideal answer for this.

In Cloud SQL connections tab it says the following :

App Engine authorization

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.

Have googled the issues and followed the steps in document and other stackoverflow questions :

1) Cannot access Google Cloud SQL database from my App Engine
2) Node JS Mysql PROTOCOL ENQUEUE AFTER FATAL ERROR
3) "PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR" in Node-MySqL
4) Error: Cannot enqueue Query after fatal error in mysql node
5) not able to connect google cloud sql from google app engine

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.
Actual: getaddrinfo ENOTFOUND INSTANCE_CONNECTION_NAME INSTANCE_CONNECTION_NAME:3306

Not sure why it's taking INSTANCE_CONNECTION_NAME twice.

Upvotes: 2

Views: 1254

Answers (2)

Anshul Tyagi
Anshul Tyagi

Reputation: 2196

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. 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

2) And in app.yaml specify INSTANCE_CONNECTION_NAME as mentioned in your Instance Overview page.

3) Deploy and run.

This will work as Flexible NodeJs environment is built using Docker. 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.

Upvotes: 3

iker lasaga
iker lasaga

Reputation: 394

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. 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. 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:

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.

Upvotes: 1

Related Questions