英文:
Could not create connection from Spring Boot app to Google Cloud SQL (mySQL)
问题
-
我检查了配置。我尝试添加了许多类似帖子上看到的配置,但测试结果仍然相同。所以我删除了它们,只保留了来自Google Cloud指南的部分。
我想确保的一件事是<database-name>在SQL控制台的数据库部分下。它是否与我在云shell上执行SQL命令时使用的"USE <database-name>"相同? -
我检查了数据库设置。我确保数据库正在运行,并且我可以使用相同的用户名和密码执行SQL命令。数据库仅允许公共访问。我已经启用了sqladmin API。
-
我尝试添加了类似帖子上看到的不同依赖项,但结果并未改变,所以我删除了其中的一些。实际上,我不确定我现在拥有的依赖项是否正确。如果它们是错误的,请告诉我。
-
我检查了我的代码,但基本上我只是使用配置类中的数据源创建了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:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>spring-cloud-gcp-dependencies</artifactId>
<version>3.3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId>
<version>1.11.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
<version>4.0.1</version>
</dependency>
Configuration class:
@Configuration
public class DatabaseConfig {
@Bean
public static DataSource dataSource() {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(String.format("jdbc:mysql://google/%s", <database-name>));
config.setUsername(<username>);
config.setPassword(<password>);
config.setMaximumPoolSize(5);
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory");
config.addDataSourceProperty("cloudSqlInstance", "<project-id>:<location>:<instance-name>");
return new HikariDataSource(config);
}
}
-
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? -
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.
-
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论