0

I have connected my node app to my Cloud SQL database and it is working perfectly locally, but when I deploy my node app to App Engine, the API no longer works. In the logs I can see the following:

(node:11) UnhandledPromiseRejectionWarning: SequelizeConnectionError: connect ETIMEDOUT

This does not happen when I run the app locally despite being connected to the same Google Cloud SQL DB.

I thought maybe it would not connect because of security restrictions, but on the SQL connections page it says the following:

Apps in this project: All authorized.

My node app is deployed within the same project, so that shouldn't be the problem.I have also whitelisted my home IP so that I could connect locally, and that was a success as mentioned before. Any help would be appreciated.

EDIT:

Here is my attempt to connect to the DB. It now fails locally & when I deploy. It works locally if I pass in the public IP to the "host."

    const sequelize = new Sequelize('linkspot', 'kyle', 'password', {
        host: '/cloudsql/linkspot:us-central1:linkspot-mysql',
        dialect: 'mysql',
        port: 3306,
        pool: {
         max: 5,
         min: 1,
         acquire: 30000,
         idle: 10000
  }

});

1 Answers1

0

When you connect in your local computer the connection is made via the SQL Proxy, but when the app is deployed in app engine several steps are needed to configure app engine depending if its using private or public IP.

For instance private ip connections requires you to create a Serverless VPC Access connector in the same VPC network as your Cloud SQL instance, and public IP connections require you to use unix domain socket using the format: /cloudsql/INSTANCE_CONNECTION_NAME.

For the instructions on both cases check this doc

Andres S
  • 1,168
  • 7
  • 11
  • Thank you for the feedback. After trying what you suggested, I am still struggling. I checked my logs and see this message: (node:10) UnhandledPromiseRejectionWarning: SequelizeHostNotFoundError: getaddrinfo ENOTFOUND /cloudsql/linkspot:us-central1:linkspot-mysql /cloudsql/linkspot:us-central1:linkspot-mysql:3306 – kyleisdreaming Aug 13 '20 at 22:43
  • I edited my question with my code where I try to connect. – kyleisdreaming Aug 13 '20 at 22:46
  • Have you checked that the service account of your GAE has one of the needed SQL roles? Cloud SQL Client (preferred), Cloud SQL Editor, Cloud SQL Admin I am not familiar with Sequelize, not sure if it is considering that the connection would have to be through a [unix socket](https://stackoverflow.com/questions/29866133/cant-connect-to-mysql-with-sequelize) The GCP NodeJS [SQL example](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/cloud-sql/mysql/mysql/server.js) do things a little different without sequelize. – Andres S Aug 14 '20 at 15:13
  • You could try the private IP method with Sequelize (private IP method doesn't use unix sockets) – Andres S Aug 14 '20 at 15:13