这有点奇怪。
在过去的一周内,我已经能够连接到我在 GCP 中创建的测试数据库。 我没有下载凭据,也没有使用信誉配置更改任何内容。 我只是在我的应用程序属性中更新了数据库源 URL。
- 我仍然可以在本地连接而不会出现问题。
这是相关的应用程序属性部分:
# ===============================
# = 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
所以,我的问题几乎是 - 任何人都知道为什么它以前有效,为什么 ti 现在不工作?做我需要凭据文件才能正常工作,如果是这样,上次我是如何如此轻松地连接的? 而且,在我发布的这两个中,谁能告诉我定义数据源的最佳方法是什么,而不必跳过过多的箍来连接到数据库?
提前感谢!
我会删除它,但它可能会帮助其他人。
我的 IP 不是静态的,在 SQL 实例的连接页面上是授权的公网 IP。 这是我的家庭IP,但后来发生了变化。
叹息。