Node.JS on Google App Enging with Cloud SQLError:

2019-08-10 12:12发布

I've successfully deployed my app to Google App Engine. My database is up and running on Cloud SQL. I'm able to connect to the database no problems from my local machine by whitelisitng my dev machine's IP on the authorised networks in cloud SQL. However when I deploy to Production, I'm getting:

Error: connect ENOENT /cloudsql/<my project id>:asia-south1:<my database instance name>

My app.yaml file looks like this:

runtime: nodejs  
env: flex

manual_scaling:  
  instances: 1
resources:  
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

env_variables:
  ST_ENV: 'Production' //Used to set the app listening on the below port
  ST_PORT: '8080'
  SQL_USER: '<username>'
  SQL_PASSWORD: '<password>'
  SQL_DATABASE: '<databasename>'
  INSTANCE_CONNECTION_NAME: '/cloudsql/<my project id>:asia-south1:<my database instance name>'

  beta_settings:
  cloud_sql_instances: '<my project id>:asia-south1:<my database instance name>'

My config file looks like this:

database: {             
            connectionLimit: 10,
            // host     : process.env.INSTANCE_CONNECTION_NAME,
            socketPath: process.env.INSTANCE_CONNECTION_NAME,
            user     : process.env.SQL_USER,  
            password : process.env.SQL_PASSWORD,  
            database : process.env.SQL_DATABASE,
            multipleStatements : true

        }

And I'm creating a connection pool (I've simplified the code for this example):

var db = config.production.database;
var pool = mysql.createPool(db);

I've been able to successfully connect from my local machine through the local cloud_sql_proxy.

I've tried using the IP of the Cloud SQL instance as the host, but that gave me the below error:

Error: connect ECONNREFUSED 127.0.0.1:3306'

I also tried specifying the host as 0.0.0.0 and port 3306 and got:

Error: connect ECONNREFUSED 0.0.0.0:3306'

Edit: both the Cloud SQL and the Cloud SQL Admin APIs are enabled:

enter image description here

It's like when pushing to production the app engine is trying to use a non existent proxy. Can anyone help?

1条回答
Animai°情兽
2楼-- · 2019-08-10 12:33

Take a look at the specific instructions for connecting from the Node.js runtime on App Engine Flexible. You can configure the connection using the instance connection name and some other environmental variables. It's not necessary to provide a host name or install the SQL proxy on your instance. Attempting to connect to the public IP of the Cloud SQL instance won't work from App Engine.

查看更多
登录 后发表回答