1
votes

I'm trying to create a basic connection to my Cloud SQL database in Google Apps Script using the code:

function myFunction()
{
    var conn = Jdbc.getCloudSqlConnection("jdbc:google:rdbms://instance_IP/DB_name");
}

I get the error:

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

I know that there is the option of including username and password parameters to the command. Information in this link https://developers.google.com/apps-script/reference/jdbc/jdbc#getCloudSqlConnection(String,String,String).

function myFunction()
{
    var conn = Jdbc.getCloudSqlConnection("jdbc:google:rdbms://instance_IP/DB_name", "userName", "password");
}

When I tried this method with my username and password, I got the same error as above. The username and password are mine for accessing the instance on Google Cloud Platform.

I'm not sure if there is something that I am missing.

1
I'd be suprised if you could open a connection to a database without some kind of authentication information. I suggest you continue troubleshooting with username/password information.Nick.McDermaid
Also looking at that link, there appears to be two ways to provide authentication info. Perhaps you could provide samples of how you are calling it with that info.Nick.McDermaid
I added in the syntax for the script using username and password. I wasn't sure what the other command was "getCloudSqlConnection(url, info)". I don't know what 'info' is nor how to use it if it's an object.FulcrumFlex17

1 Answers

2
votes

I think there are possibly two distinct things going on here.

First and easiest to fix, instead of rdbms, include the actual RDBMS type (presumably mysql).

Second, it appears you're using the Cloud SQL-specific Jdbc.getCloudSqlConnection() with what appears to be an Internet URL. You could conceivably get this working by switching to the generic Jdbc.getConnection(), but I very highly recommend sticking with the getCloudSqlConnection() because you can then also turn off non-SSL connections(!).

When you use Jdbc.getCloudSqlConnection(), instead of the using the instance IP in the URL, use the Cloud SQL Connection name.

To get that, you can go to the Google Cloud Platform project SQL view and under "Connect to this instance", you should see the "Instance connection name".

If you're indeed using mysql, and the "Instance connection name" is "mygcpproject:us-central1:mydbinstance", you'll end up with something like this:

function myFunction()
{
    var conn = Jdbc.getCloudSqlConnection("jdbc:google:mysql://mygcpproject:us-central1:mydbinstance/DB_name", "userName", "password");
}

Best of luck! Wish the docs were a little better for the Google Apps Script JDBC driver, it's pretty darn handy once you get past these quirks.

  • Tim