15

I'm trying to deploy my Node.js app on Google App Engine and it deployed fine, but it can't connect to Google Cloud SQL for some reason. Here's what it throws:

Error: connect ENOENT /cloudsql/my-project-id:asia-east1:my-sql-instance

Here's how I configured the connection:

if (process.env.INSTANCE_CONNECTION_NAME) {
     exports.mysqlConfig = {
         user: process.env.GCLOUD_SQL_USERNAME,
         password: process.env.GCLOUD_SQL_PASSWORD,
         socketPath: '/cloudsql/' + process.env.INSTANCE_CONNECTION_NAME
     }
} else {
    // Use settings for localhost
}

I'm using node-mysql module to connect to the database.

The App Engine and Cloud SQL are already in the same project. My theory is that the App Engine and the Cloud SQL has to be in the same project and same region, but I'm not sure.

5
  • I have no idea what just happened, but the thing works itself out. I guess it has something to do with Google App Engine's Node.js support being still in beta.
    – starleaf1
    Commented Feb 1, 2017 at 7:01
  • Any chance you removed the leading / from your socketPath in the course of some debugging or something like that?
    – Trott
    Commented Feb 1, 2017 at 7:06
  • 3
    I'm facing the same issue here. It worked and now all of a sudden it doesn't anymore. When I add the instance ip of the app engine to the allowed networks of cloud sql it works. App Engine and Cloud SQL are in the same project.
    – jimmy
    Commented Jul 20, 2017 at 13:06
  • @jimmy, hey, how did you do that? thanks! Commented Aug 27, 2017 at 15:33
  • @LeDinhNhatKhanh maybe this helps: stackoverflow.com/questions/45709319/…
    – jimmy
    Commented Aug 29, 2017 at 16:48

5 Answers 5

20

Check your logs for any errors during startup using:

  1. the following cmd gcloud app logs tail -s default or,

  2. with the log viewer https://console.cloud.google.com/logs/viewer

Chances are that you have not enabled the Cloud SQL API for your project: https://console.developers.google.com/apis/api/sqladmin/overview

3
  • I forgot what I did, but since not enabling Cloud SQL API is the most likely problem, here's 10 points.
    – starleaf1
    Commented Mar 1, 2017 at 1:01
  • 7
    This was the issue I had. I find it hard to believe that nowhere in Google Cloud docs do they mention you have to turn on the Cloud SQL API first, THEN deploy your app with beta_settings: cloud_sql_instances: <INSTANCE_NAME> Commented Mar 30, 2018 at 15:10
  • Google logs in the concole tell, if configuration is correct, that you have to activate SQL Admin API, while documentation doesn't... But what you don't say is that the API must be activated BEFORE deployment. If you see the log, activate the API on the URL given in the log entry, then REDEPLOY your application.
    – Leogiciel
    Commented Jan 2, 2019 at 10:58
11

make sure you have added following setting in app.yaml

beta_settings:
  # The connection name of your instance, available by using
  # 'gcloud beta sql instances describe [INSTANCE_NAME]' or from
  # the Instance details page in the Google Cloud Platform Console.
  cloud_sql_instances: YOUR_INSTANCE_CONNECTION_NAME

ref:https://cloud.google.com/appengine/docs/flexible/nodejs/using-cloud-sql-postgres

7

For anyone using 2nd gen Cloud Functions - they added a portion in the documentation:

If you're using Cloud Functions (2nd gen) and not Cloud Functions (1st gen), the following are required (also see Configure Cloud Run):

They go on to list the steps required. They're a bit scary, but do work eventually.

(If you find yourself looking for the SQL Connection in the new Cloud Run revision, notice there is a separate "Connections" tab for this)

0
4

As Oded Ben Dov noted in his answer, 2nd gen Cloud Functions required additional steps to enable a Cloud SQL connection. I am adding additional information to help anyone else who comes across this in the future. (Oded, thanks for your answer! Your clue saved me a lot of time.)

  1. First you must deploy your 2nd gen Cloud Function. It has to exist before you can configure it.

  2. Then you go to the Cloud Run instance for the new function. Click to Edit and Deploy a New Revision.

  3. Follow the steps in Google's documentation. Note that Google seems to regularly make changes to aspects of the GCP UI without updating documentation that refers to it. In my case (late 2023), the headings in the UI are already slightly different than what the documentation says. But ultimately you are looking for the section in the Cloud Run configuration that allows you to add a new Cloud SQL connection. You have to select your Cloud SQL instance here to allow the Cloud Run instance to access it.

enter image description here

The hard part is finding where to do this. Actually adding the connection is simple and takes only a few seconds.

  1. Important: even after adding the new Cloud SQL connection, you will probably still need to manually deploy your Cloud Function a second time. in my case after saving the Cloud SQL connection, Cloud Run started showing an error that it could not find the image for a new deploy.

After I manually deployed the Cloud Function a second time, the new deploy inherited the new Cloud SQL connection settings and then started working correctly.

1

Apparently the order you do things matters...

  1. enable Cloud SQL API
  2. then (re)deploy your app (gcloud app deploy)

When I did deploy -> create databases -> enable sql ipi I got the ENOENT error

1
  • I confirm the order matters. Logs told I have to activate API, but not app must be redeployed. Kudos
    – Leogiciel
    Commented Jan 2, 2019 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.