Search code examples
mysqlspringgoogle-cloud-sqlhikaricp

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


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);
    }
}
  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 is under the databases section of SQL console. Is it the one that same as "USE " 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.


Solution

  • 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.