Spring Web+JPA应用启动报错:HikariPool初始化失败及数据库连接故障
Spring Web+JPA应用启动数据库连接故障排查与解决
问题详情
启动报错信息
ERROR 15877 --- [ restartedMain] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization. ERROR 15877 --- [ restartedMain] o.h.engine.jdbc.spi.SqlExceptionHelper : Communications link failure ERROR 15877 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to open JDBC Connection for DDL execution ERROR 15877 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
应用配置文件(application.properties)
server.port=1337 # Configurations de la connexion à la base de données MySQL spring.datasource.url=jdbc:mysql://localhost:80/pgi_db spring.datasource.username=root spring.datasource.password=r68Mc72Ls7rmkJE spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # Configurations JPA spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jpa.defer-datasource-initialization=true spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
故障原因分析与解决方法
1. 数据库端口配置错误(核心原因)
你的数据库连接URL中使用了端口80,但MySQL默认服务端口为3306,端口不匹配直接导致通信链接失败。
解决方法:
修正数据库URL的端口,并补充MySQL 8.0+版本必填的时区参数和SSL配置:
spring.datasource.url=jdbc:mysql://localhost:3306/pgi_db?useSSL=false&serverTimezone=UTC
2. 其他排查与修复点
如果修改端口后仍报错,依次检查以下内容:
- MySQL服务状态:确认本地MySQL服务已启动。Linux可执行
systemctl status mysql,Windows可在服务列表中查看MySQL服务状态。 - 账号密码有效性:使用MySQL客户端登录验证账号密码是否正确:
输入配置中的密码mysql -u root -pr68Mc72Ls7rmkJE,若登录失败需修正密码配置。 - JPA方言与MySQL版本匹配:若你的MySQL版本为8.0+,需将方言替换为适配版本:
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect - 驱动类兼容性:
com.mysql.cj.jdbc.Driver是MySQL 8.0+的驱动类,若使用MySQL 5.x版本,需改为com.mysql.jdbc.Driver,同时调整项目依赖中的MySQL驱动版本。
内容的提问来源于stack exchange,提问作者ettalha0x
相关产品推荐
相关产品推荐

