jvm 将应用引擎连接到Google云SQL失败

rhfm7lfc  于 2022-11-07  发布在  Go
关注(0)|答案(1)|浏览(153)

我正在遵循此指南
我这样填充配置:

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
        )
    }
}

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

我从其中填充env变量:

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

在已部署的应用程序行val dataSource = HikariDataSource(datasourceConfig)上,崩溃并出现以下异常:

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.

update:我试着在第二个和第三个斜杠之间添加google"jdbc:mysql://google/$DB_NAME"),根据this answer,现在我得到:

Cannot connect to MySQL server on google:3,306.
b4lqfgs4

b4lqfgs41#

我缺少以下依赖项:

implementation("com.google.cloud.sql:mysql-socket-factory-connector-j-8:1.2.2")

更多信息here
此外,DB_NAME不是gcloud sql instances输出的名称,而是应该在Console -〉Project -〉Sql -〉Databases中创建的数据库名称

相关问题