Spring Boot应用无法连接数据库,配置后启动失败求助
Spring Boot启动数据源配置错误排查
问题现象
启动Spring Boot应用时出现如下错误:
APPLICATION FAILED TO START *************************** Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the classpath. If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
相关配置与代码
application.yml配置
server: port: 8081 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/microservices username: root password: root jpa: hibernate: ddl-auto: update show-sql: true properties: hibernate: dialect: org.hibernate.dialect.MySQL5Dialect
pom.xml依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.lcwd.user.service</groupId> <artifactId>UserService</artifactId> <version>0.0.1-SNAPSHOT</version> <name>UserService</name> <description>This is User microservice</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
主类代码
package com.lcwd.user.service.UserService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class UserServiceApplication { public static void main(String[] args) { SpringApplication.run(UserServiceApplication.class, args); } }
解决方案
1. 检查配置文件位置与格式
- 确认
application.yml放在src/main/resources目录下,Spring Boot默认仅加载该目录下的配置文件。 - 严格检查yaml缩进:所有子属性必须用两个空格缩进,禁止使用tab,格式错误会导致配置无法被解析。
2. 验证MySQL驱动依赖与版本匹配
- 执行
mvn clean install重新拉取依赖,确保mysql-connector-j已正确下载到本地maven仓库。 - 若本地MySQL是5.x版本,需将
driver-class-name改为com.mysql.jdbc.Driver;8.x版本则保留com.mysql.cj.jdbc.Driver。
3. 核对数据库连接参数
- 确认本地MySQL服务已启动,端口3306未被防火墙或其他程序占用。
- 检查
microservices数据库是否已创建,若不存在需先手动创建。 - 验证
root账号和密码是否能正常登录本地MySQL,避免账号密码错误。
4. 排查DevTools影响
Spring Boot DevTools可能引发配置加载异常,可临时注释掉pom.xml中的devtools依赖,重启应用测试是否恢复正常。
5. 排除自动配置冲突(可选)
若以上步骤均无效,可尝试在主类中排除数据源自动配置(仅适用于特殊场景,不推荐优先使用):
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
内容的提问来源于stack exchange,提问作者Arpit Awasthi
相关产品推荐
相关产品推荐

