【发布时间】:2019-12-08 14:37:28
【问题描述】:
我正在使用 Hibernate + Java + Jersey + MYSql,它在本地机器上运行良好。但是我无法在 Google Cloud 上创建与 MySql 的连接。
我认为 MYSql 配置和实例创建在 Google Cloud 上很好,但我无法通过休眠属性传递所有配置。在此处查看 Google 的示例代码:
// The configuration object specifies behaviors for the connection pool.
HikariConfig config = new HikariConfig();
// Configure which instance and what database user to connect with.
config.setJdbcUrl(String.format("jdbc:mysql:///%s", DB_NAME));
config.setUsername(DB_USER); // e.g. "root", "postgres"
config.setPassword(DB_PASS); // e.g. "my-password"
// For Java users, the Cloud SQL JDBC Socket Factory can provide authenticated connections.
// See https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory for details.
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory");
config.addDataSourceProperty("cloudSqlInstance", CLOUD_SQL_CONNECTION_NAME);
config.addDataSourceProperty("useSSL", "false");
// ... Specify additional connection properties here.
// ...
// Initialize the connection pool using the configuration object.
DataSource pool = new HikariDataSource(config);
现在我不知道如何在 Hibernate 中传递 cloudSqlInstance 和 socketFactory。这里我试图传递这些参数但它不起作用:
hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://google/pashumandi_db?cloudSqlInstance=pashuserver:asia-south1:mysql-instance&socketFactory=com.google.cloud.sql.mysql.SocketFactory
hibernate.connection.username = abc_user
hibernate.connection.password = 12345678
hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
您能否告诉我在 Google Cloud 上连接 MySql 的正确休眠配置是什么?谢谢。
【问题讨论】:
-
您是否将 Google Cloud DB 配置为允许从您的 IP 地址进行外部连接?
-
嘿,我看到您设法与 Hibernate 连接,因为您对证书等提出了另一个问题。你能解决这个问题吗?如果没有,请告诉我,因为我对 Hibernate 与 Cloud SQL 的连接性感到好奇,我们会进一步研究。
标签: java mysql hibernate google-app-engine google-cloud-sql