簡體   English   中英

無法通過Java Eclipse連接到Google Cloud SQL

[英]Unable to connect to Google cloud SQL via Java eclipse

我無法使用我的實例Google Cloud SQL數據庫從Eclipse連接到我的數據庫。 我成功通過mysql命令行訪問數據庫,但沒有成功。

我的代碼:

String instanceConnectionName = "****";
String databaseName = "****";
String username = "***";
String password = "***";

 String jdbcUrl = String.format(
            "jdbc:mysql://google/%s?cloudSqlInstance=%s&"
        + "socketFactory=com.google.cloud.sql.mysql.SocketFactory",
            databaseName,
            instanceConnectionName);         
 Connection connection = DriverManager.getConnection(jdbcUrl, username, password);

我得到的錯誤:

jdbc:mysql://google/****?cloudSqlInstance=****e&socketFactory=com.google.cloud.sql.mysql.SocketFactory

May 03, 2017 10:53:07 AM com.google.cloud.sql.mysql.SocketFactory connect
INFO: Connecting to Cloud SQL instance [****].
May 03, 2017 10:53:07 AM com.google.cloud.sql.mysql.SslSocketFactory getInstance
INFO: First Cloud SQL connection, generating RSA key pair.
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2332)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2085)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at neviTracker.program.ProgramConnections.main(ProgramConnections.java:27)
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)
at com.google.cloud.sql.mysql.SslSocketFactory.getInstance(SslSocketFactory.java:138)
at com.google.cloud.sql.mysql.SocketFactory.connect(SocketFactory.java:47)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2286)
... 13 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.mysql.SslSocketFactory$ApplicationDefaultCredentialFactory.create(SslSocketFactory.java:543)
... 18 more

您收到一條錯誤消息,提示未設置環境變量GOOGLE_APPLICATION_CREDENTIALS 請在根文件夾中創建一個.credentials文件,然后在其中添加您的憑據詳細信息。

請參考此鏈接https://developers.google.com/identity/protocols/application-default-credentials

應用程序默認憑據不可用。 如果它們在Google App Engine,Google Compute Engine或Google Cloud Shell上運行,則可用。 否則,必須定義環境變量GOOGLE_APPLICATION_CREDENTIALS,指向指向定義憑據的文件。 有關更多信息,請參見https://developers.google.com/accounts/docs/application-default-credentials

您需要憑據才能訪問Google Cloud sql。 您可以從這里創建

創建憑證后,請創建密鑰文件並將其指向您的bash_profile(請閱讀塊引用以獲取更多信息)。 那么它應該工作

或者,您可以使用CLOUD SQL PROXY不適用於環境變量,請按照此鏈接中的步驟進行操作,如果有任何模糊之處,請詢問。 https://cloud.google.com/sql/docs/mysql/connect-admin-proxy

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM