谢谢!
我是gcp的新手,正在尝试使用Cloud SQL Postgres。 我有一个已创建的数据库,并且想使用Java或Scala在其中创建/更新/删除表。
我正在尝试使用来自com.google.cloud.sql包的postgres-socket-factory。 但是在建立连接时出现错误
"java.sql.SQLException: No suitable driver found for jdbc:postgres://google/"
这是我的代码
def getConnection(url:String) : Connection = {
Class.forName("org.postgresql.Driver")
import java.sql.DriverManager
DriverManager.getConnection(url)
}
Java / Scala客户端从Cloud SQL访问数据库的方式是什么?
是,在按照注释中的说明进行操作并更改了有效网址之后,却显示
失败 Caused by: java.lang.RuntimeException: Unable to obtain credentials to communicate with the Cloud SQL API
at com.google.cloud.sql.core.SslSocketFactory$ApplicationDefaultCredentialFactory.create(SslSocketFactory.java:600)
at com.google.cloud.sql.core.SslSocketFactory.getInstance(SslSocketFactory.java:147)
at com.google.cloud.sql.postgres.SocketFactory.createSocket(SocketFactory.java:91)
at org.postgresql.core.PGStream.<init>(PGStream.java:62)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195)
at org.postgresql.Driver.makeConnection(Driver.java:454)
at org.postgresql.Driver.connect(Driver.java:256)
... 6 more
Caused by: java.io.IOException: The Application Default Credentials are not available. They are available if running on Google App Engine, Google Compute Engine, or Google Cloud Shell. 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:98)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.getApplicationDefault(GoogleCredential.java:213)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.getApplicationDefault(GoogleCredential.java:191)
at com.google.cloud.sql.core.SslSocketFactory$ApplicationDefaultCredentialFactory.create(SslSocketFactory.java:598)
... 15 more
如何解决此故障?
此致
答案 0 :(得分:0)
GCP上的大多数库都使用Application Default Credentials(ADC)策略来处理凭据。该项目的README也提到了该库。 Cloud SQL JDBC套接字工厂使用这些凭据来验证与数据库的连接。
最快的方法是使用gcloud auth application-default login
,这会将您的个人凭据设置为默认值。但是,最安全最安全的方法是为应用程序创建一个Service Account,为其授予“ Cloud SQL Client” IAM角色,并使用GOOGLE_APPLICATION_CREDENTIALS
环境变量来传递服务帐户密钥的位置到图书馆。