gpt4 book ai didi

java - 无法访问 AWS Tomcat 上的 MySQL 数据库,但可以在本地 Tomcat 上正常工作。 (JDBS 连接错误)

转载 作者:行者123 更新时间:2023-11-29 05:05:45 25 4
gpt4 key购买 nike

我是新手,正在尝试制作我的第一个 RESTful 服务。我用 Spring Boot 实现了它,JPA 从 MySQL 数据库映射并作为 war 工件部署到 Tomcat。我还使用所有默认设置在 RDC 服务中部署到 AWS 的 MySQL 数据库。所有在本地 Tomcat 上运行良好。当在 AWS tomcat 上尝试相同的 war 工件时:使用所有默认设置部署:web API 工作:RESTful 服务使用返回的 java 资源工作,但在尝试从数据库检索数据时连接到数据库不起作用。在日志中我可以看到这个:Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failureCaused by: org.hibernate.exception.JDBCConnectionException: 无法获取 JDBC 连接

我的设置:应用程序属性:

spring.datasource.url=jdbc:mysql://nz4.czzj8lfy5a3f.eu-central-1.rds.amazonaws.com/webshop
# I tryed here also with (and no difference)…com:3306/webshop
spring.datasource.username=… (secret)
spring.datasource.password=… (secret)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=none

在 build:gradle 中我有依赖:

compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
compile('org.springframework.boot:spring-boot-starter-data-rest')

网络API代码示例:

@Entity
@Table (name ="customer")
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "customer_id")
private Integer customerId;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
...
//getters setters

public interface CustomerRepository extends JpaRepository<Customer, Integer>
{}

@RestController
@CrossOrigin
@RequestMapping(value = "/api")
public class CustomerResource {
@Autowired
CustomerRepository customerRepository;
@GetMapping(value = "/customer")
public List< Customer> > getAll() {return customerRepository.findAll();}
...

@SpringBootApplication
public class WebshopApplication extends SpringBootServletInitializer {
public static void main(String[] args)
{SpringApplication.run(WebshopApplication.class, args);}
}

最佳答案

安全组充当虚拟防火墙,控制一个或多个实例的流量。当您启动一个实例时,您将一个或多个安全组与该实例相关联。您向每个安全组添加规​​则,以允许流量进出其关联实例。您可以随时修改安全组的规则;新规则会自动应用于与安全组关联的所有实例。当我们决定是否允许流量到达某个实例时,我们会评估与该实例关联的所有安全组的所有规则。

引用https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html

向此安全组添加入站规则将解决您的问题。希望对您有所帮助。

关于java - 无法访问 AWS Tomcat 上的 MySQL 数据库,但可以在本地 Tomcat 上正常工作。 (JDBS 连接错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48621859/

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