![](/img/trans.png)
[英]Docker Mysql 8, Docker spring boot - Failed to configure a DataSource: 'url' attribute is not specified
[英]Spring Boot DataSource: 'url' attribute is not specified
我正在尝试遵循本指南https://spring.io/guides/gs/accessing-data-mysql/
但本指南适用于 maven,我正在尝试 gradle
并得到这个错误
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
在 application.properties 我只有这些东西。
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
我尝试了 SO 上所有可能的东西。
我只有这些
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
这是一个老问题,但对于遇到这个问题的其他人来说,如果您有任何机会使用 IntelliJ(正如 OP 根据他/她的评论在此处所做的那样),请确保我们心爱的 IDE 识别您的 application.properties/application.yml因此,转到File -> Project Structure -> Modules
,然后 select 您的resources
文件,然后单击Mark as:
header 的“资源”(基于 IntelliJ 社区版 2019.1)。 还要记住,毫无疑问,IDE 肯定会通知您,通过重新导入任何 Maven 更改,您将需要再次执行此过程。
或者您必须通过将此行添加到文件application.properties
来防止 Spring 引导自动配置数据源。
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
检查目标类路径目录。 如果 application.properties 文件不存在,则删除目标并重建。
我会在其他地方搜索原因。
您的错误日志告诉您Reason: Failed to determine a suitable driver class
。 请构建您的项目,并检查 mysql 驱动程序是否包含在构建的 jar 文件中。 如果 jar 丢失,请尝试将 mysql 依赖项 scope 从runtimeOnly
为implementation
。 如果您尝试从 IDE 运行此项目,则可能会出现相同的问题(至少我认为如此),并且解决方案在这种情况下也应该有所帮助。
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
不幸的是,错误非常普遍,并且可能发生在许多不同的情况下。 例如,上周我在使用 oracle Oracle 时收到此错误,其中在properties.yaml
中指定的密码中有特殊符号。yaml 没有双引号。
我遇到了同样的问题,并通过将2.4.x
版本的项目迁移到2.3.x
来解决它。
在 IDE / STS(弹簧工具套装)中,一切正常
但是当制作一个“project.jar”文件时,
这是被抛出的。
“application.yml”文件中不必要的空格“”可能会导致这种情况。
server:
port: 8085
spring:
datasource:
url: jdbc:mysql://localhost:3306/studentdb
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
application:
name: STUDENT-SERVICE
而不是调整我的“application.yml”文件
我只是将“application.yml”文件中的所有语句移动到
“application.properties”文件并格式化“.properties”中所需的语句。
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/studentdb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format.sql=true
spring.application.name=student-service
server.port=8085
瞧
(您可以在网址末尾添加参数)
(spring.datasource.url=jdbc:mysql://localhost:3306/studentdb?allowPublicKeyRetrieval=true&useSSL=false)
确保你的 pom.xml 有战争包装。 它对我有用。
<packaging>war</packaging>
Spring 引导已将 url 更改为 jdbc-url。
您需要使用如下
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/db_example
强调一下,当然,我们会在文档中搜索“spring.cloud.config.jdbc-url”,但我们应该直接通过 spring.datasource.hikari.jdbc-url 进行搜索。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.