GoogleCloudPlatform / cloud-sql-jdbc-socket-factory

A collection of Java libraries for connecting securely to Cloud SQL
Apache License 2.0
229 stars 118 forks source link

SQLNonTransientConnectionException: Cannot connect to MySQL server #476

Closed PhilipDukhov closed 3 years ago

PhilipDukhov commented 3 years ago

I'm following this guide

I'm filling the config like this:

val datasourceConfig = HikariConfig().apply {
    jdbcUrl = "jdbc:mysql:///$DB_NAME"
    username = DB_PASS
    password = DB_USER
    mapOf(
        "cloudSqlInstance" to CLOUD_SQL_CONNECTION_NAME,
        "socketFactory" to "com.google.cloud.sql.mysql.SocketFactory",
        "ipTypes" to "PUBLIC,PRIVATE",
    ).forEach {
        addDataSourceProperty(
            it.key,
            it.value
        )
    }
}

output of the gcloud sql instances describe project-name:

backendType: SECOND_GEN
connectionName: project-name:europe-west1:project-name-db
databaseVersion: MYSQL_5_7
failoverReplica:
  available: true
gceZone: europe-west1-d
instanceType: CLOUD_SQL_INSTANCE
ipAddresses:
- ipAddress: *.*.*.*
  type: PRIMARY
kind: sql#instance
name: project-name-db
project: project-name
region: europe-west1

from which I'm filling my env variables:

DB_NAME=project-name-db
CLOUD_SQL_CONNECTION_NAME=project-name:europe-west1:project-name-db

On the deployed app line val dataSource = HikariDataSource(datasourceConfig) crashes with the following exception:

com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: Cannot connect to MySQL server on localhost:3,306.

Make sure that there is a MySQL server running on the machine/port you are trying to connect to and that the machine this software is running on is able to connect to this host/port (i.e. not firewalled). Also make sure that the server has not been started with the --skip-networking flag.

I've tried adding google between second and third slashes("jdbc:mysql://google/$DB_NAME"), according to this answer, but I still get:

Cannot connect to MySQL server on google:3,306.
kurtisvg commented 3 years ago

Looks like this is cross-posted on SO.

PhilipDukhov commented 3 years ago

Yes, I haven't got any responses from there that's why I decided to ask here

kurtisvg commented 3 years ago

No problem - just trying to tie the breadcrumbs together incase the solution is discovered in a different location :)

I'll let @shubha-rajan take a more detailed look, but my first guess is that the java-connector isn't added as a dependency for your project.

PhilipDukhov commented 3 years ago

Indeed, so stupid of me :D

After updating it, I got next error:

java.sql.SQLSyntaxErrorException: Unknown database 'project-name-db'
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:833)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
    at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81)

Also in logs I noticed following warning:

Exceeded hard memory limit of 256 MB with 261 MB after servicing 0 requests total. Consider setting a larger instance class in app.yaml

Is it normal considering my app crashes on trying to connect to the database? I haven't found any parameters related to memory limit, I checked here

shubha-rajan commented 3 years ago

Have you created a database in your Cloud SQL instance? From the info you shared, it looks like your instance name and database name are the same. The error suggests that either your database doesn't exist, or that the name is wrong.

The instance class issue with AppEngine seems unrelated to the database connection issues

PhilipDukhov commented 3 years ago

Oh I see now, I thought I need to take DB_NAME from sql instances describe, but those are different things. Works now