Spring Boot应用启动失败:DataSource配置错误,请求排查修复
Spring Boot启动失败:DataSource配置问题
启动错误信息
APPLICATION FAILED TO START
描述:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
原因:Failed to determine a suitable driver class
建议:
请考虑以下方案:
如果您需要嵌入式数据库(H2、HSQL或Derby),请将其添加到类路径中。
如果您需要从特定配置加载数据库设置,可能需要激活该配置(当前已激活prod配置)。
我的配置文件
application.properties
debug=true server.port=9090 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect #create, update, create-drop, validate spring.jpa.hibernate.ddl-auto=update #file related all configurations spring.servlet.multipart.max-file-size=10MB spring.servlet.multipart.max-request-size=10MB project.image=images/ #here we are allowing debug so that we can get every log related to security on console logging.level.org.springframework.security=DEBUG #spring.profiles.active will tell us which profile we are using spring.profiles.active= prod
application-developer.properties
spring.datasource.url=jdbc:mysql://localhost:3306/blog_app_apis spring.datasource.username = spring.datasource.password= spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.show-sql=true
application-production.properties
spring.datasource.url= jdbc:mysql://blog-db.cx8gwdujroiq.ap-south-1.rds.amazonaws.com:3306/blog_app_apis spring.datasource.username = pankaj spring.datasource.password=kicker12345
问题分析与修复方法
- prod环境缺少驱动类配置:当前激活的是
prod环境,但application-production.properties未配置spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver,导致Spring无法识别MySQL驱动。需在该文件中添加该行配置。 - 配置项空格问题:
spring.datasource.url、username、password等配置的=后存在多余空格,会导致Spring无法正确读取配置值。需删除=后的空格,例如修改为spring.datasource.url=jdbc:mysql://...、spring.datasource.username=pankaj。 - 依赖缺失检查:确保项目依赖中已引入MySQL驱动。
Maven依赖示例:
Gradle依赖示例:<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>runtimeOnly 'mysql:mysql-connector-java'
内容的提问来源于stack exchange,提问作者Pankaj
相关产品推荐
相关产品推荐

