How to connect to database with SSL in google apps script?

I can confirm that I can connect to a MySQL database with SSL within Google Apps Script.

It’s important to note useSSL=true is indeed necessary

I was able to get it working by following the example at https://issuetracker.google.com/issues/36761592#comment18 (relevant snippet repeated below):

var conn = Jdbc.getConnection('jdbc:mysql://<ip address>/<db name>?useSSL=true', { 
  user: '<user>', 
  password: '<pass>', 
  _serverSslCertificate: '-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----', 
  _clientSslCertificate: '-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----', 
  _clientSslKey: '-----BEGIN RSA PRIVATE KEY-----\n...\n-----END RSA PRIVATE KEY-----' 
});

If you’re using a Google Cloud SQL instance, you may want to consider using getCloudSqlConnection(). Presumably it’s encrypted as well (based on the fact that I can indeed get a connection when the corresponding instance has “Only secured connections are allowed to connect to this instance.” enabled), and, importantly, doesn’t require you to manage (i.e. not lose control of) the key and certs yourself.

Leave a Comment