1

Assumptions

We are developing a WebAPI with SpringBoot on GoogleCloud's GCE. We have confirmed that the Spring project starts successfully.
Java 11
Spring 2.7.6
mysql 5.7

What we want to achieve

I want to connect to CloudSQL from SpringBoot and get data from DB.

Problem

I get the following error and cannot connect.

SQLException: Access denied for user 'root'@'cloudsqlproxy~192.168.0.5' (using password: YES)

What I tried

  1. Connecting in local environment  Installed MySQL in local environment and connected.
  2. Deploy to GCE, request with Postman, check response (without using DB) → received response with Postman, OK
  3. SSH to GCE, connect to MySQL using mysql command→Connected and was able to check table contents, OK
  4. Response using DB -> connection did not work and NG  

Configuration file

build.gradle

Java
plugins {
    id 'java'
    id 'org.springframework.boot' version '2.7.6'
    id 'io.spring.dependency-management' version '1.0.15.RELEASE'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}
repositories {
    mavenCentral()
}
dependencies {
    // implementation 'org.springframework.boot:spring-boot-starter-security'
//  implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    // implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity5'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    // testImplementation 'org.springframework.security:spring-security-test'

    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.3.0'
    runtimeOnly 'mysql:mysql-connector-java'
    runtimeOnly 'com.google.cloud.sql:mysql-socket-factory-connector-j-8:1.0.15'
}
tasks.named('test') {
    useJUnitPlatform()
}

application.properties

database=mysql
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://google/<DB-Name>?cloudSqlInstance=<CONNECTION NAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory
spring.datasource.username=root
spring.datasource.password=<PASSWORD>

error log

2022-12-21 13:49:25.022 ERROR 1576 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: Access denied for user 'root'@'cloudsqlproxy~192.168.0.5' (using password: YES)
katahik
  • 427
  • 3
  • 14
  • Can you check this [link1](https://howtodoinjava.com/sql/sqlexception-access-denied-for-user-rootlocalhost-after-re-installation-of-mysql-server/) and [link2](https://stackoverflow.com/a/67191380/18265570)? – Roopa M Dec 22 '22 at 07:10

1 Answers1

0

I was able to connect with the following settings.

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://CloudSQLIPAddress/DBname
spring.datasource.username=test_user
spring.datasource.password=Password
katahik
  • 427
  • 3
  • 14