【问题标题】:How to configure Java Hibernate for Google Cloud SQL?如何为 Google Cloud SQL 配置 Java Hibernate?
【发布时间】: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 中传递 cloudSqlInstancesocketFactory。这里我试图传递这些参数但它不起作用:

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


【解决方案1】:

对于 Google Cloud,您可以选择您的数据库实例,然后在“连接”下,您可以选择允许来自公共 IP 地址的连接,然后将您机器的 IP 地址添加到授权建立连接的地址列表中。从那里您需要做的就是在 URL 中使用数据库实例的公共 IP 地址。

【讨论】:

  • 我明白你的意思了,你能告诉我如何传递“cloudSqlInstance”吗?
【解决方案2】:

在 Google Cloud Console 的“实例详细信息”页面上查找实例的 INSTANCE_CONNECTION_NAME。它使用 PROJECT_ID:REGION:INSTANCE_ID 格式,用于标识您要连接的 Cloud SQL 实例。

要启用本地 TCP 端口,请将以下内容添加到项目的 app.yaml 文件中:

runtime: java
env: flex
beta_settings:
  cloud_sql_instances: <INSTANCE_CONNECTION_NAME>=tcp:<PORT>

然后这里是我的 hibernate.properties 文件:

hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://172.17.0.1:PORT_NUMBER_IN_YAML_FILE
hibernate.connection.username = DB_USERNAME
hibernate.connection.password = DB_PASSWORD
hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
hibernate.default_schema = DATABASE_NAME
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create

更多详情您可以访问:https://cloud.google.com/sql/docs/mysql/connect-app-engine

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    相关资源
    最近更新 更多