【问题标题】:Unable to determine jdbc url from datasource when connecting to Google SQL连接到 Google SQL 时无法从数据源确定 jdbc url
【发布时间】:2019-09-04 20:32:09
【问题描述】:

这有点奇怪。

在上周,我已经能够连接到我在 GCP 中创建的测试数据库。我没有下载凭据,也没有使用 cred 配置更改任何内容。我只是在 application.properties 中更新了数据库源 URL。

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

这是相关的 application.properties 部分:

# ===============================
# = 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

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

    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

我已经检查过: -我可以使用云终端连接到谷歌数据库

-数据库已启动并运行

-没有防火墙等

-应用程序没有发生任何戏剧性的事情(添加了存储方法,没有别的)

  • 我尝试在连接前使用终端将我的 IP 列入白名单
  • 我检查了云日志,它已启动、运行并等待连接。
  • 我没有向连接发送垃圾邮件,所以那里应该没有问题。
  • 仔细检查 pom.xml 以确保 JDBC 仍然存在
  • 对属性和 pom 进行比较以检查是否存在可疑内容。

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

spring.datasource.url=jdbc:mysql://google/<DBNAME>?cloudSqlInstance=<MYDABINSTANCENAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory

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

    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

所以,我的问题非常多 - 任何人都知道为什么它以前有效,为什么现在不有效? 我需要凭据文件才能工作,如果是这样,我上次是如何轻松连接的?而且,在我发布的那两个中,任何人都可以告诉我定义数据源的最佳方法是什么,而不必跳过过多的箍来连接到数据库?

提前致谢!

【问题讨论】:

    标签: java spring-boot jdbc google-cloud-platform google-cloud-sql


    【解决方案1】:

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

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

    叹息。

    【讨论】:

      猜你喜欢
      • 2019-04-21
      • 2018-08-10
      • 2019-01-31
      • 2018-12-25
      • 2013-04-13
      • 2018-11-08
      • 2021-05-12
      • 1970-01-01
      • 2018-04-07
      相关资源
      最近更新 更多