使用Java连接到Google Cloud SQL [英] Connecting to Google Cloud SQL with Java

查看:76
本文介绍了使用Java连接到Google Cloud SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了好几天,却一无所获.我正在尝试通过正在构建的JavaFX程序连接到我的MySQL数据库,而不要求我将尝试连接的每个IP列入白名单.GCP支持团队已答复我一次,但完全误解了该问题(提供了一些日志示例,这些日志仅在我将自己的IP列入白名单以测试程序的其他方面之后出现).

I've been trying for days and have gotten nowhere. I am trying to connect to my MySQL database through a JavaFX program I'm building, without requiring me to whitelist every IP that attempts to connect. The GCP support team has replied to me once but completely misinterpreted the issue (gave examples of logs that only occurred after I whitelisted my own IP to test the other aspects of my program).

我在 https://cloud.google找到了说明.com/sql/docs/mysql/connect-external-app#java ,然后将以下代码粘贴到我的主要方法中(用适当的值替换databaseName,instanceConnectionName,用户名和密码):

I found instructions at https://cloud.google.com/sql/docs/mysql/connect-external-app#java, and pasted the following code into my main method (substituting the appropriate values for databaseName, instanceConnectionName, username, and password):

String jdbcUrl = String.format(
"jdbc:mysql://google/%s?cloudSqlInstance=%s"
+ "&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false",
databaseName,
instanceConnectionName);

Connection connection = DriverManager.getConnection(jdbcUrl, username, 
password);

然后,我启用了Cloud API,并据我所知,按照指示安装并验证了Cloud SDK.尽管如此,尽管文档说这是解决方法,但如果没有列入白名单的IP地址,我仍然无法连接到该实例.有人看到我尝试连接的方式有问题或知道如何进行此工作吗?

I then enabled the Cloud API and, to the best of my knowledge, installed and authenticated the Cloud SDK, as directed. Yet despite all of that, I still cannot connect to the instance without a whitelisted IP address, even though the documentation says this is the workaround for that. Does anyone see an issue with how I'm attempting to connect or know how to make this work?

推荐答案

自述文件中针对回购.

一些潜在的跳闸点:

  1. 使用应用程序默认凭据提供工厂凭证.

确保已为项目启用 Cloud SQL API (并且,如果您使用的是服务帐户,请确保具有 Cloud SQL Client 角色).

Make sure you have the Cloud SQL API enabled to your project (and if you are using a service account, make sure to have the Cloud SQL Client role added to it).

将库添加为

Add the library as a dependency in your POM or gradlefile.

确保防火墙在端口3307上允许Cloud SQL实例通过.

Make sure your firewall allows out on port 3307 to your Cloud SQL instance.

这篇关于使用Java连接到Google Cloud SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆