简体   繁体   English

无法通过Java Eclipse连接到Google Cloud SQL

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

I'm having trouble to connect to my database from eclipse using my instance a google cloud sql database. 我无法使用我的实例Google Cloud SQL数据库从Eclipse连接到我的数据库。 I successed to access to the database via mysql command line but no in eclise. 我成功通过mysql命令行访问数据库,但没有成功。

My code: 我的代码:

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);

The Error I get: 我得到的错误:

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

You have an error message that says the environment variable GOOGLE_APPLICATION_CREDENTIALS is not set. 您收到一条错误消息,提示未设置环境变量GOOGLE_APPLICATION_CREDENTIALS Please create a .credentials file in a root folder and add your credential details in it. 请在根文件夹中创建一个.credentials文件,然后在其中添加您的凭据详细信息。

Please refer to this link https://developers.google.com/identity/protocols/application-default-credentials 请参考此链接https://developers.google.com/identity/protocols/application-default-credentials

The Application Default Credentials are not available. 应用程序默认凭据不可用。 They are available if running on Google App Engine, Google Compute Engine, or Google Cloud Shell. 如果它们在Google App Engine,Google Compute Engine或Google Cloud Shell上运行,则可用。 Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. 否则,必须定义环境变量GOOGLE_APPLICATION_CREDENTIALS,指向指向定义凭据的文件。 See https://developers.google.com/accounts/docs/application-default-credentials for more information. 有关更多信息,请参见https://developers.google.com/accounts/docs/application-default-credentials

You need credentials to access google cloud sql. 您需要凭据才能访问Google Cloud sql。 You can create from here 您可以从这里创建

After creating your credential, create your key file and point it in your bash_profile(read the block quote for more info). 创建凭证后,请创建密钥文件并将其指向您的bash_profile(请阅读块引用以获取更多信息)。 then it should work 那么它应该工作

Alternatively you can use CLOUD SQL PROXY to not work with environment variables, follow the steps in this link, if something is blurry please ask. 或者,您可以使用CLOUD SQL PROXY不适用于环境变量,请按照此链接中的步骤进行操作,如果有任何模糊之处,请询问。 https://cloud.google.com/sql/docs/mysql/connect-admin-proxy https://cloud.google.com/sql/docs/mysql/connect-admin-proxy

hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM