远程连接mysql返回连链接失败.日志内容如下,
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

com.mysql.cj.jdbc.exceptions.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. 
        at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.22.jar!/:8.0.22] 
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.22.jar!/:8.0.22] 
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.22.jar!/:8.0.22] 
        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) ~[mysql-connector-java-8.0.22.jar!/:8.0.22] 
        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) ~[mysql-connector-java-8.0.22.jar!/:8.0.22] 
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-java-8.0.22.jar!/:8.0.22] 
        at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.5.jar!/:na] 
        at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.5.jar!/:na] 
        at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar!/:na] 
        at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) [HikariCP-3.4.5.jar!/:na] 
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) [HikariCP-3.4.5.jar!/:na] 
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.4.5.jar!/:na] 
        at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.4.5.jar!/:na] 
        at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158) [spring-jdbc-5.3.2.jar!/:5.3.2] 
        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116) [spring-jdbc-5.3.2.jar!/:5.3.2] 
        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79) [spring-jdbc-5.3.2.jar!/:5.3.2] 
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:330) [spring-jdbc-5.3.2.jar!/:5.3.2] 
        at org.springframework.boot.jdbc.EmbeddedDatabaseConnection.isEmbedded(EmbeddedDatabaseConnection.java:182) [spring-boot-2.4.1.jar!/:2.4.1] 
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateDefaultDdlAutoProvider.getDefaultDdlAuto(HibernateDefaultDdlAutoProvider.java:42) [spring-boot-autoconfigure-2.4.1.jar!/:2.4.1] 
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.lambda$getVendorProperties$1(HibernateJpaConfiguration.java:130) [spring-boot-autoconfigure-2.4.1.jar!/:2.4.1] 
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings.getDdlAuto(HibernateSettings.java:41) ~[spring-boot-autoconfigure-2.4.1.jar!/:2.4.1] 
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.determineDdlAuto(HibernateProperties.java:136) ~[spring-boot-autoconfigure-2.4.1.jar!/:2.4.1] 
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.getAdditionalProperties(HibernateProperties.java:102) ~[spring-boot-autoconfigure-2.4.1.jar!/:2.4.1] 
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.determineHibernateProperties(HibernateProperties.java:94) ~[spring-boot-autoconfigure-2.4.1.jar!/:2.4.1] 
        at org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.getVendorProperties(HibernateJpaConfiguration.java:132) [spring-boot-autoconfigure-2.4.1.jar!/:2.4.1] 
        at org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(JpaBaseConfiguration.java:134) ~[spring-boot-autoconfigure-2.4.1.jar!/:2.4.1] 
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91] 
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91] 
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91] 
        at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_91] 

改过各种连接字符串.仍然没效果.

后来发现,是版本问题在这里找到了答案

Application配置

#数据库相关配置 
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 
spring.datasource.url=jdbc:mysql://**.mysql.rds.aliyuncs.com:3306/testdb?characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai 
spring.datasource.characterEncoding=utf-8 
spring.datasource.useSSL=false 
spring.datasource.username=dbuser 
spring.datasource.password=**** 
spring.datasource.max-idle=10 
spring.datasource.max-wait=10000 
spring.datasource.min-idle=5 
spring.datasource.initial-size=5 
spring.jpa.show-sql=true 
spring.jpa.generate-ddl=true 
# spring.jpa.hibernate.ddl-auto=update 
# spring.datasource.platform=org.hibernate.dialect.MySQL57Dialect 

pom.xml中的配置

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

这个版本不知道该选哪个的话, 可以通过mysql客户端,连接以后执行

show variables like '%version%' 

找到对应的版本之后,
到maven 库中看看存不存在同样版本的jar包.

https://mvnrepository.com/artifact/mysql/mysql-connector-java

我的pom.xml配置 之前不存在8.0.18 后来自己加的, 然后刷新maven包就可以了刷新方法见下图.
idea更新maven依赖包 连接mysql 一直连接不上._java
看到版本号正确即可.
idea更新maven依赖包 连接mysql 一直连接不上._mysql_02