![](/img/trans.png)
[英]Spring boot Failed to configure a DataSource: 'url' attribute is not specified
[英]Docker Mysql 8, Docker spring boot - Failed to configure a DataSource: 'url' attribute is not specified
我一直在研究所有可能的排列,但没有成功。
->application.properties
## DATASOURCE
#local connection
#spring.datasource.url=jdbc:mysql://localhost:3306/Apple?useSSL=false&allowPublicKeyRetrieval=true&createDatabaseIfNotExist=true&autoReconnect=true
# docker network connection
spring.datasource.url=jdbc:mysql://mysqldb_container:3306/Apple?useSSL=false&allowPublicKeyRetrieval=true&createDatabaseIfNotExist=true&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
-> build.gradle
plugins {
id 'org.springframework.boot' version '2.2.4.RELEASE';
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}; group 'org.example'; version '1.0-SNAPSHOT'; sourceCompatibility = 1.8; repositories { mavenCentral() }; dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation('mysql:mysql-connector-java'); testCompile group: 'junit', name: 'junit', version: '4.12'
}
-> Dockerfile
From openjdk:8
copy build/libs/Java_Mysql_Docker-1.0-SNAPSHOT.jar java_mysql_docker_jar.jar
CMD ["java","-jar","java_mysql_docker_jar.jar"]
-> docker 命令
docker network create java_mysql_docker_network
docker container run --name mysqldb_container --network java_mysql_docker_network -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=Apple -p 3306:3306 -d mysql:8
gradle clean build
docker build -t java_mysql_docker_image .
docker container run --name java_mysql_docker_image_container --network java_mysql_docker_network -p 8080:8080 java_mysql_docker_image
->异常描述:
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
对于以下 URL 的本地安装的 MySQL 8,相同的应用程序工作正常
spring.datasource.url=jdbc:mysql://localhost:3306/Apple?useSSL=false&allowPublicKeyRetrieval=true&createDatabaseIfNotExist=true&autoReconnect=true
但 Docker MySQL 8 失败
spring.datasource.url=jdbc:mysql://mysqldb_container:3306/Apple?useSSL=false&allowPublicKeyRetrieval=true&createDatabaseIfNotExist=true&autoReconnect=true
我的猜测是。
# This driver is not in the classpath.
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Spring auto-detects the driver based on the JDBC URL. This one.
# Explicitly setting the driver that is in the class path.
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
您看到的错误是来自 spring-boot 项目的这个类:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html
显然评论驱动程序适用于本地安装和 docker MySQL 8。
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
但仍然想解释为什么会这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.