2017-05-03 5 views
0

구글 클라우드 SQL 데이터베이스를 사용하여 이클립스에서 내 데이터베이스에 연결하는 데 문제가 있습니다. mysql 명령 줄을 통해 데이터베이스에 액세스했지만 eclise에서 데이터베이스에 액세스하는 데 성공했습니다.자바 이클립스를 통해 Google 클라우드 SQL에 연결할 수 없습니다.

내 코드 :

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 
+0

귀하의 MySQL 서비스가 정상적으로 작동하고 있는지 확인 했습니까? – patateskafa

+0

예 @patateskafa –

답변

1

기본 자격 증명을 사용할 수없는 응용 프로그램을 참조하십시오. Google App Engine, Google Compute Engine 또는 Google Cloud Shell에서 실행중인 경우 사용할 수 있습니다. 그렇지 않으면 환경 변수 GOOGLE_APPLICATION_CREDENTIALS가 자격 증명을 정의하는 파일을 가리 키도록 정의되어야합니다. 자세한 내용은 https://developers.google.com/accounts/docs/application-default-credentials을 참조하십시오.

Google 클라우드 SQL에 액세스하려면 자격 증명이 필요합니다. here

신임장을 작성한 후 키 파일을 작성하여 bash_profile에 지정하십시오 (자세한 정보는 블록 견적을 읽으십시오).

또는 CLOUD SQL PROXY을 환경 변수와 함께 사용하지 않을 수 있습니다.이 링크의 단계를 따르십시오. 흐릿한 것이 있으면 물어보십시오. https://cloud.google.com/sql/docs/mysql/connect-admin-proxy

희망이 도움이됩니다.