gpt4 book ai didi

mysql - Spring Boot 应用程序无法连接到 Google Cloud SQL

转载 作者:行者123 更新时间:2023-11-30 21:48:04 31 4
gpt4 key购买 nike

我无法将我的 Spring Boot 应用程序连接到 Cloud MySQL。我在 App Engine 和未连接到 Cloud SQL 的网站上部署我的应用程序工作正常。但是,每当我的应用程序尝试连接到云时,我都会收到错误消息。我遵循了 https://cloud.google.com/appengine/docs/flexible/java/using-cloud-sql?hl=it 的灵活环境和标准环境的指南。但它似乎不起作用。我尝试按照宠物诊所教程进行操作,但没有用。我将以下依赖项放在文件 pom.xml 中:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory-connector-j-6</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

我的 application.properties 中有以下项目:

spring.jpa.database=MYSQL
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://google/DATABASENAME?cloudSqlInstance=PROJECTID:REGION:INSTANCE&socketFactory=com.google.cloud.sql.mysql.SocketFactory
spring.datasource.username=USER
spring.datasource.password=*********

当我尝试打开试图连接到数据库的网站时遇到的错误是:出现意外错误(类型=内部服务器错误,状态=500)。无法打开 JPA EntityManager 进行交易;嵌套异常是 javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection

我需要做什么才能连接到 Google Cloud?

最佳答案

尝试在同一个属性上使用用户名和密码

spring.datasource.url=jdbc:mysql://google/DATABASE?cloudSqlInstance=INTANCE_NAME&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=USERNAME_HERE&password=PASSWORD_HERE

spring.jpa.hibernate.ddl-auto=none
spring.datasource.username=USERNAME_HERE
spring.datasource.password=PASSWORD_HERE
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database=MYSQL
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

然后确保你有这些依赖

<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.22.0</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId>
<version>1.0.5</version>
</dependency>

记得为你的项目启用google cloud sql api

这对我来说适用于标准环境。我使用下一个说明使我的项目与标准兼容 https://github.com/GoogleCloudPlatform/getting-started-java/tree/master/appengine-standard-java8/springboot-appengine-standard

我还没有在 flexible 上测试过祝你好运¡

关于mysql - Spring Boot 应用程序无法连接到 Google Cloud SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48538105/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com