Fails to connect to Cloud SQL when using Knex.js (direct driver works fine)

1 533 vues
Accéder directement au premier message non lu

Igor Savin

non lue,
27 sept. 2018, 13:36:4727/09/2018
à Google Cloud SQL discuss
We are trying to query Cloud SQL instances (PostgreSQL and MySQL 2nd generation) from a Cloud Function (Node.js 8).

When we create pool directly using driver (e. g. using "pg" or "mysql2"), for an example like this:


const dbConfig = {
 user
: 'username',
 password
: 'password',
 database
: 'dbName',
 socketPath
= `/cloudsql/instance_name`;
};



mysql
.createPool(dbConfig)

It works just fine. However, if we try instantiating pool using knex, e. g. like this:

const dbConfig = {
 user
: 'username',
 password
: 'password',
 database
: 'dbName',
 socketPath
= `/cloudsql/instance_name`;
};


Knex(client: 'mysql2',
connection
: dbConfig)


we get error during execution:

D functionName kkb7wsgnbixj Function execution started functionName kkb7wsgnbixj
D functionNAme kkb7wsgnbixj
Function execution took 818 ms, finished with status: 'connection error' functionName kkb7wsgnbixj


This seems to be specific to Cloud Functions/Cloud SQL, because locally knex code works just fine and it also works just fine from non-cloud-function projects as well.

Yasser Karout (Cloud Platform Support)

non lue,
27 sept. 2018, 23:02:1127/09/2018
à Google Cloud SQL discuss
Is the function returning this error consistently? As per this documentation regarding connecting to Cloud SQL in Cloud Functions, connections to the database might be dropped by the infrastructure underlying Cloud Functions, so you will need to use a library that supports automatically reconnecting broken connections. Also you need to make sure you are setting maximum concurrent connections to 1 since Cloud Functions limits it to 1.

I did some research regarding re-connection using knex specifically, I suggest looking into this issue thread, which discusses reconnecting after a server disconnect. It might be the reason the connection keeps dropping in your case.

It is recommended to post development related questions on Stackoverflow as they will get more visibility. Groups is better suited for general discussion questions.

Emerson Moraes

non lue,
27 sept. 2018, 23:29:3927/09/2018
à Google Cloud SQL discuss
Hi Igor, i'm sorry to use your issue to make a question to you.

I'm trying to run postgresDemo (from node js samples) function to connect with my CloudSQL Postgres passing all needed env params. You said that if you use "pg" directly it works fine. In my case it's not working. I'm getting the following error:

{"code":"ECONNREFUSED","errno":"ECONNREFUSED","syscall":"connect","address":"127.0.0.1","port":5432}

Do you passed by this problem too?

Note: my App Engine Java Application is getting connection with the same params that I'm using in Cloud Function.

Thank You so much,

Emerson Leite de Moraes

Igor Savin

non lue,
28 sept. 2018, 13:49:5028/09/2018
à Google Cloud SQL discuss
Also you should be using socketPath instead of host/port to connect to CloudSQL from cloud function, see documentation - https://cloud.google.com/functions/docs/sql

Igor Savin

non lue,
28 sept. 2018, 13:49:5028/09/2018
à Google Cloud SQL discuss
Hi Emerson!

We've had exactly same problem: check and double-check that you are using the SA that you think you are using for the connection and that it has the correct permissions.

On Friday, September 28, 2018 at 5:29:39 AM UTC+2, Emerson Moraes wrote:

Emerson Moraes

non lue,
28 sept. 2018, 14:15:3928/09/2018
à Google Cloud SQL discuss
Thank You so much for the answer. The functions is using the default AppEngine SA. And this account has Editor role.

I'm using socketPath instead of ip too. i have the following params (omitting only user and password here):
max: 1, user: 'username', password: 'password', database: 'appengine_helloworld_db', socketPath: '/cloudsql/hello-world-4736435:southamerica-east1:hello-world-postgis-sp'

I tried to do the connection after adding client Cloud SQL role to the SA, but I'm having the same issue. When you made this connection, which were the roles of SA?

Thank You.

David (Google Cloud Support)

non lue,
2 oct. 2018, 11:46:0202/10/2018
à Google Cloud SQL discuss

Hello Emerson,


My colleague already answered you in your thread about this issue you are having connecting to your Cloud SQL instance.

Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message