Springboot中mysql连接失败问题

问题:2024-06-03 13:31:25.128 ERROR 12340 --- [ main] com.zaxxer.hikari.pool.HikariPool : mysql - Exception during pool initialization. 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.

解决方案:1.有可能是mysql使用8.0版本,但驱动仍使用老版本,将application中:spring.datasource.dynamic.datasource.mysql.driver-class-name=com.mysql.cj.jdbc.Driver

 2.检查主机名,用户名,密码是否错误

依旧报错:The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone conf

解决方案:这是时区问题,spring.datasource.dynamic.datasource.mysql.url=jdbc:mysql://localhost:3306/user_profile_manager1214?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC

最终运行成功。

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
连接 MySQL 数据库时,可能会出现以下几种错误: 1. 驱动包未导入:如果你没有在项目导入 MySQL 驱动包,则会导致连接 MySQL 失败。可以在 Maven 或 Gradle 添加以下依赖来解决这个问题: Maven: ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> ``` Gradle: ```groovy implementation 'mysql:mysql-connector-java:8.0.23' ``` 2. 数据库 URL 不正确:在 application.properties 或 application.yml 文件配置数据库连接 URL 时,需要确保 URL 的格式正确。例如,MySQL 默认端口是 3306,如果你指定了不正确的端口,则会导致连接失败。 application.properties: ``` spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 ``` application.yml: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: 123456 ``` 3. 用户名或密码不正确:如果你的用户名或密码不正确,则连接 MySQL失败。确保你在配置文件指定了正确的用户名和密码。 application.properties: ``` spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 ``` application.yml: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: 123456 ``` 如果你仍然无法解决连接 MySQL问题,可以查看控制台输出的错误信息或者查看 MySQL 的日志文件,以便更好地诊断问题

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值