简体   繁体   English

来自 Google Apps 脚本的 Jdbc 连接错误

[英]Jdbc connection error from Google Apps Script

I have created a Google Cloud Project MySQL database to use in conjunction with the Jdbc service provided by Google Apps Script.我创建了一个 Google Cloud Project MySQL 数据库,与 Google Apps Script 提供的 Jdbc 服务结合使用。 Everything went as planned with the connection.连接一切按计划进行。 I am basically connecting as it does in the docs.我基本上按照文档中的方式进行连接。

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

I shared the file with another account and all of a sudden I am seeing a red error saying:我与另一个帐户共享了该文件,突然间我看到一条红色错误消息:

'Failed to establish a database connection. '建立数据库连接失败。 Check connection string, username and password.'检查连接字符串、用户名和密码。'

Nothing changed in the code, but there is an error.代码没有任何变化,但有一个错误。 When I go back to my original account and run the same bit of code, there is no error.当我 go 回到我的原始帐户并运行相同的代码时,没有错误。 What is happening here?这里发生了什么? Any ideas?有任何想法吗?

Jdbc.getConnection works from both: my account and another account: Jdbc.getConnection适用于:我的帐户和另一个帐户:

var conn = Jdbc.getConnection('jdbc:mysql://' + IP + ':3306/' + database_name, user, password)

I'm really confused because the recommended method did not work.我真的很困惑,因为推荐的方法不起作用。

There are two ways of establishing a connection with a Google Cloud SQL database using Apps Script's JDBC service:使用 Apps Script 的 JDBC 服务建立与 Google Cloud SQL 数据库的连接有两种方法:

  • (Recommended) Connecting using Jdbc.getCloudSqlConnection(url) (推荐)使用Jdbc.getCloudSqlConnection(url)连接
  • Connecting using Jdbc.getConnection(url)使用Jdbc.getConnection(url)连接

Notes:笔记:

  • IP is a Public IP address from the OVERVIEW tab in your database console: IP是来自数据库控制台中 OVERVIEW 选项卡的公共 IP 地址: 在此处输入图片说明
  • I've allowed any host when created a user:我在创建用户时允许任何主机: 在此处输入图片说明

I think this is a permission issue in your second account.我认为这是您的第二个帐户中的权限问题。 Necessary information are missing in your question.您的问题中缺少必要的信息。 But, the secound account, if run as a another user, won't necessarily have your sqlservice authorization.但是,第二个帐户如果以其他用户身份运行,则不一定具有您的 sqlservice 授权。 The permission,许可,

https://www.googleapis.com/auth/sqlservice https://www.googleapis.com/auth/sqlservice

Manage the data in your Google SQL Service instances管理您的Google SQL 服务实例中的数据

is required to use Jdbc.getCloudSqlConnection(url) , while Jdbc#getConnectionUrl() just requires external link connection permission需要使用Jdbc.getCloudSqlConnection(url) ,而Jdbc#getConnectionUrl()只需要外部链接连接权限

https://www.googleapis.com/auth/script.external_request https://www.googleapis.com/auth/script.external_request

I believe that you can only connect to sql instances owned by you with getCloudSqlConnection() which doesn't even require external connection permission.我相信您只能使用getCloudSqlConnection()连接到您拥有的 sql 实例,它甚至不需要外部连接权限。 This method probably calls your sql instance internally.此方法可能会在内部调用您的 sql 实例。

References:参考:


Conclusion结论

To connect to any external service, you need external_request permission.要连接到任何外部服务,您需要 external_request 权限。 But, You don't need that permission to connect to your own documents say, Spreadsheets owned by you/have edit access permission - through SpreadsheetApp.openByUrl.但是,您不需要该权限即可连接到您自己的文档,例如您拥有的电子表格/具有编辑访问权限 - 通过 SpreadsheetApp.openByUrl。 I believe it's the same thing with Jdbc.getCloudSqlConnection().我相信这与 Jdbc.getCloudSqlConnection() 是一样的。 It calls your Google sql internally - So, even if you grant external request permission, It won't work.它在内部调用您的 Google sql - 因此,即使您授予外部请求权限,它也不起作用。 What will work for this method is1.Installable triggers (which runs as you).对这个方法有用的是1.Installable 触发器(它像你一样运行)。 One more thing I'll try is: 2. Add the second account also as owner in GCP-IAM(may not work though).我将尝试的另一件事是: 2. 在 GCP-IAM 中添加第二个帐户作为所有者(虽然可能不起作用)。

I am not sure whether this question has been resolved or not, but let me add this answer.我不确定这个问题是否已经解决,但让我添加这个答案。

I also faced the same problem but I found the resolution.我也遇到了同样的问题,但我找到了解决方案。 What I did is:我所做的是:

First, go to the console.首先,进入控制台。

https://console.cloud.google.com https://console.cloud.google.com

Then, open IAM.然后,打开 IAM。 and add the account as a member and add this permission: "Cloud SQL Client".并将该帐户添加为成员并添加此权限:“Cloud SQL Client”。

I'd double-check once again all IP ranges which should be whitelisted.我会再次仔细检查所有应该列入白名单的 IP 范围。 According to your description it worked fine in first account, probably in second account Apps Script uses another IP for connection, which was not whitelisted or whitelisted with some typo.根据您的描述,它在第一个帐户中运行良好,可能在第二个帐户中,Apps Script 使用另一个 IP 进行连接,该 IP 未列入白名单或白名单中存在一些错字。 Could you share screenshot how did you exactly whitelist the ranges from this article ?您能否分享屏幕截图,您是如何将本文中的范围完全列入白名单的?

I have a GAS Add-On that uses a Google cloud dB.我有一个使用 Google 云 dB 的 GAS 插件。 I initially set this up by:我最初是通过以下方式设置的:

  1. Whitelisting Google Cloud IP ranges in my SQL instance在我的 SQL 实例中将 Google Cloud IP范围列入白名单
  2. Getting the script.external_request scope approved for OAuth Consent screen为 OAuth 同意屏幕获取script.external_request scope 批准

This all works great from GAS for the add-on, but I suspect that if this whitelist is not comprehensive and volatile (which I expect it is), I will see intermittent connectivity issues.这一切对于 GAS 来说都非常适用于附加组件,但我怀疑如果这个白名单不全面且不稳定(我希望如此),我会看到间歇性的连接问题。

I recently added a Firebase web app that needs access to the same dB.我最近添加了一个需要访问相同 dB 的 Firebase web 应用程序。 I had issues, because Firebase does not conform to those Google IP ranges and does not expose its IP for whitelisting.我遇到了问题,因为 Firebase 不符合那些 Google IP 范围,并且没有公开其 IP 用于白名单。 So I had to create a socket layer connection as if Firebase was an external service.所以我必须创建一个套接字层连接,就好像 Firebase 是一个外部服务一样。

Which got me thinking, should I put a socket layer in my GAS Add-On?这让我想到,我应该在我的 GAS 附加组件中放置一个套接字层吗? But nothing in the GAS JBDC Class documentation indicates a socket parameter.但是 GAS JBDC Class 文档中没有任何内容指示套接字参数。

Which leads me to a question that was not really answered in this thread:这让我想到了一个在这个线程中没有真正回答的问题:

Does anyone know why Jdbc.getCloudSqlConnection(url) is the "Recommended" approach?有谁知道为什么Jdbc.getCloudSqlConnection(url)“推荐”方法? The documentation seems to imply that because the IP whitelisting is not required, Jdbc.getCloudSqlConnection(url) is using a socket (or some other secure method) to connect to the dB?文档似乎暗示因为不需要 IP 白名单, Jdbc.getCloudSqlConnection(url)正在使用套接字(或其他一些安全方法)连接到 dB?

It also seems silly that if that is the case, that I would need two have two sensitive scopes to manage a dB connection.如果是这样的话,我还需要两个具有两个敏感范围的设备来管理 dB 连接,这似乎也很愚蠢。 I would rather not go through another OAuth const audit and require my users to accept another scope unless there is a benefit to doing so.我宁愿不通过另一个 OAuth const 审计 go 并要求我的用户接受另一个 scope 除非这样做有好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spark JDBC 连接到 Google Cloud Spanner 失败 - Spark JDBC connection to Google Cloud Spanner failure 从谷歌应用程序脚本连接到 Amazon S3 - Connect to Amazon S3 from google apps script 云 sql 连接中的 Google 身份验证错误 - Error from Google Authentication in cloud sql connection 从 Google Apps 脚本发送到 SendGrid 时为空白 PDF - Blank PDF when sending from Google Apps Script to SendGrid Dialogflow 无法解析来自 Google Apps 脚本的 Webhook 响应 - Dialogflow fails to parse Webhook response from Google Apps Script JDBC 连接到 Google Cloud Spanner 参数 - JDBC connection to Google Cloud Spanner parameters 当应用程序脚本文件或容器文件从云端硬盘中删除时,Google 应用程序脚本 GCP 项目不会从控制台中删除 - Google apps script GCP projects are not deleted from console when apps script files or container files are trashed from Drive 编写 Google Apps 脚本以在 BigQuery“查询执行期间超出资源”时引发错误 - Program Google Apps Script to throw error when BigQuery "Resources exceeded during query execution" 仅在谷歌应用程序脚本中获取请求失败 - Fetch request failing in google apps script ONLY 使用 Google Apps 脚本将多个 .CSV 文件从 Google Drive 导入 BigQuery - Use Google Apps Script to import multiple .CSV files from Google Drive into BigQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM