0

I'm not having any success connecting to Cloud SQL from my Node-JS app hosted on App-Engine, when I deploy via gcloud app deploy

After deploying, it throws error Error: connect ENOENT /cloudsql/<INSTANCE_CONNECTION_NAME> at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)

  var config = {
      user: 'my_username',
      database: 'test_db',
      password: 'example_pass'
  };

  if (process.env.DB_INSTANCE_NAME && process.env.NODE_ENV === 'production') {
      // Executing remotely
      config.socketPath = "/cloudsql/<INSTANCE_CONNECTION_NAME>"
  } else {
      // Local development - add IP address to SQL access
      config.host = 'XX.XX.XXX.XX';
  }

  let pool;
  const createPool = async () => {
    pool = await mysql.createPool(config);
    pool.query('SELECT * FROM test_db.test_table', function (error, results, fields) {
      if (error) throw error;
      console.log('The result is: ', results);
    });
  };
  createPool();

My app.yaml looks like this :

# [START app_yaml]
runtime: nodejs
beta_settings:
  cloud_sql_instances: <INSTANCE_CONNECTION_NAME>
env: flex
# [END app_yaml]

The App Engine and the Cloud SQL db are both within the same project. I've been through the steps listed here many times : https://cloud.google.com/sql/docs/mysql/connect-app-engine

I can connect locally by whitelisting my ip address in the SQL Connections tab no problems.

2
  • are you setting right the connection name? Have you verified that the Service Account has properly set the Role: Cloud SQL Client (which is the preferred one)? Commented Feb 10, 2020 at 22:29
  • 1
    Just in addition, the ENOENT error is usuarlly an issue where there are no such directory entry found on your configuration. Could you please check the socketpath configuration on your app.js file? Try to change the socketPath as shown below: socketPath = /cloudsql/CloudSQLInstanceName:Location:root Commented Feb 10, 2020 at 22:36

1 Answer 1

0

After deleting everything and starting again, I had some success today. I think the problem was trying to connect to a first generation Cloud SQL instance. After creating a new test database and configuring access locally with the Cloud Sql Proxy, I'm managing to get this working locally and on App Engine build. Thanks for your help.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.