从GCE实例上的Tomcat webapp连接到第二代MySQL

时间:2018-03-15 00:21:08

标签: java mysql tomcat google-cloud-sql

我正在开发一个webapp的“试用”安装,它运行在一个GCE实例中的Tomcat中,与第二代MySQL服务器通信。当我接近使其运作时,我现在遇到了一些我无法理解的问题。

假设:

应用程序是Java,一个在Tomcat服务器上运行的webapp上下文,用于GCE实例的负载平衡集群。

所有这些实例共享几个公共存储桶,并为其数据库共享一个常见的第二代MySQL实例。

现在,负责webapp的开发人员在他自己的私有Google Cloud项目中运行了一个实验性集群,他刚刚向全世界开放了SQL服务器,并且没有使用任何SSL。但显然,当我们接近真实的东西时,它不会飞。

我正试图接近真实的东西。但是,当我不知道GCE实例将具有哪些IP地址时,我应该如何建立数据库连接?

查看我的MySQL实例的“授权”选项卡,我看到:

  

App Engine授权
  默认授权此项目中的所有应用程序。要在其他项目中授权应用,
  请按照以下步骤操作   此项目中的应用程序:全部授权。

但那是“App Engine”。 Google Compute Engine。

我看到了一个名为“JDBC Socket Factory”的内容,但它位于一个关于从运行在Google Cloud外部的应用程序进行连接的页面上。

我看到一个关于在Google Compute实例上连接MySQL客户端的页面(这引出了一个问题,“当我可以通过桌面连接时,为什么我要 有点用户友好吗?“)。 连接在动态创建的实例上运行的Tomcat webapp上下文。

我在这里缺少什么?

我现在一直在尝试使用代理,如果代理在我的帐户下运行,我发现可以连接,但如果它在两个服务帐户中运行,也应该能够连接,则无法连接。

在所有三种情况下,代理都会启动,并且正在等待连接。如果我像我一样运行代理,我可以很好地连接,无论是来自默认的MySQL客户端,还是来自Sequel Pro,还是来自Squirrel。但是,如果我从默认服务帐户或我专门为此创建的服务帐户运行代理,则客户端会获得:

  

MySQL说:在'读取初始化时失去与MySQL服务器的连接   通信包',系统错误:0

并且代理显示:

2018/03/22 09:38:23 New connection for "<REDACTED>"
2018/03/22 09:38:24 couldn't connect to "<REDACTED>": ensure that the
account has access to "<REDACTED>" (and make sure there's no typo in 
that name). Error during createEphemeral for <REDACTED>: googleapi: 
Error 403: Access Not Configured. Cloud SQL Administration API has
not been used in project 773874261491 before or it is disabled.
Enable it by visiting
https://console.developers.google.com/apis/api/sqladmin.googleapis.com
/o erview?project=<REDACTED> then retry. If you enabled this API
recently, wait a few minutes for the action to propagate to our
systems and retry., accessNotConfigured

我有:

  • Cloud SQL Admin
  • Cloud SQL Client
  • Compute Instance Admin(v1)
  • 计算网络管理员
  • 计算安全管理员
  • Deployment Manager Editor
  • 服务帐户主管
  • 服务帐户管理员
  • 服务帐户密钥管理
  • Project IAM Admin
  • Storage Admin

默认服务帐户包含:

  • Cloud SQL客户端编辑器

我创建的服务帐户包含:

  • Cloud SQL Client

建议尝试重新启用Cloud SQL API,我已将其禁用并重新启用。没变。仍然:

Jamess-Mac-mini:~ jamesl$ ./cloud_sql_proxy -instances=<REDACTED>=tcp:3306
2018/03/22 09:37:42 Listening on 127.0.0.1:3306 for <REDACTED>
2018/03/22 09:37:42 Ready for new connections
{Sequel Pro connected just fine}
2018/03/22 09:37:50 New connection for "<REDACTED>"
2018/03/22 09:37:52 New connection for "<REDACTED>"
2018/03/22 09:38:01 Client closed local connection on 127.0.0.1:3306
2018/03/22 09:38:01 Client closed local connection on 127.0.0.1:3306
^C
Jamess-Mac-mini:~ jamesl$ ./cloud_sql_proxy -instances=<REDACTED>=tcp:3306 -credential_file=<REDACTED my new service account>.json
2018/03/22 09:38:19 using credential file for authentication; email=<REDACTED my new service account>
2018/03/22 09:38:19 Listening on 127.0.0.1:3306 for <REDACTED>
2018/03/22 09:38:19 Ready for new connections
{Sequel Pro failed to connect}
2018/03/22 09:38:23 New connection for "<REDACTED>"
2018/03/22 09:38:24 couldn't connect to "<REDACTED>": ensure that the
account has access to "<REDACTED>" (and make sure there's no typo in
that name). Error during createEphemeral for <REDACTED>: googleapi: 
Error 403: Access Not Configured. Cloud SQL Administration API has
not been used in project 773874261491 before or it is disabled.
Enable it by visiting https://console.developers.google.com/apis/api/sqladmin.googleapis.com/overview?project=<REDACTED>
then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry., accessNotConfigured
^C
Jamess-Mac-mini:~ jamesl$ ./cloud_sql_proxy -instances=<REDACTED>=tcp:3306 -credential_file=<REDACTED default service account>.json
2018/03/22 09:46:41 using credential file for authentication; email=<REDACTED default service account>
2018/03/22 09:46:41 Listening on 127.0.0.1:3306 for <REDACTED>
2018/03/22 09:46:41 Ready for new connections
{Sequel Pro failed to connect}
2018/03/22 09:46:45 New connection for "<REDACTED>"
2018/03/22 09:46:46 couldn't connect to "<REDACTED>": ensure that the account has access to "<REDACTED>" (and make sure there's no typo in that name). Error during createEphemeral for <REDACTED>: googleapi: Error 403: Access Not Configured. Cloud SQL Administration API has not been used in project <REDACTED> before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/sqladmin.googleapis.com/overview?project=<REDACTED> then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry., accessNotConfigured
^C

1 个答案:

答案 0 :(得分:1)

当Compute Engine实例的源IP可能正在更改时,您可以考虑使用Cloud SQL Proxy。本指南的第8步说明了如何启动代理,您可以使用mysql客户端(步骤9)来测试GCE实例的连接。

设置代理后,在connection URL中,使用&#39; localhost&#39;因为它将是在127.0.0.1上进行连接的代理。如果您使用TCP套接字连接,如果使用Unix套接字,则会有所不同。