运行Spring Boot应用时MySQL驱动与JDBC报错原因咨询
报错信息
"Caused by: java.lang.RuntimeException: Driver com.mysql.cj.jdbc.Driver claims to not accept jdbcUrl, mysql://localhost:3306"
"Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.RuntimeException: Driver com.mysql.cj.jdbc.Driver claims to not accept jdbcUrl, mysql://localhost:3306"
核心原因
JDBC URL前缀错误
MySQL Connector/J 8.x版本驱动要求JDBC URL必须以jdbc:mysql://开头,你当前配置的mysql://localhost:3306缺少jdbc:前缀,这是驱动拒绝该URL的直接原因。URL未指定目标数据库
正确的MySQL JDBC URL需要明确指向具体数据库名称,仅写localhost:3306没有指定要连接的数据库,驱动无法完成有效连接。(可选但建议)缺少时区参数
MySQL 8.x驱动默认使用UTC时区,若你的数据库服务器是国内时区(如Asia/Shanghai),未指定时区会引发额外兼容性问题,建议补充该参数。
正确配置示例
以Spring Boot的application.properties为例,标准数据源配置如下:
spring.datasource.url=jdbc:mysql://localhost:3306/你的数据库名称?serverTimezone=Asia/Shanghai spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=你的数据库用户名 spring.datasource.password=你的数据库密码
内容的提问来源于stack exchange,提问作者Zacharie

