问题
我在EC2实例中放了个Java程序。当我执行 java -jar app.jar 命令时,SpringBoot应用程序能正常启动,但连接MySQL RDS数据库时却失败了。
已经打开了应用程序正在运行的端口(8090)和MySql端口(3306),用于入站和出站流量:

错误日志
1 2 3 4 5 6 7 8 9 10 |
2020-10-30 17:46:38.132 INFO 10161 --- [main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8090 2020-10-30 17:46:38.604 INFO 10161 --- [main] o.apache.catalina.core.StandardService : Starting service Tomcat 2020-10-30 17:46:38.605 INFO 10161 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.54 2020-10-30 17:46:38.724 INFO 10161 --- [ost startStop 1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-10-30 17:46:38.725 INFO 10161 --- [ost startStop 1] o.s.web.context.ContextLoader: Root WebApplicationContext: initialization completed in 5028 ms 2020-10-30 17:48:48.476 ERROR 10161 --- [ost startStop 1] o.a.tomcat.jdbc.pool.ConnectionPool: Unable to create initial connections of pool. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. |
解决
检查EC2实例和RDS实例的安全组(SG)配置。
可以通过EC2仪表板/ RDS仪表板->单击实例并查看“安全组”描述来进行检查,也可以单击“设置”图标(“显示/隐藏”列)并勾选“安全组”。
在RDS的SG配置中:确保已启用从EC2实例的SG到端口3306的访问。可以通过将EC2实例的SG ID作为“自定义IP”值放入配置的“源”字段中来进行此操作。有关更多详细信息,参考下面。
使用mysql命令行测试EC2实例和RDS之间的连接。
参考
VPC Introduction和Security in your VPC和Scenarios for Accessing a DB Instance in a VPC
0