原因:

MySQL连接异常,报错:Communications link failure,一般是MySQL版本兼容问题。可以采用如下方法解决:
mysql-connector-java:8.0.18版本时用driver-class-name:com.mysql.cj.jdbc.Driver(而8.0以下只能使用com.mysql.jdbc.Driver(去掉cj);而且8.0版本以上需要设置时区为:serverTimezone=GMT%2B8)
使用命令select version()查看MySQL版本:5.7.39,
检查数据库连接参数:在application.properties 文件中,配置如下:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/book?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456

解决:

问题是在url配置中useSSL=true参数,MySQL在高版本需要指明是否进行SSL连接,MySQL5是不需要这个参数的。

解决方案是:useSSL=false。即改成如下配置:

spring.datasource.url=jdbc:mysql://localhost:3306/book?serverTimezone=GMT%2B8&useSSL=false

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐