You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Google表格连接到MariaDB?Google Apps Script连接失败求助

Troubleshooting Google Apps Script JDBC Connection to MariaDB

Since you can connect to MariaDB via Navicat but hit a wall with Google Apps Script, the issue almost always boils down to network restrictions, JDBC URL configuration, or environment-specific authentication rules. Let’s walk through the most likely fixes step by step:

1. Fix the JDBC URL Format

MariaDB is compatible with the MySQL JDBC driver, but explicitly using the MariaDB protocol can resolve subtle compatibility issues. Also, don’t forget to include the port (default is 3306) if your server uses a non-standard one:

// Replace your current URL line with this
var dbUrl = 'jdbc:mariadb://' + address + ':3306/' + db;

If your MariaDB uses a custom port (like 3307), swap 3306 with that number.

2. Whitelist Google’s JDBC IP Addresses

Navicat connects from your local IP, but Google Apps Script runs on Google’s servers. Your database server’s firewall or access control list (ACL) is probably blocking these external IPs.

  • Log into your hosting provider’s control panel and add Google’s published JDBC IP ranges to the allowed hosts list.
  • Also, confirm your MariaDB user has permission to connect from external hosts (use username@'%' instead of username@localhost when setting up user privileges).

3. Encode Special Characters in Credentials

If your username or password has special characters (like @, #, $, or &), they’ll break the JDBC URL unless URL-encoded. For example:

var encodedPwd = encodeURIComponent(userPwd);
var dbUrl = 'jdbc:mariadb://' + user + ':' + encodedPwd + '@' + address + '/' + db;

This ensures characters like pass@123 become pass%40123, which the JDBC driver can parse correctly.

4. Adjust SSL Settings

MariaDB servers often require SSL for external connections, and Navicat handles this automatically—but your script might not. Try adding SSL parameters to your URL:

var dbUrl = 'jdbc:mariadb://' + address + '/' + db + '?useSSL=true&requireSSL=true';

If your server doesn’t enforce SSL (not recommended for production), you can disable it with ?useSSL=false to test.

5. Double-Check Your Code Syntax

Make sure your Jdbc.getConnection call uses the correct parameter order:

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

Typos in variable names (like userPwd vs password) or missing arguments are easy to miss!

6. Add Error Handling for More Clues

If you’re still stuck, wrap your code in a try-catch block to get detailed error logs:

function getConnection() {
  try {
    var address = 'url.domain';
    var user = 'username';
    var userPwd = 'pass';
    var db = 'database_name';
    var dbUrl = 'jdbc:mariadb://' + address + ':3306/' + db + '?useSSL=true';
    var conn = Jdbc.getConnection(dbUrl, user, userPwd);
    Logger.log("Connection successful!");
    return conn;
  } catch (e) {
    Logger.log("Full error details: " + e.toString());
    throw e;
  }
}

Check the Apps Script log (View > Logs) for the exact error message—it’ll point you directly to the problem.

内容的提问来源于stack exchange,提问作者Constantin Cornita

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:12:30