简体   繁体   中英

jdbc connection error

I would like to fill in a google spreadsheet cell with the output of a mysql database query, so I've written this simple script following the example in the google tutorial:

function get_tbs_number() {
    var conn = Jdbc.getConnection("jdbc:mysql://127.0.0.1:9008/icadata");
    var stmt = conn.createStatement();
    stmt.setMaxRows(100);
    var start = new Date();
    var rs = stmt.executeQuery("select count(*) from CNGSfiltered where run=10269 and (GTO1>4 or GTO2>4 or GTO3>4 or GTO4>4);");
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    var cell = doc.getRange('a1');
    cell.offset(0, 0).setValue(rs.getString(1));
    rs.close();
    stmt.close();
    conn.close();
    var end = new Date();
    Logger.log("time took: " + (end.getTime() - start.getTime()));
}

The problem is that when I try to run the script a get an error connecting to the database, while the connection to the same database from command line (mysql -h localhost -P 9008 -u # -p# icadata) does work.

The problem is Google cannot reach 127.0.0.1 . Try opening up your mysql port (9008) on your router. Then use www.whatismyip.com to get your public IP address.

"jdbc:mysql://<myPublicIP>:9008/icadata"

You will then likely have to run a sql grant command that includes googles IP's or use % (not very secure).

grant all on icadata.* to '<user>'@'<google ips>' identified by '<password>';

Good luck! Here's some reference: https://developers.google.com/apps-script/jdbc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM