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

时间:2019-04-14 19:43:35

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

这有点奇怪。

在上周内,我已经能够连接到我在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

我检查了: -我可以使用Cloud终端连接到Google DB

-数据库已启动并正在运行

-没有防火墙等

-应用程序发生了Nothin戏剧性(添加了存储方法,仅此而已)

  • 我尝试在连接之前使用终端将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

所以,我的问题非常多-有人知道为什么以前可行,为什么现在不可行? DO 我需要凭据文件才能正常工作,如果可以,我上一次如何轻松连接?而且,在我发布的这两种方法中,谁能告诉我定义数据源的最佳方法是什么,而不必跳过过多的连接数据库的麻烦?

谢谢!

1 个答案:

答案 0 :(得分:1)

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

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

叹气。