首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >连接到Google SQL时无法从数据源确定jdbc url。

连接到Google SQL时无法从数据源确定jdbc url。
EN

Stack Overflow用户
提问于 2019-04-15 03:43:35
回答 1查看 322关注 0票数 1

这有点奇怪。

在过去的一周内,我已经能够连接到我在GCP中创建的测试数据库。我没有下载凭证,也没有更改任何凭证配置。我只是更新了application.properties中的数据库源url。

  • 我仍然可以毫无问题地进行本地连接。

这是相关的application.properties部分:

代码语言:javascript
复制
# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
#spring.datasource.url=jdbc:mysql://localhost:3306/gametest
#gcp
#spring.datasource.url=jdbc:mysql://35.222.131.172:3306/gametest
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

今天,我尝试连接并接收此异常

代码语言:javascript
复制
    com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_45]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_45]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_45]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_45]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:342) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2221) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2016) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_45]
    at

我已经检查过了:-I可以使用云终端连接到Google DB

-The DB已启动并正在运行

-No防火墙等已就位

这个应用程序已经发生了-Nothin戏剧性的变化(添加了存储方法,没有其他内容)

  • 我尝试在连接之前使用终端将我的IP列入白名单
  • 我检查了云日志,它已经启动,正在运行,正在等待connections.
  • I've没有发送垃圾邮件连接所以应该没有问题there.
  • Double检查pom.xml以确保JDBC仍然存在
  • 对属性和pom进行了比较以检查不可靠的东西。

在一些SO帖子之后,我发现有人建议这是正确的方法:

代码语言:javascript
复制
spring.datasource.url=jdbc:mysql://google/<DBNAME>?cloudSqlInstance=<MYDABINSTANCENAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory

我已经在中添加了它,现在收到了另一个指向API凭据的异常。

代码语言:javascript
复制
    Caused by: java.lang.RuntimeException: Unable to obtain credentials to communicate with the Cloud SQL API
    at com.google.cloud.sql.mysql.SslSocketFactory$ApplicationDefaultCredentialFactory.create(SslSocketFactory.java:545) ~[mysql-socket-factory-1.0.0.jar:na]
    at com.google.cloud.sql.mysql.SslSocketFactory.getInstance(SslSocketFactory.java:138) ~[mysql-socket-factory-1.0.0.jar:na]
    at com.google.cloud.sql.mysql.SocketFactory.connect(SocketFactory.java:47) ~[mysql-socket-factory-1.0.0.jar:na]
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:301) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2221) ~[mysql-connector-java-5.1.46.jar:5.1.46]
    ... 61 common frames omitted
Caused by: java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
    at com.google.api.client.googleapis.auth.oauth2.DefaultCredentialProvider.getDefaultCredential(DefaultCredentialProvider.java:95) ~[google-api-client-1.21.0.jar:1.21.0]
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.getApplicationDefault(GoogleCredential.java:213) ~[google-api-client-1.21.0.jar:1.21.0]
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.getApplicationDefault(GoogleCredential.java:191) ~[google-api-client-1.21.0.jar:1.21.0]
    at com.google.cloud.sql.mysql.SslSocketFactory$ApplicationDefaultCredentialFactory.create(SslSocketFactory.java:543) ~[mysql-socket-factory-1.0.0.jar:na]
    ... 66 common frames omitted

所以,我的问题是--有人知道为什么它以前有效,为什么现在不行吗?我需要凭据文件才能工作吗?如果需要,我上次是如何如此轻松地连接的?并且,在我发布的这两个方法中,谁能告诉我定义数据源的最好方法是什么,而不需要通过过多的环路连接到数据库?

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2019-04-15 04:43:06

我会删除这篇文章,但它可能会对其他人有所帮助。

我的IP不是静态的,在SQL实例的连接页面上是一个授权的公网IP。这是我的家庭IP地址,但后来它发生了变化。

叹一口气。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55679357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档