简体   繁体   中英

ENOENT when connecting to Google Cloud SQL from App Engine

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.

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

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM