从本地机连接gcp上的第二代mysql

neskvpey  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(164)

我正在尝试为某个restapi构建一个spring引导应用程序。作为其中的一部分,我决定使用gcp cloud sql(mysql服务)。它是所有配置和设置的,使用控制台上提到的说明。
现在我正试图从我的机器上用小型jdbc客户机连接到clouddb。我正在使用的库是用于jdbc连接的googlesdk Package 器的一部分。

compile 'com.google.cloud.sql:mysql-socket-factory:1.0.9'

我已经配置了服务帐户并以json格式创建了密钥文件。设置google\u application\u credentials=d:\serviceaccountkeygcp\streaming.json。
在做了所有这些之后,我仍然会犯错误

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 TestCloudConnection.main(TestCloudConnection.java:15)
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:556)
    at com.google.cloud.sql.core.SslSocketFactory.getInstance(SslSocketFactory.java:143)
    at com.google.cloud.sql.mysql.SocketFactory.connect(SocketFactory.java:56)
    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.core.SslSocketFactory$ApplicationDefaultCredentialFactory.create(SslSocketFactory.java:554)
    ... 18 more

示例代码:

//System.out.println(GoogleCredential.getApplicationDefault().getServiceAccountPrivateKey().getAlgorithm());

            //GoogleCredential credential=GoogleCredential.fromStream(new FileInputStream(new File("D:\\ServiceAccountKeyGCP\\streaming-208006-393bbeb34baf.json")));
            //System.out.println(credential.getServiceAccountId());
            Connection connection=DriverManager.getConnection("jdbc:mysql://google/streaming?cloudSqlInstance=streaming-208006:asia-southeast1:streamingdev&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=root&password=streamingdev&useSSL=false");
    System.out.println(connection.getCatalog());

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题