无法从Spring Boot应用程序创建与Google Cloud SQL(MySQL)的连接。

huangapple go评论58阅读模式
英文:

Could not create connection from Spring Boot app to Google Cloud SQL (mySQL)

问题

  1. 我检查了配置。我尝试添加了许多类似帖子上看到的配置,但测试结果仍然相同。所以我删除了它们,只保留了来自Google Cloud指南的部分。
    我想确保的一件事是<database-name>在SQL控制台的数据库部分下。它是否与我在云shell上执行SQL命令时使用的"USE <database-name>"相同?

  2. 我检查了数据库设置。我确保数据库正在运行,并且我可以使用相同的用户名和密码执行SQL命令。数据库仅允许公共访问。我已经启用了sqladmin API。

  3. 我尝试添加了类似帖子上看到的不同依赖项,但结果并未改变,所以我删除了其中的一些。实际上,我不确定我现在拥有的依赖项是否正确。如果它们是错误的,请告诉我。

  4. 我检查了我的代码,但基本上我只是使用配置类中的数据源创建了jdbcTemplate,然后我使用这个jdbcTemplate调用了queryForObject。从错误消息来看,我认为代码本身是正确的。

英文:

I am trying to connect a Spring Boot application to a Cloud SQL database and I keep getting the same error.
The stacktrace:

com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: Could not create connection to database server.
 at com.zaxxer.hikari.pool.HikariPool.throwPoolInitializationException(HikariPool.java:596)
 at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:582)
[delete some lines because of spam detection]
 ... 74 more
Caused by: java.lang.RuntimeException: Unable to obtain credentials to communicate with the Cloud SQL API
 at com.google.cloud.sql.core.CoreSocketFactory$ApplicationDefaultCredentialFactory.create(CoreSocketFactory.java:392)
[delete some lines because of spam detection]
 ... 83 more
Caused by: java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See [delete the link because of spam detection] for more information.
 at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:125)
 at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:125)
[delete some lines because of spam detection]
 ... 91 more

Dependencies:

		&lt;dependency&gt;
        	&lt;groupId&gt;com.google.cloud&lt;/groupId&gt;
        	&lt;artifactId&gt;spring-cloud-gcp-dependencies&lt;/artifactId&gt;
        	&lt;version&gt;3.3.0&lt;/version&gt;
        	&lt;type&gt;pom&lt;/type&gt;
       		&lt;scope&gt;import&lt;/scope&gt;
        &lt;/dependency&gt;

		&lt;dependency&gt;
            &lt;groupId&gt;com.zaxxer&lt;/groupId&gt;
            &lt;artifactId&gt;HikariCP&lt;/artifactId&gt;
			&lt;version&gt;5.0.1&lt;/version&gt;
        &lt;/dependency&gt;

		&lt;dependency&gt;
			&lt;groupId&gt;mysql&lt;/groupId&gt;
			&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
			&lt;version&gt;8.0.31&lt;/version&gt;
			&lt;scope&gt;runtime&lt;/scope&gt;
		&lt;/dependency&gt;
		
		&lt;dependency&gt;
    		&lt;groupId&gt;com.google.cloud.sql&lt;/groupId&gt;
    		&lt;artifactId&gt;mysql-socket-factory&lt;/artifactId&gt;
    		&lt;version&gt;1.11.0&lt;/version&gt;
		&lt;/dependency&gt;

		&lt;dependency&gt;
    		&lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
    		&lt;artifactId&gt;spring-cloud-openfeign-core&lt;/artifactId&gt;
    		&lt;version&gt;4.0.1&lt;/version&gt;
		&lt;/dependency&gt;

Configuration class:

@Configuration
public class DatabaseConfig {

    @Bean
    public static DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(String.format(&quot;jdbc:mysql://google/%s&quot;, &lt;database-name&gt;));
        config.setUsername(&lt;username&gt;);
        config.setPassword(&lt;password&gt;);
        config.setMaximumPoolSize(5);
        config.addDataSourceProperty(&quot;socketFactory&quot;, &quot;com.google.cloud.sql.mysql.SocketFactory&quot;);
        config.addDataSourceProperty(&quot;cloudSqlInstance&quot;, &quot;&lt;project-id&gt;:&lt;location&gt;:&lt;instance-name&gt;&quot;);

        return new HikariDataSource(config);
    }
}
  1. I checked for the configuration. I tried to add many configurations that I saw on similar threads, but the test still remains the same result. So I delete them and only keep the part from the Google Cloud Guide.
    One thing I do want to make sure is that <database-name> is under the databases section of SQL console. Is it the one that same as "USE <database-name>" when I try to execue SQL commands on the cloud shell?

  2. I checked for the database setting. I made sure the database is working and I can perform SQL commands using the same username and password. The database allows only public access. I have enabled the sqladmin API.

  3. I tried adding different dependencies I saw on similar threads but it didn't change anything either so I deleted some of them. Actually I am not sure if the dependencies I have now are correct. Please let me know if they are wrong.

4.I checked for the code I have, but basically I just creating the jdbcTemplate using the datasource from the configuration class, and then I call queryForObject using this jdbcTemplate. From the error msg, I think the code itself is correct.

答案1

得分: 0

你需要配置应用程序默认凭据,这是Java连接器进行Cloud SQL Admin API调用的方式。这一步骤与与数据库进行身份验证是分开的。

英文:

You'll need to configure Application Default Credentials, which is how the Java Connector makes Cloud SQL Admin API calls. This step is separate from authenticating with your database.

huangapple
  • 本文由 发表于 2023年3月9日 16:54:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定