我遵循指南
我正在填写这样的配置:
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.
更新:我已经尝试在第二个和第三个斜杠("jdbc:mysql://google/$DB_NAME"
(之间添加google
,根据这个答案,现在我得到了:
Cannot connect to MySQL server on google:3,306.
我缺少以下依赖项:
implementation("com.google.cloud.sql:mysql-socket-factory-connector-j-8:1.2.2")
更多信息点击这里
此外,DB_NAME
不是gcloud sql instances
输出的名称,而是应该在Console中创建的数据库名称->项目->Sql->数据库