Tuesday, April 24, 2018

[google-cloud-sql-discuss] Unable to connect to mysql/postgres from App Engine flexible using nodejs/knex

Using simplest knex example, I'm unable to connect to postgres (nor mysql) instance from App Engine Flexible.

app.yaml
env: flex
runtime
: nodejs
skip_files
:
- node_modules/
- test/

env_variables
:
  SQL_USER
: "root"
  SQL_PASSWORD
: "asd"
  SQL_DATABASE
: "dbname"
  INSTANCE_CONNECTION_NAME
: "some-project:europe-west3:some-instance-name"


beta_settings
:
 cloud_sql_instances
: "some-project:europe-west3:some-instance-name"

server.js
console.log('Initialising knex ...');

const knex = require('knex')({
    client: 'pg',
    connection: {
        database: 'dbname',
        user: 'admin',
        password: 'asd',
        host: `/cloudsql/some-project:europe-west3:some-instance-name`,
    },
    pool: {
        afterCreate: function (conn, done) {
            // in this example we use pg driver's connection API
            conn.query('select 1+1 as result;', (err) => {
                if (err) {
                    console.log('KNEX errored out:', err);
                    return done(err, conn);
                }
                console.log('KNEX connected');
            });
        }
    }
});

knex.raw('select 1+1 as result')
.then(() => {
    // there is a valid connection in the pool
    console.log('KNEX works!');
})
.catch((err) => console.log('KNEX error', err));


package.json
{
  "name": "sample-project",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "engines": {
    "node": "9.x"
  },
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "knex": "^0.14.6",
    "mysql": "^2.15.0",
    "mysql2": "^1.5.3",
    "pg": "^7.4.1"
  }
}

Deployment output:
Updating service [default] (this may take several minutes)...failed.
ERROR: (gcloud.app.deploy) Error Response: [9]
Application startup error:

> sample-project@1.0.0 start /app
> node server.js

Initialising knex ...
KNEX error { Error: connect ENOENT /cloudsql/some-project:europe-west3:some-instance-name/.s.PGSQL.5432
    at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1173:14)
  errno: 'ENOENT',
  code: 'ENOENT',
  syscall: 'connect',
  address: '/cloudsql/some-project:europe-west3:some-instance-name/.s.PGSQL.5432' }



I've tried creating different App engine projects. I've tried with mysql, and I've tried using sequelize ORM instead of Knex. Always the same error: "ENOENT ..."
SQL instance is in the same project.
Btw, I did manage to connect to CloudSQL postgres instance from local machine, using local proxy (as per docs), but never managed to connect from App Engine.

Any suggestions?

Thank you.

--
You received this message because you are subscribed to the Google Groups "Google Cloud SQL discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-cloud-sql-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-cloud-sql-discuss/33be1899-fb25-4539-b055-40d07982366c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment