java - Docker Mysql 8,Docker spring boot - 无法配置数据源:未指定“url”属性
问题描述
我一直在研究所有可能的排列,但没有成功。
->应用程序.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
->构建.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 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
推荐阅读
- python - 形状为 (5, 75, 100, 10) 的目标数组被传递为形状 (None, 74, 100, 10) 的输出,同时用作损失“mean_squared_error”
- powershell - -Match 中间的通配符
- c++ - 在合适的情况下编写索引是否会比仅从 c++ 中的性能 POV 中的 for 循环更好?
- sql - 有没有办法在没有子查询的情况下重写这个语句?
- python - 熊猫将 str 转换为日期并计算年龄
- c# - 英孚模型。导航属性只能参与单个关系
- c# - 我的 sql 问题错误列表未在 c# mvc 中转换 Int32
- javascript - 反应路由器 this.props.location 总是未定义
- cmake - Google Benchmark:“以调试模式构建的库”
- c# - 捏和缩放WIN2D