I'm new to using Google Scripts and am trying to make a connection to a MySQL (5.5.65-MariaDB) database which is hosted on an external (non-Google) server running CentOS Linux 7.7.1908 Core.
I'm following the guide here: https://developers.google.com/apps-script/guides/jdbc#other_databases
In my script I have used the following function:
// export_sheet_data.gs
function writeOneRecord() {
var conn = Jdbc.getConnection('jdbc:mysql://HOST_NAME.com:3306/DB_NAME', 'DB_USER', 'DB_PASSWORD');
var stmt = conn.prepareStatement('INSERT INTO entries '
+ '(guestName, content) values (?, ?)');
stmt.setString('andy', 'First Guest');
stmt.execute();
}
Obviously the HOST_NAME
, DB_NAME
, DB_USER
and DB_PASSWORD
are strings (entered literally, not variables) which correspond to the appropriate values: host name of my external server, database name, database username and database password.
When I execute this in the Scripts console (Run > Run function > writeOneRecord()
) it says
Running function writeOneRecord()
for approx 10 seconds.
It then errors with
Exception: Failed to establish a database connection. Check connection string, username and password.
If I click "Details" it doesn't really elaborate on this. It says
Exception: Failed to establish a database connection. Check connection string, username and password. (line 54, file "export_sheet_data")
I have done the following:
- Added the IP addresses on the linked webpage to my servers firewall. Reloaded the firewall to make sure it's using this configuration.
- Checked that the database,
DB_NAME
, exists on my server. - Checked that the hostname,
HOST_NAME
matches my server. Also tried it with the external IP address. - Checked MySQL is using port 3306
- Checked the username/password (
DB_USER
andDB_PASSWORD
) are correct.
There is nothing appearing in my firewall log on the server I'm connecting to about this.
I've checked that I'm using Jdbc.getConnection
correctly.
How else can I debug this? There doesn't seem to be anything in the Google Scripts console which can help.
getConnection
: user, pwd and dbname as a object. - TheMaster