Cloud SQL Proxy no longer working in App Engine environment

774 views
Skip to first unread message

Jordan Cote

unread,
Feb 24, 2017, 9:03:57 PM2/24/17
to Google Cloud SQL discuss

Sometime in the past few weeks an issue with Cloud SQL started to occur with the App Engine's Cloud SQL Proxy. I get the following error in the App Engine logs:

error when connecting to db: { Error: connect ECONNREFUSED 127.0.0.1:3306

The app works on my local machine with Cloud SQL Proxy, as does the counterpart app in Compute Cluster. It used to work in App Engine, too. I double checked the App Engine user's credentials are set to Editor.  I noticed that recently the App Engine parameter vm:true has been deprecated in favor of env: flex. I tried it both ways, with the same result. The following shows how I am connecting in Node.js:

var connection;

function handleDisconnect() {
  connection = mysql.createConnection({
    user: process.env.MYSQL_USER,
    password: process.env.MYSQL_PASSWORD,
    socketPath: process.env.MYSQL_SOCKET_PATH,
    database: process.env.MYSQL_DATABASE
  });                                             // Recreate the connection, since
                                                  // the old one cannot be reused.

  connection.connect(function(err) {              // The server is either down
    if(err) {                                     // or restarting (takes a while sometimes).
      console.log('error when connecting to db:', err);
      setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
    }                                     // to avoid a hot loop, and to allow our node script to
  });                                     // process asynchronous requests in the meantime.
                                          // If you're also serving http, display a 503 error.
  connection.on('error', function(err) {
    console.log('db error', err);
    if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
      handleDisconnect();                         // lost due to either server restart, or a
    } else {                                      // connnection idle timeout (the wait_timeout
      throw err;                                  // server variable configures this)
    }
  });
}

handleDisconnect();

I have the connection string as follows:
  INSTANCE_CONNECTION_NAME: /cloudsql/cybers-cafe:us-east1:cybers-cafe
and under beta_settings:
  cloud_sql_instances: cybers-cafe:us-east1:cybers-cafe

I also tried restarting the SQL server. It is in us-east1 and the instances are in us-east1-b/c.

Adam (Cloud Platform Support)

unread,
Feb 27, 2017, 2:32:25 PM2/27/17
to google-cloud...@googlegroups.com
Your app shouldn't be trying to connect to localhost in production. It should use the instance connection name (/cloudsql/[INSTANCE_CONNECTION_NAME] UNIX socket). I can reproduce your issue though with the tutorial app, because it uses nconf to check if NODE_ENV is set to "production" to conditionally parse INSTANCE_CONNECTION_NAME. This is actually related to a different issue where NODE_ENV is not being respected in app.yaml for deployed apps.

In my case, the workaround was to manually set NODE_ENV to "production" in config.json.

Valter Negreiros

unread,
Sep 20, 2017, 9:00:45 AM9/20/17
to Google Cloud SQL discuss
Same error...

How to fix ?
Reply all
Reply to author
Forward
0 new messages