Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jdbc connection error from Google Apps Script

I have created a Google Cloud Project MySQL database to use in conjunction with the Jdbc service provided by Google Apps Script. Everything went as planned with the connection. I am basically connecting as it does in the docs.

var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);

I shared the file with another account and all of a sudden I am seeing a red error saying:

'Failed to establish a database connection. Check connection string, username and password.'

Nothing changed in the code, but there is an error. When I go back to my original account and run the same bit of code, there is no error. What is happening here? Any ideas?

like image 799
Jordan Rhea Avatar asked Jul 06 '18 00:07

Jordan Rhea


4 Answers

I think this is a permission issue in your second account. Necessary information are missing in your question. But, the secound account, if run as a another user, won't necessarily have your sqlservice authorization. The permission,

https://www.googleapis.com/auth/sqlservice

Manage the data in your Google SQL Service instances

is required to use Jdbc.getCloudSqlConnection(url), while Jdbc#getConnectionUrl() just requires external link connection permission

https://www.googleapis.com/auth/script.external_request

I believe that you can only connect to sql instances owned by you with getCloudSqlConnection() which doesn't even require external connection permission. This method probably calls your sql instance internally.

References:

  • Jdbc#getCloudConnection

  • Jdbc#getConnection


Conclusion

To connect to any external service, you need external_request permission. But, You don't need that permission to connect to your own documents say, Spreadsheets owned by you/have edit access permission - through SpreadsheetApp.openByUrl(). I believe it's the same thing with Jdbc.getCloudSqlConnection(). It calls your Google sql internally - So, even if you grant external request permission, It won't work. What will work for this method is

  1. Installable triggers (which runs as you).

  2. Add the second account also as owner in GCP-IAM (may not work though) See this answer

like image 54
TheMaster Avatar answered Oct 11 '22 00:10

TheMaster


I am not sure whether this question has been resolved or not, but let me add this answer.

I also faced the same problem but I found the resolution. What I did is:

First, go to the console.

https://console.cloud.google.com

Then, open IAM. and add the account as a member and add this permission: "Cloud SQL Client".

like image 37
Herbert Avatar answered Oct 10 '22 22:10

Herbert


Jdbc.getConnection works from both: my account and another account:

var conn = Jdbc.getConnection('jdbc:mysql://' + IP + ':3306/' + database_name, user, password)

I'm really confused because the recommended method did not work.

There are two ways of establishing a connection with a Google Cloud SQL database using Apps Script's JDBC service:

  • (Recommended) Connecting using Jdbc.getCloudSqlConnection(url)
  • Connecting using Jdbc.getConnection(url)

Notes:

  • IP is a Public IP address from the OVERVIEW tab in your database console: enter image description here
  • I've allowed any host when created a user: enter image description here
like image 29
Max Makhrov Avatar answered Oct 11 '22 00:10

Max Makhrov


I'd double-check once again all IP ranges which should be whitelisted. According to your description it worked fine in first account, probably in second account Apps Script uses another IP for connection, which was not whitelisted or whitelisted with some typo. Could you share screenshot how did you exactly whitelist the ranges from this article?

like image 35
edward Avatar answered Oct 10 '22 22:10

edward